2016-03-13 04:49:00 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models;
|
|
|
|
|
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;
|
|
|
|
use common\models\Account;
|
|
|
|
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
|
|
|
|
|
|
|
// Частота повтора отправки нового письма
|
|
|
|
const REPEAT_FREQUENCY = 5 * 60;
|
|
|
|
|
|
|
|
public $email;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
|
|
|
['email', 'filter', 'filter' => 'trim'],
|
|
|
|
['email', 'required', 'message' => 'error.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-03-13 04:49:00 +05:30
|
|
|
if (!$this->hasErrors($attribute)) {
|
|
|
|
$account = $this->getAccount();
|
2016-03-13 23:54:49 +05:30
|
|
|
if ($account === null) {
|
2016-03-13 04:49:00 +05:30
|
|
|
$this->addError($attribute, "error.{$attribute}_not_found");
|
2016-03-13 23:54:49 +05:30
|
|
|
} elseif ($account->status === Account::STATUS_ACTIVE) {
|
|
|
|
$this->addError($attribute, "error.account_already_activated");
|
|
|
|
} elseif ($account->status !== Account::STATUS_REGISTERED) {
|
|
|
|
// TODO: такие аккаунты следует логировать за попытку к саботажу
|
|
|
|
$this->addError($attribute, "error.account_cannot_resend_message");
|
2016-03-13 04:49:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateExistsActivation($attribute) {
|
|
|
|
if (!$this->hasErrors($attribute)) {
|
|
|
|
if ($this->getActiveActivation() !== null) {
|
|
|
|
$this->addError($attribute, 'error.recently_sent_message');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
$activation = new EmailActivation();
|
|
|
|
$activation->account_id = $account->id;
|
|
|
|
$activation->type = EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION;
|
|
|
|
$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()
|
|
|
|
->andWhere(['email' => $this->email])
|
|
|
|
->one();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return EmailActivation|null
|
|
|
|
*/
|
|
|
|
public function getActiveActivation() {
|
|
|
|
return $this->getAccount()
|
|
|
|
->getEmailActivations()
|
|
|
|
->andWhere(['type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION])
|
|
|
|
->andWhere(['>=', 'created_at', time() - self::REPEAT_FREQUENCY])
|
|
|
|
->one();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|