diff --git a/apps/advanced/common/models/User.php b/apps/advanced/common/models/User.php index 514b570..e4101e8 100644 --- a/apps/advanced/common/models/User.php +++ b/apps/advanced/common/models/User.php @@ -112,6 +112,7 @@ class User extends ActiveRecord implements Identity array('email', 'required'), array('email', 'email'), array('email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'), + array('email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'), array('password', 'required'), array('password', 'string', 'min' => 6), @@ -124,6 +125,7 @@ class User extends ActiveRecord implements Identity 'signup' => array('username', 'email', 'password'), 'login' => array('username', 'password'), 'resetPassword' => array('password'), + 'requestPasswordResetToken' => array('email'), ); } diff --git a/apps/advanced/frontend/controllers/SiteController.php b/apps/advanced/frontend/controllers/SiteController.php index 44a7fd9..93f161b 100644 --- a/apps/advanced/frontend/controllers/SiteController.php +++ b/apps/advanced/frontend/controllers/SiteController.php @@ -8,7 +8,6 @@ use common\models\LoginForm; use frontend\models\ContactForm; use common\models\User; use yii\web\HttpException; -use frontend\models\SendPasswordResetTokenForm; class SiteController extends Controller { @@ -48,7 +47,7 @@ class SiteController extends Controller { $model = new ContactForm; if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) { - Yii::$app->session->setFlash('contactFormSubmitted'); + Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.'); return $this->refresh(); } else { return $this->render('contact', array( @@ -77,37 +76,70 @@ class SiteController extends Controller )); } - public function actionResetPassword($token = null) + public function actionRequestPasswordReset() { - if ($token) { - $model = User::find(array( - 'password_reset_token' => $token, - 'status' => User::STATUS_ACTIVE, - )); - - if (!$model) { - throw new HttpException(400, 'Wrong password reset token.'); - } - - $model->scenario = 'resetPassword'; - if ($model->load($_POST) && $model->save()) { - // TODO: confirm that password was successfully saved + $model = new User(); + $model->scenario = 'requestPasswordResetToken'; + if ($model->load($_POST) && $model->validate()) { + if ($this->sendPasswordResetEmail($email)) { + Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.'); $this->redirect('index'); + } else { + Yii::$app->getSession()->setFlash('error', 'There was an error sending email.'); } + } + $this->render('requestPasswordResetToken', array( + 'model' => $model, + )); + } - $this->render('resetPassword', array( - 'model' => $model, - )); + public function actionResetPassword($token) + { + $model = User::find(array( + 'password_reset_token' => $token, + 'status' => User::STATUS_ACTIVE, + )); + + if (!$model) { + throw new HttpException(400, 'Wrong password reset token.'); } - else { - $model = new SendPasswordResetTokenForm(); - if ($model->load($_POST) && $model->sendEmail()) { - // TODO: confirm that password reset token was sent - $this->redirect('index'); - } - $this->render('sendPasswordResetTokenForm', array( - 'model' => $model, + + $model->scenario = 'resetPassword'; + if ($model->load($_POST) && $model->save()) { + Yii::$app->getSession()->setFlash('success', 'New password was saved.'); + $this->redirect('index'); + } + + $this->render('resetPassword', array( + 'model' => $model, + )); + } + + private function sendPasswordResetEmail($email) + { + $user = User::find(array( + 'status' => User::STATUS_ACTIVE, + 'email' => $email, + )); + + if (!$user) { + return false; + } + + $user->password_reset_token = Security::generateRandomKey(); + if ($user->save(false)) { + $fromEmail = \Yii::$app->params['supportEmail']; + $name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?='; + $subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?='; + $body = $this->renderPartial('/emails/passwordResetToken', array( + 'user' => $this, )); + $headers = "From: $name <{$fromEmail}>\r\n" . + "MIME-Version: 1.0\r\n" . + "Content-type: text/plain; charset=UTF-8"; + return mail($fromEmail, $subject, $body, $headers); } + + return false; } } diff --git a/apps/advanced/frontend/models/SendPasswordResetTokenForm.php b/apps/advanced/frontend/models/SendPasswordResetTokenForm.php deleted file mode 100644 index 5e48084..0000000 --- a/apps/advanced/frontend/models/SendPasswordResetTokenForm.php +++ /dev/null @@ -1,59 +0,0 @@ -validate()) { - /** @var User $user */ - $user = User::find(array( - 'email' => $this->email, - 'status' => User::STATUS_ACTIVE, - )); - if ($user) { - $user->password_reset_token = Security::generateRandomKey(); - if ($user->save(false)) { - $view = new View(array( - 'context' => \Yii::$app->controller, - )); - - $fromEmail = \Yii::$app->params['supportEmail']; - $name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?='; - $subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?='; - $body = $view->render('/emails/passwordResetToken', array( - 'user' => $user, - )); - $headers = "From: $name <{$fromEmail}>\r\n" . - "MIME-Version: 1.0\r\n" . - "Content-type: text/plain; charset=UTF-8"; - mail($fromEmail, $subject, $body, $headers); - return true; - } - } - } - - return false; - } -} diff --git a/apps/advanced/frontend/views/layouts/main.php b/apps/advanced/frontend/views/layouts/main.php index 391f7c8..f123892 100644 --- a/apps/advanced/frontend/views/layouts/main.php +++ b/apps/advanced/frontend/views/layouts/main.php @@ -3,6 +3,7 @@ use frontend\config\AppAsset; use yii\helpers\Html; use yii\widgets\Menu; use yii\widgets\Breadcrumbs; +use frontend\widgets\Alert; /** * @var $this \yii\base\View @@ -54,6 +55,9 @@ AppAsset::register($this); isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : array(), )); ?> + + +
diff --git a/apps/advanced/frontend/views/site/contact.php b/apps/advanced/frontend/views/site/contact.php index 41e87db..62bb9ef 100644 --- a/apps/advanced/frontend/views/site/contact.php +++ b/apps/advanced/frontend/views/site/contact.php @@ -13,12 +13,6 @@ $this->params['breadcrumbs'][] = $this->title; ?>

title); ?>

-session->hasFlash('contactFormSubmitted')): ?> -
- Thank you for contacting us. We will respond to you as soon as possible. -
- -

If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.

diff --git a/apps/advanced/frontend/views/site/login.php b/apps/advanced/frontend/views/site/login.php index ea9f456..fef5ace 100644 --- a/apps/advanced/frontend/views/site/login.php +++ b/apps/advanced/frontend/views/site/login.php @@ -22,3 +22,5 @@ $this->params['breadcrumbs'][] = $this->title; 'btn btn-primary')); ?> + +

