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
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
use api\aop\annotations\CollectModelMetrics;
|
2016-12-23 03:52:51 +05:30
|
|
|
use api\models\base\ApiForm;
|
|
|
|
use api\validators\EmailActivationKeyValidator;
|
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;
|
2016-11-02 02:29:43 +05:30
|
|
|
use common\validators\PasswordValidator;
|
2016-05-12 03:43:19 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
class RecoverPasswordForm extends ApiForm {
|
|
|
|
|
|
|
|
public $key;
|
2016-05-12 03:43:19 +05:30
|
|
|
|
|
|
|
public $newPassword;
|
|
|
|
|
|
|
|
public $newRePassword;
|
|
|
|
|
|
|
|
public function rules() {
|
2016-12-23 03:52:51 +05:30
|
|
|
return [
|
|
|
|
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY],
|
2016-06-17 02:02:23 +05:30
|
|
|
['newPassword', 'required', 'message' => E::NEW_PASSWORD_REQUIRED],
|
|
|
|
['newRePassword', 'required', 'message' => E::NEW_RE_PASSWORD_REQUIRED],
|
2016-11-02 02:29:43 +05:30
|
|
|
['newPassword', PasswordValidator::class],
|
2016-05-12 03:43:19 +05:30
|
|
|
['newRePassword', 'validatePasswordAndRePasswordMatch'],
|
2016-12-23 03:52:51 +05:30
|
|
|
];
|
2016-05-12 03:43:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
/**
|
2017-11-19 21:02:51 +05:30
|
|
|
* @CollectModelMetrics(prefix="authentication.recoverPassword")
|
2017-09-19 22:36:16 +05:30
|
|
|
* @return \api\components\User\AuthenticationResult|bool
|
2016-12-23 03:52:51 +05:30
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
2016-05-12 03:43:19 +05:30
|
|
|
public function recoverPassword() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
/** @var \common\models\confirmations\ForgotPassword $confirmModel */
|
|
|
|
$confirmModel = $this->key;
|
|
|
|
$account = $confirmModel->account;
|
|
|
|
$account->password = $this->newPassword;
|
|
|
|
if (!$confirmModel->delete()) {
|
|
|
|
throw new ErrorException('Unable remove activation key.');
|
2016-05-12 03:43:19 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
if (!$account->save(false)) {
|
|
|
|
throw new ErrorException('Unable activate user account.');
|
|
|
|
}
|
2016-05-12 03:43:19 +05:30
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
$transaction->commit();
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
return Yii::$app->user->createJwtAuthenticationToken($account, false);
|
2016-05-12 03:43:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|