При попытке запроса смены E-mail теперь происходит проверка, как давно был выполнен предыдущий запрос

This commit is contained in:
ErickSkrauch
2016-07-17 20:46:04 +03:00
parent e756dbacd6
commit 681996740d
11 changed files with 85 additions and 34 deletions

View File

@@ -2,6 +2,7 @@
namespace api\models\profile\ChangeEmail;
use api\models\base\PasswordProtectedForm;
use common\helpers\Error as E;
use common\models\Account;
use common\models\confirmations\CurrentEmailConfirmation;
use common\models\EmailActivation;
@@ -18,6 +19,7 @@ class InitStateForm extends PasswordProtectedForm {
public function __construct(Account $account, array $config = []) {
$this->account = $account;
$this->email = $account->email;
parent::__construct($config);
}
@@ -26,12 +28,20 @@ class InitStateForm extends PasswordProtectedForm {
}
public function rules() {
// TODO: поверить наличие уже отправленных подтверждений смены E-mail
return array_merge(parent::rules(), [
['email', 'validateFrequency'],
]);
}
public function validateFrequency($attribute) {
if (!$this->hasErrors()) {
$emailConfirmation = $this->getEmailActivation();
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
}
}
}
public function sendCurrentEmailConfirmation() : bool {
if (!$this->validate()) {
return false;
@@ -39,6 +49,7 @@ class InitStateForm extends PasswordProtectedForm {
$transaction = Yii::$app->db->beginTransaction();
try {
$this->removeOldCode();
$activation = $this->createCode();
$this->sendCode($activation);
@@ -66,6 +77,18 @@ class InitStateForm extends PasswordProtectedForm {
return $emailActivation;
}
/**
* Удаляет старый ключ активации, если он существует
*/
public function removeOldCode() {
$emailActivation = $this->getEmailActivation();
if ($emailActivation === null) {
return;
}
$emailActivation->delete();
}
public function sendCode(EmailActivation $code) {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
@@ -91,4 +114,24 @@ class InitStateForm extends PasswordProtectedForm {
}
}
/**
* Возвращает E-mail активацию, которая использовалась внутри процесса для перехода на следующий шаг.
* Метод предназначен для проверки, не слишком ли часто отправляются письма о смене E-mail.
* Проверяем тип подтверждения нового E-mail, поскольку при переходе на этот этап, активация предыдущего
* шага удаляется.
* @return EmailActivation|null
* @throws ErrorException
*/
public function getEmailActivation() {
return $this->getAccount()
->getEmailActivations()
->andWhere([
'type' => [
EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
],
])
->one();
}
}