2016-03-13 04:49:00 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\authentication;
|
2016-03-13 04:49:00 +05:30
|
|
|
|
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-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-03-13 04:49:00 +05:30
|
|
|
use common\components\UserFriendlyRandomKey;
|
|
|
|
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;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
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;
|
|
|
|
|
2016-03-13 04:49:00 +05:30
|
|
|
public function rules() {
|
|
|
|
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'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-13 23:54:49 +05:30
|
|
|
public function validateEmailForAccount($attribute) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateExistsActivation($attribute) {
|
2016-08-03 18:27:41 +05:30
|
|
|
if (!$this->hasErrors()) {
|
2016-05-11 01:10:06 +05:30
|
|
|
$activation = $this->getActivation();
|
|
|
|
if ($activation !== null && !$activation->canRepeat()) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 23:54:49 +05:30
|
|
|
public function sendRepeatMessage() {
|
2016-03-13 04:49:00 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
try {
|
2016-03-13 23:54:49 +05:30
|
|
|
EmailActivation::deleteAll([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
|
|
|
]);
|
2016-03-13 04:49:00 +05:30
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
$activation = new RegistrationConfirmation();
|
2016-03-13 04:49:00 +05:30
|
|
|
$activation->account_id = $account->id;
|
|
|
|
$activation->key = UserFriendlyRandomKey::make();
|
|
|
|
if (!$activation->save()) {
|
|
|
|
throw new ErrorException('Unable save email-activation model.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$regForm = new RegistrationForm();
|
|
|
|
$regForm->sendMail($activation, $account);
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
$transaction->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public function getAccount() {
|
|
|
|
return Account::find()
|
2016-05-11 01:10:06 +05:30
|
|
|
->andWhere(['email' => $this->email])
|
|
|
|
->one();
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return EmailActivation|null
|
|
|
|
*/
|
2016-05-11 01:10:06 +05:30
|
|
|
public function getActivation() {
|
|
|
|
if ($this->emailActivation === null) {
|
|
|
|
$this->emailActivation = $this->getAccount()
|
|
|
|
->getEmailActivations()
|
|
|
|
->andWhere(['type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION])
|
|
|
|
->one();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->emailActivation;
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|