2016-05-12 03:43:19 +05:30
|
|
|
<?php
|
2019-07-26 19:34:57 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
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;
|
2019-07-26 19:34:57 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2016-05-12 03:43:19 +05:30
|
|
|
use Yii;
|
|
|
|
|
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;
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
public function rules(): array {
|
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
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
public function validatePasswordAndRePasswordMatch(string $attribute): void {
|
|
|
|
if (!$this->hasErrors() && $this->newPassword !== $this->newRePassword) {
|
|
|
|
$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")
|
2016-12-23 03:52:51 +05:30
|
|
|
*/
|
2019-08-01 14:47:12 +05:30
|
|
|
public function recoverPassword(): ?AuthenticationResult {
|
2016-05-12 03:43:19 +05:30
|
|
|
if (!$this->validate()) {
|
2019-08-01 14:47:12 +05:30
|
|
|
return null;
|
2016-05-12 03:43:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
2019-08-01 14:47:12 +05:30
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
2019-07-26 19:34:57 +05:30
|
|
|
Assert::notSame($confirmModel->delete(), false, 'Unable remove activation key.');
|
2016-05-12 03:43:19 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
Assert::true($account->save(), 'Unable activate user account.');
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
$token = Yii::$app->tokensFactory->createForWebAccount($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
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
return new AuthenticationResult($token);
|
2016-05-12 03:43:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|