示例#1
0
 public function actionForgot()
 {
     if (!\Yii::$app->user->isGuest) {
         return $this->redirect($this->cabinetAction());
     }
     $model = new Forgot();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $recoverHash = Yii::$app->security->generateRandomString();
         $rowsUpdated = User::updateAll(['recover_hash' => $recoverHash], 'email = :email', [':email' => $model->email]);
         if ($rowsUpdated === 0) {
             throw new HttpException(500, Yii::t('app', 'Database error'));
         }
         $recoveryUrl = \yii\helpers\Url::to(['recover', self::HASH_PARAM_NAME => $recoverHash], true);
         $isSent = Yii::$app->mailer->compose('user_recoverPassword', ['recoveryUrl' => $recoveryUrl])->setTo($model->email)->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->params['adminFrom']])->setSubject(Yii::t('app', 'Password recovery'))->send();
         if (!$isSent) {
             throw new HttpException(500, Yii::t('app', 'Email sending error occurred'));
         }
         $alert = \yii\bootstrap\Alert::widget(['options' => ['class' => 'alert-success'], 'body' => Yii::t('app', 'Email with further instructions was sent')]);
         Yii::$app->getSession()->setFlash('success', $alert);
     }
     return $this->render('forgot', ['model' => $model]);
 }