public function actionNewPassword($key = false)
 {
     if ($key) {
         $user = FrontendUser::model()->findByAttributes(array('recover_pwd_key' => $key));
         if ($user && time() < strtotime($user->recover_pwd_expiration)) {
             $model = new NewPasswordForm();
             if (isset($_POST['NewPasswordForm'])) {
                 $model->attributes = $_POST['NewPasswordForm'];
                 if ($model->validate()) {
                     $user->password = $model->password;
                     $user->recover_pwd_key = '';
                     $user->recover_pwd_expiration = date('Y-m-d h:i:s', time() - 1);
                     if ($user->save()) {
                         echo '{"status": "ok"}';
                     } else {
                         echo json_encode(array('status' => 'fail', 'errors' => CHtml::errorSummary($user)));
                     }
                 } else {
                     echo json_encode(array('status' => 'fail', 'errors' => CHtml::errorSummary($model)));
                 }
             }
         } else {
             throw new CHttpException(404, 'Not found or this link already expired');
         }
     } else {
         $model = new ForgotPasswordForm();
         if (isset($_POST['ForgotPasswordForm'])) {
             $model->attributes = $_POST['ForgotPasswordForm'];
             if ($model->validate()) {
                 $user = FrontendUser::model()->findByAttributes(array('email' => $model->email));
                 if ($user) {
                     Yii::app()->user->setFlash('success', 'Вы получите письмо с инструкциями как восстановить ваш пароль.');
                     EmailManager::sendRecoveryPassword($user);
                     $this->refresh();
                 } else {
                     $model->addError('email', 'Email address not found');
                 }
             }
         }
         $this->render('recoveryPassword', array('model' => $model));
     }
 }
 public function actionNewPassword($key = false)
 {
     if ($key) {
         $user = User::model()->findByAttributes(array('recover_pwd_key' => $key));
         if ($user && time() < strtotime($user->recover_pwd_expiration)) {
             $model = new NewPasswordForm();
             if (isset($_POST['NewPasswordForm'])) {
                 $model->attributes = $_POST['NewPasswordForm'];
                 if ($model->validate()) {
                     $user->password = $model->password;
                     $user->recover_pwd_key = '';
                     $user->recover_pwd_expiration = date('Y-m-d h:i:s', time() - 1);
                     if ($user->save()) {
                         Yii::app()->user->setFlash('success', 'You have successfully changed your password. You may now use it to login to Present Value.');
                     } else {
                         $model->addErrors($user->errors);
                     }
                 }
             }
             $this->render('newPassword', array('model' => $model));
         } else {
             throw new CHttpException(404, 'Not found or this link already expired');
         }
     } else {
         $model = new ForgotPasswordForm();
         if (isset($_POST['ForgotPasswordForm'])) {
             $model->attributes = $_POST['ForgotPasswordForm'];
             if ($model->validate()) {
                 $user = User::model()->findByAttributes(array('email' => $model->email));
                 if ($user) {
                     Yii::app()->user->setFlash('success', 'You will receive an email shortly with instructions to create a new password.');
                     EmailManager::sendRecoveryPassword($user);
                     $this->refresh();
                 } else {
                     $model->addError('email', 'Email address not found');
                 }
             }
         }
         $this->render('recoveryPassword', array('model' => $model));
     }
 }