Alexander Makarov
12 years ago
8 changed files with 165 additions and 5 deletions
@ -0,0 +1,59 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace frontend\models; |
||||||
|
use yii\base\Model; |
||||||
|
use common\models\User; |
||||||
|
use yii\base\View; |
||||||
|
use yii\helpers\SecurityHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* SendPasswordResetTokenForm is the model behind requesting password reset token form. |
||||||
|
*/ |
||||||
|
class SendPasswordResetTokenForm extends Model |
||||||
|
{ |
||||||
|
public $email; |
||||||
|
|
||||||
|
/** |
||||||
|
* @return array the validation rules. |
||||||
|
*/ |
||||||
|
public function rules() |
||||||
|
{ |
||||||
|
return array( |
||||||
|
array('email', 'required'), |
||||||
|
array('email', 'email'), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
public function sendEmail() |
||||||
|
{ |
||||||
|
if($this->validate()) { |
||||||
|
/** @var User $user */ |
||||||
|
$user = User::find(array( |
||||||
|
'email' => $this->email, |
||||||
|
'status' => User::STATUS_ACTIVE, |
||||||
|
)); |
||||||
|
if ($user) { |
||||||
|
$user->password_reset_token = SecurityHelper::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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
<?php |
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var common\models\User $user; |
||||||
|
*/ |
||||||
|
|
||||||
|
$resetLink = $this->context->createUrl('site/resetPassword', array('token' => $user->password_reset_token)); |
||||||
|
?> |
||||||
|
|
||||||
|
Hello <?php echo Html::encode($user->username)?>,
|
||||||
|
|
||||||
|
Follow the link below to reset your password: |
||||||
|
|
||||||
|
<?php Html::a(Html::encode($resetLink), $resetLink)?> |
@ -0,0 +1,22 @@ |
|||||||
|
<?php |
||||||
|
use yii\helpers\Html; |
||||||
|
use yii\widgets\ActiveForm; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\widgets\ActiveForm $form |
||||||
|
* @var common\models\User $model |
||||||
|
*/ |
||||||
|
$this->title = 'Reset password'; |
||||||
|
$this->params['breadcrumbs'][] = $this->title; |
||||||
|
?> |
||||||
|
<h1><?php echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<p>Please choose your new password:</p> |
||||||
|
|
||||||
|
<?php $form = ActiveForm::begin(array('options' => array('class' => 'form-horizontal'))); ?> |
||||||
|
<?php echo $form->field($model, 'password')->passwordInput(); ?> |
||||||
|
<div class="form-actions"> |
||||||
|
<?php echo Html::submitButton('Save', array('class' => 'btn btn-primary')); ?> |
||||||
|
</div> |
||||||
|
<?php ActiveForm::end(); ?> |
@ -0,0 +1,22 @@ |
|||||||
|
<?php |
||||||
|
use yii\helpers\Html; |
||||||
|
use yii\widgets\ActiveForm; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\widgets\ActiveForm $form |
||||||
|
* @var frontend\models\SendPasswordResetTokenForm $model |
||||||
|
*/ |
||||||
|
$this->title = 'Request password reset'; |
||||||
|
$this->params['breadcrumbs'][] = $this->title; |
||||||
|
?> |
||||||
|
<h1><?php echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<p>Please fill out your email. A link to reset password will be sent there.</p> |
||||||
|
|
||||||
|
<?php $form = ActiveForm::begin(array('options' => array('class' => 'form-horizontal'))); ?> |
||||||
|
<?php echo $form->field($model, 'email')->textInput(); ?> |
||||||
|
<div class="form-actions"> |
||||||
|
<?php echo Html::submitButton('Send', array('class' => 'btn btn-primary')); ?> |
||||||
|
</div> |
||||||
|
<?php ActiveForm::end(); ?> |
Loading…
Reference in new issue