Реализована форма восстановления пароля от аккаунта

Логика проверки пароля вынесена в отдельный валидатор
В composer.json докинута зависимость от php7
This commit is contained in:
ErickSkrauch
2016-05-12 01:13:19 +03:00
parent ebf4947c37
commit 2a4da87fd5
9 changed files with 201 additions and 8 deletions

View File

@@ -1,9 +1,9 @@
<?php
namespace api\models;
use api\models\base\ApiForm;
use api\models\base\PasswordProtectedForm;
use common\models\Account;
use common\validators\PasswordValidate;
use Yii;
use yii\helpers\ArrayHelper;
@@ -25,9 +25,8 @@ class ChangePasswordForm extends PasswordProtectedForm {
*/
public function rules() {
return ArrayHelper::merge(parent::rules(), [
['newPassword', 'required', 'message' => 'error.newPassword_required'],
['newRePassword', 'required', 'message' => 'error.newRePassword_required'],
['newPassword', 'string', 'min' => 8, 'tooShort' => 'error.password_too_short'],
[['newPassword', 'newRePassword'], 'required', 'message' => 'error.{attribute}_required'],
['newPassword', PasswordValidate::class],
['newRePassword', 'validatePasswordAndRePasswordMatch'],
['logoutAll', 'boolean'],
]);

View File

@@ -0,0 +1,71 @@
<?php
namespace api\models;
use api\models\base\KeyConfirmationForm;
use common\models\EmailActivation;
use common\validators\PasswordValidate;
use Yii;
use yii\base\ErrorException;
class RecoverPasswordForm extends KeyConfirmationForm {
public $newPassword;
public $newRePassword;
public function rules() {
return array_merge(parent::rules(), [
[['newPassword', 'newRePassword'], 'required', 'message' => 'error.{attribute}_required'],
['newPassword', PasswordValidate::class],
['newRePassword', 'validatePasswordAndRePasswordMatch'],
]);
}
public function validatePasswordAndRePasswordMatch($attribute) {
if (!$this->hasErrors()) {
if ($this->newPassword !== $this->newRePassword) {
$this->addError($attribute, 'error.rePassword_does_not_match');
}
}
}
public function recoverPassword() {
if (!$this->validate()) {
return false;
}
$confirmModel = $this->getActivationCodeModel();
if ($confirmModel->type !== EmailActivation::TYPE_FORGOT_PASSWORD_KEY) {
$confirmModel->delete();
// TODO: вот где-то здесь нужно ещё попутно сгенерировать соответствующую ошибку
return false;
}
$transaction = Yii::$app->db->beginTransaction();
try {
$account = $confirmModel->account;
$account->password = $this->newPassword;
if (!$confirmModel->delete()) {
throw new ErrorException('Unable remove activation key.');
}
if (!$account->save()) {
throw new ErrorException('Unable activate user account.');
}
$transaction->commit();
} catch (ErrorException $e) {
$transaction->rollBack();
if (YII_DEBUG) {
throw $e;
} else {
return false;
}
}
// TODO: ещё было бы неплохо уведомить пользователя о том, что его E-mail изменился
return $account->getJWT();
}
}

View File

@@ -7,6 +7,7 @@ use common\components\UserFriendlyRandomKey;
use common\models\Account;
use common\models\confirmations\RegistrationConfirmation;
use common\models\EmailActivation;
use common\validators\PasswordValidate;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\base\ErrorException;
@@ -30,7 +31,7 @@ class RegistrationForm extends ApiForm {
['password', 'required', 'message' => 'error.password_required'],
['rePassword', 'required', 'message' => 'error.rePassword_required'],
['password', 'string', 'min' => 8, 'tooShort' => 'error.password_too_short'],
['password', PasswordValidate::class],
['rePassword', 'validatePasswordAndRePasswordMatch'],
];
}