mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Rework email_activation model, get rid of behaviors, use json column to store additional data
This commit is contained in:
@@ -88,10 +88,10 @@ class AuthenticationController extends Controller {
|
||||
];
|
||||
|
||||
if (ArrayHelper::getValue($data['errors'], 'login') === E::RECENTLY_SENT_MESSAGE) {
|
||||
/** @var \common\models\confirmations\ForgotPassword $emailActivation */
|
||||
$emailActivation = $model->getEmailActivation();
|
||||
$data['data'] = [
|
||||
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
||||
'repeatFrequency' => $emailActivation->repeatTimeout,
|
||||
'canRepeatIn' => $emailActivation->canResendAt()->getTimestamp(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -102,8 +102,7 @@ class AuthenticationController extends Controller {
|
||||
$response = [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
||||
'repeatFrequency' => $emailActivation->repeatTimeout,
|
||||
'canRepeatIn' => $emailActivation->canResendAt()->getTimestamp(),
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@@ -62,10 +62,10 @@ class SignupController extends Controller {
|
||||
];
|
||||
|
||||
if (ArrayHelper::getValue($response['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
||||
/** @var \common\models\confirmations\RegistrationConfirmation $activation */
|
||||
$activation = $model->getActivation();
|
||||
$response['data'] = [
|
||||
'canRepeatIn' => $activation->canRepeatIn(),
|
||||
'repeatFrequency' => $activation->repeatTimeout,
|
||||
'canRepeatIn' => $activation->canResendAt(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ class ForgotPasswordForm extends ApiForm {
|
||||
public function validateFrequency(string $attribute): void {
|
||||
if (!$this->hasErrors()) {
|
||||
$emailConfirmation = $this->getEmailActivation();
|
||||
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
|
||||
if ($emailConfirmation !== null && !$emailConfirmation->canResend()) {
|
||||
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ class ForgotPasswordForm extends ApiForm {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getEmailActivation(): ?EmailActivation {
|
||||
public function getEmailActivation(): ?ForgotPassword {
|
||||
$account = $this->getAccount();
|
||||
if ($account === null) {
|
||||
return null;
|
||||
|
||||
@@ -50,7 +50,7 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
public function validateExistsActivation(string $attribute): void {
|
||||
if (!$this->hasErrors()) {
|
||||
$activation = $this->getActivation();
|
||||
if ($activation !== null && !$activation->canRepeat()) {
|
||||
if ($activation !== null && !$activation->canResend()) {
|
||||
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
||||
}
|
||||
}
|
||||
@@ -94,7 +94,7 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
->one();
|
||||
}
|
||||
|
||||
public function getActivation(): ?EmailActivation {
|
||||
public function getActivation(): ?RegistrationConfirmation {
|
||||
return $this->getAccount()
|
||||
->getEmailActivations()
|
||||
->withType(EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION)
|
||||
|
||||
@@ -17,11 +17,11 @@ class EmailVerificationAction extends BaseAccountAction {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @var \common\models\EmailActivation $emailActivation */
|
||||
$emailActivation = $model->getEmailActivation();
|
||||
|
||||
return [
|
||||
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
||||
'repeatFrequency' => $emailActivation->repeatTimeout,
|
||||
'canRepeatIn' => $emailActivation->canResendAt()->getTimestamp(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ class SendEmailVerificationForm extends AccountActionForm {
|
||||
public function validateFrequency(string $attribute): void {
|
||||
if (!$this->hasErrors()) {
|
||||
$emailConfirmation = $this->getEmailActivation();
|
||||
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
|
||||
if ($emailConfirmation !== null && !$emailConfirmation->canResend()) {
|
||||
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,8 @@ class SendEmailVerificationForm extends AccountActionForm {
|
||||
* The method is designed to check if the E-mail change messages are sent too often.
|
||||
* Including checking for the confirmation of the new E-mail type, because when you go to this step,
|
||||
* the activation of the previous step is removed.
|
||||
*
|
||||
* @return CurrentEmailConfirmation|\common\models\confirmations\NewEmailConfirmation
|
||||
*/
|
||||
public function getEmailActivation(): ?EmailActivation {
|
||||
return $this->getAccount()
|
||||
|
||||
@@ -57,7 +57,6 @@ class ForgotPasswordCest {
|
||||
],
|
||||
]);
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.canRepeatIn');
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.repeatFrequency');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +67,6 @@ class ForgotPasswordCest {
|
||||
'success' => true,
|
||||
]);
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.canRepeatIn');
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.repeatFrequency');
|
||||
if ($expectEmailMask) {
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.emailMask');
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ class RepeatAccountActivationCest {
|
||||
],
|
||||
]);
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.canRepeatIn');
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.repeatFrequency');
|
||||
}
|
||||
|
||||
public function testSuccess(FunctionalTester $I) {
|
||||
|
||||
@@ -39,7 +39,6 @@ class ChangeEmailInitializeCest {
|
||||
],
|
||||
]);
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.canRepeatIn');
|
||||
$I->canSeeResponseJsonMatchesJsonPath('$.data.repeatFrequency');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use api\models\authentication\ForgotPasswordForm;
|
||||
use api\tests\unit\TestCase;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\models\EmailActivation;
|
||||
use common\tasks\SendPasswordRecoveryEmail;
|
||||
use common\tests\fixtures\AccountFixture;
|
||||
@@ -126,7 +127,7 @@ class ForgotPasswordFormTest extends TestCase {
|
||||
return new class($params) extends ForgotPasswordForm {
|
||||
public $key;
|
||||
|
||||
public function getEmailActivation(): ?EmailActivation {
|
||||
public function getEmailActivation(): ?ForgotPassword {
|
||||
return EmailActivation::findOne(['key' => $this->key]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@ use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use api\models\authentication\RepeatAccountActivationForm;
|
||||
use api\tests\unit\TestCase;
|
||||
use Codeception\Specify;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use common\tasks\SendRegistrationEmail;
|
||||
use common\tests\fixtures\AccountFixture;
|
||||
@@ -100,7 +101,7 @@ class RepeatAccountActivationFormTest extends TestCase {
|
||||
return new class($params) extends RepeatAccountActivationForm {
|
||||
public $emailKey;
|
||||
|
||||
public function getActivation(): ?EmailActivation {
|
||||
public function getActivation(): ?RegistrationConfirmation {
|
||||
return EmailActivation::findOne($this->emailKey);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace api\tests\unit\modules\accounts\models;
|
||||
|
||||
use api\modules\accounts\models\ChangeEmailForm;
|
||||
@@ -20,16 +22,17 @@ class ChangeEmailFormTest extends TestCase {
|
||||
public function testChangeEmail() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->getAccountId());
|
||||
/** @var EmailActivation $newEmailConfirmationFixture */
|
||||
$newEmailConfirmationFixture = $this->tester->grabFixture('emailActivations', 'newEmailConfirmation');
|
||||
$model = new ChangeEmailForm($account, [
|
||||
'key' => $newEmailConfirmationFixture['key'],
|
||||
'key' => $newEmailConfirmationFixture->key,
|
||||
]);
|
||||
$this->assertTrue($model->performAction());
|
||||
$this->assertNull(EmailActivation::findOne([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||
]));
|
||||
$data = unserialize($newEmailConfirmationFixture['_data']);
|
||||
$data = $newEmailConfirmationFixture->data;
|
||||
$this->assertSame($data['newEmail'], $account->email);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ class EmailActivationKeyValidatorTest extends TestCase {
|
||||
->getMock();
|
||||
|
||||
$expiredActivation = new ForgotPassword();
|
||||
$expiredActivation->created_at = time() - $expiredActivation->expirationTimeout - 10;
|
||||
$expiredActivation->created_at = time() - 60 * 60 - 10;
|
||||
|
||||
$validActivation = new EmailActivation();
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ class EmailActivationKeyValidator extends Validator {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($activation->isExpired()) {
|
||||
if ($activation->isStale()) {
|
||||
$this->addError($model, $attribute, $this->expired);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user