2016-05-12 03:43:19 +05:30
|
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
|
namespace api\models\authentication;
|
2016-05-12 03:43:19 +05:30
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
|
use api\models\AccountIdentity;
|
2016-05-12 03:43:19 +05:30
|
|
|
|
use api\models\base\KeyConfirmationForm;
|
2016-06-17 02:02:23 +05:30
|
|
|
|
use common\helpers\Error as E;
|
2016-05-12 03:43:19 +05:30
|
|
|
|
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(), [
|
2016-06-17 02:02:23 +05:30
|
|
|
|
['newPassword', 'required', 'message' => E::NEW_PASSWORD_REQUIRED],
|
|
|
|
|
['newRePassword', 'required', 'message' => E::NEW_RE_PASSWORD_REQUIRED],
|
2016-05-12 03:43:19 +05:30
|
|
|
|
['newPassword', PasswordValidate::class],
|
|
|
|
|
['newRePassword', 'validatePasswordAndRePasswordMatch'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validatePasswordAndRePasswordMatch($attribute) {
|
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
|
if ($this->newPassword !== $this->newRePassword) {
|
2016-06-17 02:02:23 +05:30
|
|
|
|
$this->addError($attribute, E::NEW_RE_PASSWORD_DOES_NOT_MATCH);
|
2016-05-12 03:43:19 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
|
// TODO: ещё было бы неплохо уведомить пользователя о том, что его пароль изменился
|
2016-05-12 03:43:19 +05:30
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
|
/** @var \api\components\User\Component $component */
|
|
|
|
|
$component = Yii::$app->user;
|
|
|
|
|
|
|
|
|
|
return $component->login(new AccountIdentity($account->attributes), false);
|
2016-05-12 03:43:19 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|