/**
  * Resets password.
  *
  * @return boolean if password was reset.
  * @throws \yii\db\StaleObjectException
  * @throws \Exception
  */
 public function resetPassword()
 {
     $user = $this->token->user;
     $user->password = $this->password;
     if ($user->save()) {
         $this->token->delete();
     }
     return true;
 }
 /**
  * Sends an email with a link, for resetting the password.
  *
  * @return boolean whether the email was send
  */
 public function sendEmail()
 {
     /* @var $user User */
     $user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $this->email]);
     if ($user) {
         $token = UserToken::create($user->id, UserToken::TYPE_PASSWORD_RESET, Time::SECONDS_IN_A_DAY);
         if ($user->save()) {
             return Yii::$app->commandBus->handle(new SendEmailCommand(['to' => $this->email, 'subject' => Yii::t('frontend', 'Password reset for {name}', ['name' => Yii::$app->name]), 'view' => 'passwordResetToken', 'params' => ['user' => $user, 'token' => $token->token]]));
         }
     }
     return false;
 }
Example #3
0
 /**
  * Signs user up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function signup()
 {
     if ($this->validate()) {
         $shouldBeActivated = $this->shouldBeActivated();
         $user = new User();
         $user->username = $this->username;
         $user->email = $this->email;
         $user->is_activated = !$shouldBeActivated;
         $user->setPassword($this->password);
         $user->save();
         $user->afterSignup();
         if ($shouldBeActivated) {
             $token = UserToken::create($user->id, UserToken::TYPE_ACTIVATION, Time::SECONDS_IN_A_DAY);
             Yii::$app->commandBus->handle(new SendEmailCommand(['subject' => Yii::t('frontend', 'Activation email'), 'view' => 'activation', 'params' => ['url' => Url::to(['/user/sign-in/activation', 'token' => $token->token], true)]]));
         }
         return $user;
     }
     return null;
 }
Example #4
0
 /**
  *
  */
 public function getToken($force = false)
 {
     if ($this->_token === null) {
         $this->_token = UserToken::getToken($this->getId(), $force);
     }
     return $this->_token;
 }
Example #5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getUserTokens()
 {
     return $this->hasMany(UserToken::className(), ['user_id' => 'id']);
 }
 public function actionActivation($token)
 {
     $token = UserToken::find()->byType(UserToken::TYPE_ACTIVATION)->byToken($token)->notExpired()->one();
     if (!$token) {
         throw new BadRequestHttpException();
     }
     $user = $token->user;
     $user->updateAttributes(['is_activated' => true]);
     $token->delete();
     Yii::$app->getUser()->login($user);
     Yii::$app->getSession()->setFlash('alert', ['body' => Yii::t('frontend', 'Your account has been successfully activated.'), 'options' => ['class' => 'alert-success']]);
     return $this->goHome();
 }