2016-02-27 03:52:09 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models;
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
use api\models\base\PasswordProtectedForm;
|
2016-02-27 03:52:09 +05:30
|
|
|
use common\models\Account;
|
2016-05-12 03:43:19 +05:30
|
|
|
use common\validators\PasswordValidate;
|
2016-02-27 03:52:09 +05:30
|
|
|
use Yii;
|
2016-03-21 02:03:09 +05:30
|
|
|
use yii\helpers\ArrayHelper;
|
2016-02-27 03:52:09 +05:30
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
class ChangePasswordForm extends PasswordProtectedForm {
|
2016-02-27 03:52:09 +05:30
|
|
|
|
|
|
|
public $newPassword;
|
|
|
|
|
|
|
|
public $newRePassword;
|
|
|
|
|
2016-04-25 03:00:10 +05:30
|
|
|
public $logoutAll;
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
/**
|
|
|
|
* @var \common\models\Account
|
|
|
|
*/
|
|
|
|
private $_account;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function rules() {
|
2016-03-21 02:03:09 +05:30
|
|
|
return ArrayHelper::merge(parent::rules(), [
|
2016-05-12 03:43:19 +05:30
|
|
|
[['newPassword', 'newRePassword'], 'required', 'message' => 'error.{attribute}_required'],
|
|
|
|
['newPassword', PasswordValidate::class],
|
2016-02-27 03:52:09 +05:30
|
|
|
['newRePassword', 'validatePasswordAndRePasswordMatch'],
|
2016-04-25 03:00:10 +05:30
|
|
|
['logoutAll', 'boolean'],
|
2016-03-21 02:03:09 +05:30
|
|
|
]);
|
2016-02-27 03:52:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function validatePasswordAndRePasswordMatch($attribute) {
|
2016-05-02 15:41:10 +05:30
|
|
|
if (!$this->hasErrors($attribute)) {
|
2016-02-27 03:52:09 +05:30
|
|
|
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);
|
|
|
|
|
2016-04-25 03:00:10 +05:30
|
|
|
if ($this->logoutAll) {
|
|
|
|
// TODO: реализовать процесс разлогинивания всех авторизованных устройств и дописать под это всё тесты
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
return $account->save();
|
|
|
|
}
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
protected function getAccount() {
|
|
|
|
return $this->_account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
public function __construct(Account $account, array $config = []) {
|
|
|
|
$this->_account = $account;
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
}
|