2016-02-27 03:52:09 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\profile;
|
2016-02-27 03:52:09 +05:30
|
|
|
|
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-06-05 19:31:35 +05:30
|
|
|
use yii\base\ErrorException;
|
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;
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
public function __construct(Account $account, array $config = []) {
|
|
|
|
$this->_account = $account;
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 19:31:35 +05:30
|
|
|
* @return boolean
|
2016-02-27 03:52:09 +05:30
|
|
|
*/
|
|
|
|
public function changePassword() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
2016-02-27 03:52:09 +05:30
|
|
|
$account = $this->_account;
|
|
|
|
$account->setPassword($this->newPassword);
|
|
|
|
|
2016-04-25 03:00:10 +05:30
|
|
|
if ($this->logoutAll) {
|
2016-06-05 19:31:35 +05:30
|
|
|
/** @var \api\components\User\Component $userComponent */
|
|
|
|
$userComponent = Yii::$app->user;
|
|
|
|
$sessions = $account->sessions;
|
|
|
|
$activeSession = $userComponent->getActiveSession();
|
|
|
|
foreach ($sessions as $session) {
|
|
|
|
if (!$activeSession || $activeSession->id !== $session->id) {
|
|
|
|
$session->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$account->save()) {
|
|
|
|
throw new ErrorException('Cannot save user model');
|
2016-04-25 03:00:10 +05:30
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
$transaction->commit();
|
|
|
|
|
|
|
|
return true;
|
2016-02-27 03:52:09 +05:30
|
|
|
}
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
protected function getAccount() {
|
|
|
|
return $this->_account;
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
}
|