2016-05-11 01:10:06 +05:30
|
|
|
<?php
|
2019-12-14 00:57:13 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\authentication;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
use api\aop\annotations\CollectModelMetrics;
|
2017-04-18 21:38:11 +05:30
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
2016-05-11 01:10:06 +05:30
|
|
|
use api\models\base\ApiForm;
|
|
|
|
use common\components\UserFriendlyRandomKey;
|
2018-04-18 02:17:25 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\Account;
|
2016-05-11 01:58:04 +05:30
|
|
|
use common\models\confirmations\ForgotPassword;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\EmailActivation;
|
2017-11-26 07:14:41 +05:30
|
|
|
use common\tasks\SendPasswordRecoveryEmail;
|
|
|
|
use Yii;
|
2016-05-11 01:10:06 +05:30
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
|
|
|
class ForgotPasswordForm extends ApiForm {
|
|
|
|
|
2017-04-18 21:38:11 +05:30
|
|
|
public $captcha;
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
public $login;
|
2017-04-18 21:38:11 +05:30
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function rules(): array {
|
2016-05-11 01:10:06 +05:30
|
|
|
return [
|
2017-04-18 21:38:11 +05:30
|
|
|
['captcha', ReCaptchaValidator::class],
|
2016-06-17 02:02:23 +05:30
|
|
|
['login', 'required', 'message' => E::LOGIN_REQUIRED],
|
2016-05-11 01:10:06 +05:30
|
|
|
['login', 'validateLogin'],
|
|
|
|
['login', 'validateActivity'],
|
|
|
|
['login', 'validateFrequency'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function validateLogin(string $attribute): void {
|
2016-05-11 01:10:06 +05:30
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
if ($this->getAccount() === null) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::LOGIN_NOT_EXIST);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function validateActivity(string $attribute): void {
|
2016-05-11 01:10:06 +05:30
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
$account = $this->getAccount();
|
|
|
|
if ($account->status !== Account::STATUS_ACTIVE) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::ACCOUNT_NOT_ACTIVATED);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function validateFrequency(string $attribute): void {
|
2016-05-11 01:10:06 +05:30
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
$emailConfirmation = $this->getEmailActivation();
|
2019-12-21 03:53:58 +05:30
|
|
|
if ($emailConfirmation !== null && !$emailConfirmation->canResend()) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function getAccount(): ?Account {
|
|
|
|
return Account::find()->andWhereLogin($this->login)->one();
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
/**
|
|
|
|
* @CollectModelMetrics(prefix="authentication.forgotPassword")
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-12-14 00:57:13 +05:30
|
|
|
public function forgotPassword(): bool {
|
2016-05-11 01:10:06 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
$emailActivation = $this->getEmailActivation();
|
|
|
|
if ($emailActivation === null) {
|
2016-05-11 01:58:04 +05:30
|
|
|
$emailActivation = new ForgotPassword();
|
2016-05-11 01:10:06 +05:30
|
|
|
$emailActivation->account_id = $account->id;
|
|
|
|
} else {
|
|
|
|
$emailActivation->created_at = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
$emailActivation->key = UserFriendlyRandomKey::make();
|
|
|
|
if (!$emailActivation->save()) {
|
|
|
|
throw new ErrorException('Cannot create email activation for forgot password form');
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:14:41 +05:30
|
|
|
Yii::$app->queue->push(SendPasswordRecoveryEmail::createFromConfirmation($emailActivation));
|
2016-05-11 01:10:06 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
public function getEmailActivation(): ?ForgotPassword {
|
2016-05-11 01:10:06 +05:30
|
|
|
$account = $this->getAccount();
|
|
|
|
if ($account === null) {
|
2019-12-14 00:57:13 +05:30
|
|
|
return null;
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
return $account->getEmailActivations()->withType(EmailActivation::TYPE_FORGOT_PASSWORD_KEY)->one();
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|