2016-03-13 04:49:00 +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-03-13 04:49:00 +05:30
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
use api\aop\annotations\CollectModelMetrics;
|
2016-08-03 18:27:41 +05:30
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
2016-03-20 05:03:49 +05:30
|
|
|
use api\models\base\ApiForm;
|
2016-03-13 04:49:00 +05:30
|
|
|
use common\components\UserFriendlyRandomKey;
|
2018-04-18 02:17:25 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-03-13 04:49:00 +05:30
|
|
|
use common\models\Account;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\confirmations\RegistrationConfirmation;
|
2016-03-13 04:49:00 +05:30
|
|
|
use common\models\EmailActivation;
|
2017-11-26 07:14:41 +05:30
|
|
|
use common\tasks\SendRegistrationEmail;
|
2019-12-13 23:18:13 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2016-03-13 04:49:00 +05:30
|
|
|
use Yii;
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
class RepeatAccountActivationForm extends ApiForm {
|
2016-03-13 04:49:00 +05:30
|
|
|
|
2016-08-03 18:27:41 +05:30
|
|
|
public $captcha;
|
|
|
|
|
2016-03-13 04:49:00 +05:30
|
|
|
public $email;
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
private $emailActivation;
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function rules(): array {
|
2016-03-13 04:49:00 +05:30
|
|
|
return [
|
2016-08-03 18:27:41 +05:30
|
|
|
['captcha', ReCaptchaValidator::class],
|
2016-03-13 04:49:00 +05:30
|
|
|
['email', 'filter', 'filter' => 'trim'],
|
2016-06-17 02:02:23 +05:30
|
|
|
['email', 'required', 'message' => E::EMAIL_REQUIRED],
|
2016-03-13 23:54:49 +05:30
|
|
|
['email', 'validateEmailForAccount'],
|
2016-03-13 04:49:00 +05:30
|
|
|
['email', 'validateExistsActivation'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function validateEmailForAccount(string $attribute): void {
|
2016-08-03 18:27:41 +05:30
|
|
|
if (!$this->hasErrors()) {
|
2016-03-13 04:49:00 +05:30
|
|
|
$account = $this->getAccount();
|
2016-03-13 23:54:49 +05:30
|
|
|
if ($account === null) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::EMAIL_NOT_FOUND);
|
2016-03-13 23:54:49 +05:30
|
|
|
} elseif ($account->status === Account::STATUS_ACTIVE) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::ACCOUNT_ALREADY_ACTIVATED);
|
2016-03-13 23:54:49 +05:30
|
|
|
} elseif ($account->status !== Account::STATUS_REGISTERED) {
|
|
|
|
// TODO: такие аккаунты следует логировать за попытку к саботажу
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::ACCOUNT_CANNOT_RESEND_MESSAGE);
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function validateExistsActivation(string $attribute): void {
|
2016-08-03 18:27:41 +05:30
|
|
|
if (!$this->hasErrors()) {
|
2016-05-11 01:10:06 +05:30
|
|
|
$activation = $this->getActivation();
|
2019-12-21 03:53:58 +05:30
|
|
|
if ($activation !== null && !$activation->canResend()) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
/**
|
|
|
|
* @CollectModelMetrics(prefix="signup.repeatEmail")
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-12-14 00:57:13 +05:30
|
|
|
public function sendRepeatMessage(): bool {
|
2016-03-13 04:49:00 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
/** @var Account $account */
|
2016-03-13 04:49:00 +05:30
|
|
|
$account = $this->getAccount();
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
2017-11-26 07:14:41 +05:30
|
|
|
EmailActivation::deleteAll([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
|
|
|
]);
|
2016-03-13 04:49:00 +05:30
|
|
|
|
2017-11-26 07:14:41 +05:30
|
|
|
$activation = new RegistrationConfirmation();
|
|
|
|
$activation->account_id = $account->id;
|
|
|
|
$activation->key = UserFriendlyRandomKey::make();
|
2019-12-13 23:18:13 +05:30
|
|
|
Assert::true($activation->save(), 'Unable save email-activation model.');
|
2016-03-13 04:49:00 +05:30
|
|
|
|
2017-11-27 04:59:15 +05:30
|
|
|
$this->emailActivation = $activation;
|
|
|
|
|
2017-11-26 07:14:41 +05:30
|
|
|
Yii::$app->queue->push(SendRegistrationEmail::createFromConfirmation($activation));
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
|
2016-03-13 04:49:00 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function getAccount(): ?Account {
|
2016-03-13 04:49:00 +05:30
|
|
|
return Account::find()
|
2016-05-11 01:10:06 +05:30
|
|
|
->andWhere(['email' => $this->email])
|
|
|
|
->one();
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
public function getActivation(): ?RegistrationConfirmation {
|
2019-12-14 00:57:13 +05:30
|
|
|
return $this->getAccount()
|
|
|
|
->getEmailActivations()
|
|
|
|
->withType(EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION)
|
|
|
|
->one();
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|