public function sendForgotEmail()
 {
     /** @var User $user */
     $user = User::findOne(['email' => $this->email]);
     if (!$user) {
         throw new Exception('Não foi identificado usuário com o e-mail informado: ' . $this->email);
     }
     $user->setScenario('forgot');
     if (!$user->generateToken()) {
         throw new Exception('Houve um erro ao tentar gerar o seu Token de acesso.');
     }
     return Yii::$app->mailer->compose(['html' => 'layouts/reset-password'], ['token' => $user->token])->setFrom(['*****@*****.**' => Yii::$app->name])->setTo($user->email)->setSubject('Recuperação de Senha - ' . Yii::$app->name)->send();
 }
Ejemplo n.º 2
0
 /**
  * @return bool
  * @throws UserException
  */
 public function login()
 {
     /** @var User $user */
     $user = User::findOne(['email' => $this->email, 'status' => Yii::$app->params['active']]);
     $user->senha = $user->currentPassword;
     if (!$user) {
         throw new UserException('E-mail e/ou senha são invalidos.');
     }
     if (!Yii::$app->getSecurity()->validatePassword($this->password, $user->senha)) {
         throw new UserException('E-mail e/ou senha são invalidos.');
     }
     $identity = User::findIdentity($user->id);
     return Yii::$app->user->login($identity, (bool) $this->rememberMe ? 3600 * 24 * 30 : 0);
 }
 public function actionRenewPassword($token)
 {
     $this->controllerDescription = 'Alterar senha de Acesso';
     try {
         if (empty($token) || is_null($token)) {
             throw new Exception('Não foi informado o seu token de alteração de senha.');
         }
         /** @var User $user */
         $user = User::findOne(['token' => $token]);
         if (!$user) {
             throw new Exception('O Token informado é inválido e/ou está fora de validade.');
         }
         $form = new RenewPasswordForm();
         if ($form->load($this->getPost()) && $form->validate()) {
             $form->generateNewPassword($user);
             $this->getSession()->setFlash('success', 'Sua senha de acesso foi alterada com sucesso! Entre no sistema com sua nova senha.');
             return $this->redirect(Yii::$app->user->loginUrl);
         }
         return $this->render('renew-password', ['formModel' => $form]);
     } catch (Exception $e) {
         $this->getSession()->setFlash('warning', $e->getMessage());
         return $this->redirect(['forgot']);
     }
 }