2016-02-27 03:52:09 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models;
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
use api\models\base\ApiForm;
|
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;
|
|
|
|
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-02-27 03:52:09 +05:30
|
|
|
['newPassword', 'required', 'message' => 'error.newPassword_required'],
|
|
|
|
['newRePassword', 'required', 'message' => 'error.newRePassword_required'],
|
|
|
|
['newPassword', 'string', 'min' => 8, 'tooShort' => 'error.password_too_short'],
|
|
|
|
['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) {
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
}
|