mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Реализованы формы для шагов смены E-mail адреса, покрыты unit-тестами
У EmailActivation добавлено поле $_data и дописано поведение для работы с ним Упрощено подключение фикстур для EmailActivations
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace common\models;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
use common\behaviors\EmailActivationExpirationBehavior;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
@@ -10,10 +12,11 @@ use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* Поля модели:
|
||||
* @property string $key
|
||||
* @property integer $account_id
|
||||
* @property integer $type
|
||||
* @property integer $created_at
|
||||
* @property string $key
|
||||
* @property integer $account_id
|
||||
* @property integer $type
|
||||
* @property string $_data
|
||||
* @property integer $created_at
|
||||
*
|
||||
* Отношения:
|
||||
* @property Account $account
|
||||
@@ -21,15 +24,14 @@ use yii\helpers\ArrayHelper;
|
||||
* Поведения:
|
||||
* @mixin TimestampBehavior
|
||||
* @mixin EmailActivationExpirationBehavior
|
||||
*
|
||||
* TODO: у модели могут быть проблемы с уникальностью, т.к. key является первичным и не автоинкрементом
|
||||
* TODO: мб стоит ловить beforeCreate и именно там генерировать уникальный ключ для модели.
|
||||
* Но опять же нужно продумать, а как пробросить формат и обеспечить преемлемую уникальность.
|
||||
* @mixin DataBehavior
|
||||
*/
|
||||
class EmailActivation extends ActiveRecord {
|
||||
|
||||
const TYPE_REGISTRATION_EMAIL_CONFIRMATION = 0;
|
||||
const TYPE_FORGOT_PASSWORD_KEY = 1;
|
||||
const TYPE_CURRENT_EMAIL_CONFIRMATION = 2;
|
||||
const TYPE_NEW_EMAIL_CONFIRMATION = 3;
|
||||
|
||||
public static function tableName() {
|
||||
return '{{%email_activations}}';
|
||||
@@ -46,6 +48,10 @@ class EmailActivation extends ActiveRecord {
|
||||
'repeatTimeout' => 5 * 60,
|
||||
'expirationTimeout' => -1,
|
||||
],
|
||||
'dataBehavior' => [
|
||||
'class' => DataBehavior::class,
|
||||
'attribute' => '_data',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -74,7 +80,23 @@ class EmailActivation extends ActiveRecord {
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert) {
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->key === null) {
|
||||
do {
|
||||
$this->key = UserFriendlyRandomKey::make();
|
||||
} while (EmailActivation::find()->andWhere(['key' => $this->key])->exists());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
23
common/models/confirmations/CurrentEmailConfirmation.php
Normal file
23
common/models/confirmations/CurrentEmailConfirmation.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class CurrentEmailConfirmation extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 6 * 60 * 60,
|
||||
'expirationTimeout' => 1 * 60 * 60,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
}
|
29
common/models/confirmations/NewEmailConfirmation.php
Normal file
29
common/models/confirmations/NewEmailConfirmation.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* Поведения:
|
||||
* @mixin NewEmailConfirmationBehavior
|
||||
*/
|
||||
class NewEmailConfirmation extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 5 * 60,
|
||||
],
|
||||
'dataBehavior' => [
|
||||
'class' => NewEmailConfirmationBehavior::class,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
}
|
19
common/models/confirmations/NewEmailConfirmationBehavior.php
Normal file
19
common/models/confirmations/NewEmailConfirmationBehavior.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
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) {
|
||||
$this->setKey('newEmail', $newEmail);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user