If you forgot your password you can .

\ No newline at end of file diff --git a/apps/advanced/frontend/views/site/sendPasswordResetTokenForm.php b/apps/advanced/frontend/views/site/requestPasswordResetToken.php similarity index 91% rename from apps/advanced/frontend/views/site/sendPasswordResetTokenForm.php rename to apps/advanced/frontend/views/site/requestPasswordResetToken.php index 4d8c429..f0bb587 100644 --- a/apps/advanced/frontend/views/site/sendPasswordResetTokenForm.php +++ b/apps/advanced/frontend/views/site/requestPasswordResetToken.php @@ -5,7 +5,7 @@ use yii\widgets\ActiveForm; /** * @var yii\base\View $this * @var yii\widgets\ActiveForm $form - * @var frontend\models\SendPasswordResetTokenForm $model + * @var frontend\models\User $model */ $this->title = 'Request password reset'; $this->params['breadcrumbs'][] = $this->title; diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php new file mode 100644 index 0000000..f3a31cb --- /dev/null +++ b/apps/advanced/frontend/widgets/Alert.php @@ -0,0 +1,40 @@ +getSession()->setFlash('error', 'This is the message'); + * - \Yii::$app->getSession()->setFlash('success', 'This is the message'); + * - \Yii::$app->getSession()->setFlash('info', 'This is the message'); + * + * @author Alexander Makarov + */ +class Alert extends \yii\bootstrap\Alert +{ + public function init() + { + if ($this->body = \Yii::$app->getSession()->getFlash('error')) { + Html::addCssClass($this->options, 'alert-error'); + } elseif ($this->body = \Yii::$app->getSession()->getFlash('success')) { + Html::addCssClass($this->options, 'alert-success'); + } elseif ($this->body = \Yii::$app->getSession()->getFlash('info')) { + Html::addCssClass($this->options, 'alert-info'); + } elseif ($this->body = \Yii::$app->getSession()->getFlash('warning')) { + + } else { + // no message passed, no need to render widget + return; + } + + parent::init(); + } +} \ No newline at end of file