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:
@@ -3,10 +3,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
use common\behaviors\EmailActivationExpirationBehavior;
|
||||
use common\behaviors\PrimaryKeyValueBehavior;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveRecord;
|
||||
@@ -14,19 +14,17 @@ use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* Fields:
|
||||
* @property string $key
|
||||
* @property integer $account_id
|
||||
* @property integer $type
|
||||
* @property string $_data
|
||||
* @property integer $created_at
|
||||
* @property string $key
|
||||
* @property int $account_id
|
||||
* @property int $type
|
||||
* @property array|null $data
|
||||
* @property int $created_at
|
||||
*
|
||||
* Relations:
|
||||
* @property Account $account
|
||||
*
|
||||
* Behaviors:
|
||||
* @mixin TimestampBehavior
|
||||
* @mixin EmailActivationExpirationBehavior
|
||||
* @mixin DataBehavior
|
||||
*/
|
||||
class EmailActivation extends ActiveRecord {
|
||||
|
||||
@@ -39,41 +37,15 @@ class EmailActivation extends ActiveRecord {
|
||||
return 'email_activations';
|
||||
}
|
||||
|
||||
public static function find(): EmailActivationQuery {
|
||||
return new EmailActivationQuery(static::class);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
public static function getClassMap(): array {
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'updatedAtAttribute' => false,
|
||||
],
|
||||
[
|
||||
'class' => PrimaryKeyValueBehavior::class,
|
||||
'value' => function() {
|
||||
return UserFriendlyRandomKey::make();
|
||||
},
|
||||
],
|
||||
'expirationBehavior' => [
|
||||
'class' => EmailActivationExpirationBehavior::class,
|
||||
'repeatTimeout' => 5 * 60, // 5m
|
||||
'expirationTimeout' => -1,
|
||||
],
|
||||
'dataBehavior' => [
|
||||
'class' => DataBehavior::class,
|
||||
'attribute' => '_data',
|
||||
],
|
||||
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
|
||||
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
||||
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
|
||||
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function getAccount(): AccountQuery {
|
||||
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function instantiate($row) {
|
||||
$type = ArrayHelper::getValue($row, 'type');
|
||||
if ($type === null) {
|
||||
@@ -88,13 +60,79 @@ class EmailActivation extends ActiveRecord {
|
||||
return new $classMap[$type]();
|
||||
}
|
||||
|
||||
public static function getClassMap(): array {
|
||||
public static function find(): EmailActivationQuery {
|
||||
return new EmailActivationQuery(static::class);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return [
|
||||
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
|
||||
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
||||
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
|
||||
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'updatedAtAttribute' => false,
|
||||
],
|
||||
[
|
||||
'class' => PrimaryKeyValueBehavior::class,
|
||||
'value' => function(): string {
|
||||
return UserFriendlyRandomKey::make();
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getAccount(): AccountQuery {
|
||||
/** @noinspection PhpIncompatibleReturnTypeInspection */
|
||||
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
||||
}
|
||||
|
||||
public function canResend(): bool {
|
||||
$timeout = $this->getResendTimeout();
|
||||
if ($timeout === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->compareTime($timeout);
|
||||
}
|
||||
|
||||
public function canResendAt(): DateTimeImmutable {
|
||||
return $this->calculateTime($this->getResendTimeout() ?? new DateInterval('PT0S'));
|
||||
}
|
||||
|
||||
public function isStale(): bool {
|
||||
$duration = $this->getExpireDuration();
|
||||
if ($duration === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->compareTime($duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* After which time the message for this action type can be resended.
|
||||
* When null returned the message can be sent immediately.
|
||||
*
|
||||
* @return DateInterval|null
|
||||
*/
|
||||
protected function getResendTimeout(): ?DateInterval {
|
||||
return new DateInterval('PT5M');
|
||||
}
|
||||
|
||||
/**
|
||||
* How long the activation code should be valid.
|
||||
* When null returned the code is never expires
|
||||
*
|
||||
* @return DateInterval|null
|
||||
*/
|
||||
protected function getExpireDuration(): ?DateInterval {
|
||||
return null;
|
||||
}
|
||||
|
||||
private function compareTime(DateInterval $value): bool {
|
||||
return (new DateTimeImmutable()) > $this->calculateTime($value);
|
||||
}
|
||||
|
||||
private function calculateTime(DateInterval $interval): DateTimeImmutable {
|
||||
/** @noinspection PhpUnhandledExceptionInspection */
|
||||
return (new DateTimeImmutable('@' . $this->created_at))->add($interval);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use DateInterval;
|
||||
|
||||
class CurrentEmailConfirmation extends EmailActivation {
|
||||
|
||||
@@ -13,18 +13,17 @@ class CurrentEmailConfirmation extends EmailActivation {
|
||||
return parent::find()->withType(EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 6 * 60 * 60, // 6h
|
||||
'expirationTimeout' => 1 * 60 * 60, // 1h
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
protected function getResendTimeout(): ?DateInterval {
|
||||
return new DateInterval('PT6H');
|
||||
}
|
||||
|
||||
protected function getExpireDuration(): ?DateInterval {
|
||||
return new DateInterval('PT1H');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use DateInterval;
|
||||
|
||||
class ForgotPassword extends EmailActivation {
|
||||
|
||||
@@ -13,18 +13,17 @@ class ForgotPassword extends EmailActivation {
|
||||
return parent::find()->withType(EmailActivation::TYPE_FORGOT_PASSWORD_KEY);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 30 * 60,
|
||||
'expirationTimeout' => 1 * 60 * 60,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_FORGOT_PASSWORD_KEY;
|
||||
}
|
||||
|
||||
protected function getResendTimeout(): ?DateInterval {
|
||||
return new DateInterval('PT30M');
|
||||
}
|
||||
|
||||
protected function getExpireDuration(): ?DateInterval {
|
||||
return new DateInterval('PT1H');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,30 +7,25 @@ use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* Behaviors:
|
||||
* @mixin NewEmailConfirmationBehavior
|
||||
*/
|
||||
class NewEmailConfirmation extends EmailActivation {
|
||||
|
||||
public static function find(): EmailActivationQuery {
|
||||
return parent::find()->withType(EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 5 * 60,
|
||||
],
|
||||
'dataBehavior' => [
|
||||
'class' => NewEmailConfirmationBehavior::class,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
public function getNewEmail(): string {
|
||||
return $this->data['newEmail'];
|
||||
}
|
||||
|
||||
public function setNewEmail(string $newEmail): void {
|
||||
$this->data = ArrayHelper::merge($this->data ?? [], [
|
||||
'newEmail' => $newEmail,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
|
||||
/**
|
||||
* @property string $newEmail
|
||||
*/
|
||||
class NewEmailConfirmationBehavior extends DataBehavior {
|
||||
|
||||
public function getNewEmail(): string {
|
||||
return $this->getKey('newEmail');
|
||||
}
|
||||
|
||||
public function setNewEmail(string $newEmail): void {
|
||||
$this->setKey('newEmail', $newEmail);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user