mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Добавлена форма смены пароля и её тесты
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace api\controllers;
|
||||
|
||||
use api\models\ChangePasswordForm;
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
use yii\filters\AccessControl;
|
||||
@ -14,7 +15,7 @@ class AccountsController extends Controller {
|
||||
'class' => AccessControl::class,
|
||||
'rules' => [
|
||||
[
|
||||
'actions' => ['current'],
|
||||
'actions' => ['current', 'change-password'],
|
||||
'allow' => true,
|
||||
'roles' => ['@'],
|
||||
],
|
||||
@ -26,6 +27,7 @@ class AccountsController extends Controller {
|
||||
public function verbs() {
|
||||
return [
|
||||
'current' => ['GET'],
|
||||
'change-password' => ['POST'],
|
||||
];
|
||||
}
|
||||
|
||||
@ -42,4 +44,21 @@ class AccountsController extends Controller {
|
||||
];
|
||||
}
|
||||
|
||||
public function actionChangePassword() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new ChangePasswordForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
if (!$model->changePassword()) {
|
||||
return [
|
||||
'success' => false,
|
||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
71
api/models/ChangePasswordForm.php
Normal file
71
api/models/ChangePasswordForm.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
|
||||
class ChangePasswordForm extends BaseApiForm {
|
||||
|
||||
public $password;
|
||||
|
||||
public $newPassword;
|
||||
|
||||
public $newRePassword;
|
||||
|
||||
/**
|
||||
* @var \common\models\Account
|
||||
*/
|
||||
private $_account;
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->_account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules() {
|
||||
return [
|
||||
['password', 'required', 'message' => 'error.password_required'],
|
||||
['newPassword', 'required', 'message' => 'error.newPassword_required'],
|
||||
['newRePassword', 'required', 'message' => 'error.newRePassword_required'],
|
||||
['password', 'validatePassword'],
|
||||
['newPassword', 'string', 'min' => 8, 'tooShort' => 'error.password_too_short'],
|
||||
['newRePassword', 'validatePasswordAndRePasswordMatch'],
|
||||
];
|
||||
}
|
||||
|
||||
public function validatePassword($attribute) {
|
||||
if (!$this->hasErrors() && !$this->_account->validatePassword($this->$attribute)) {
|
||||
$this->addError($attribute, 'error.' . $attribute . '_incorrect');
|
||||
}
|
||||
}
|
||||
|
||||
public function validatePasswordAndRePasswordMatch($attribute) {
|
||||
if (!$this->hasErrors()) {
|
||||
if ($this->newPassword !== $this->newRePassword) {
|
||||
$this->addError($attribute, 'error.newRePassword_does_not_match');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean if password was changed.
|
||||
*/
|
||||
public function changePassword() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$account = $this->_account;
|
||||
$account->setPassword($this->newPassword);
|
||||
|
||||
return $account->save();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user