2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
|
|
|
namespace common\models;
|
|
|
|
|
2016-05-16 04:03:19 +05:30
|
|
|
use common\behaviors\DataBehavior;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\behaviors\EmailActivationExpirationBehavior;
|
2016-08-21 03:52:14 +05:30
|
|
|
use common\behaviors\PrimaryKeyValueBehavior;
|
2016-05-16 04:03:19 +05:30
|
|
|
use common\components\UserFriendlyRandomKey;
|
2016-05-11 01:10:06 +05:30
|
|
|
use yii\base\InvalidConfigException;
|
2016-01-15 14:51:27 +05:30
|
|
|
use yii\behaviors\TimestampBehavior;
|
2016-05-11 01:10:06 +05:30
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
use yii\helpers\ArrayHelper;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Поля модели:
|
2016-05-16 04:03:19 +05:30
|
|
|
* @property string $key
|
|
|
|
* @property integer $account_id
|
|
|
|
* @property integer $type
|
|
|
|
* @property string $_data
|
|
|
|
* @property integer $created_at
|
2016-01-15 14:51:27 +05:30
|
|
|
*
|
|
|
|
* Отношения:
|
|
|
|
* @property Account $account
|
|
|
|
*
|
|
|
|
* Поведения:
|
|
|
|
* @mixin TimestampBehavior
|
2016-05-11 01:10:06 +05:30
|
|
|
* @mixin EmailActivationExpirationBehavior
|
2016-05-16 04:03:19 +05:30
|
|
|
* @mixin DataBehavior
|
2016-01-15 14:51:27 +05:30
|
|
|
*/
|
2016-05-11 01:10:06 +05:30
|
|
|
class EmailActivation extends ActiveRecord {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
const TYPE_REGISTRATION_EMAIL_CONFIRMATION = 0;
|
2016-05-11 01:10:06 +05:30
|
|
|
const TYPE_FORGOT_PASSWORD_KEY = 1;
|
2016-05-16 04:03:19 +05:30
|
|
|
const TYPE_CURRENT_EMAIL_CONFIRMATION = 2;
|
|
|
|
const TYPE_NEW_EMAIL_CONFIRMATION = 3;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
public static function tableName() {
|
|
|
|
return '{{%email_activations}}';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'updatedAtAttribute' => false,
|
|
|
|
],
|
2016-08-21 03:52:14 +05:30
|
|
|
[
|
|
|
|
'class' => PrimaryKeyValueBehavior::class,
|
|
|
|
'value' => function() {
|
|
|
|
return UserFriendlyRandomKey::make();
|
|
|
|
},
|
|
|
|
],
|
2016-05-11 01:10:06 +05:30
|
|
|
'expirationBehavior' => [
|
|
|
|
'class' => EmailActivationExpirationBehavior::class,
|
2016-07-17 23:16:04 +05:30
|
|
|
'repeatTimeout' => 5 * 60, // 5m
|
2016-05-11 01:10:06 +05:30
|
|
|
'expirationTimeout' => -1,
|
|
|
|
],
|
2016-05-16 04:03:19 +05:30
|
|
|
'dataBehavior' => [
|
|
|
|
'class' => DataBehavior::class,
|
|
|
|
'attribute' => '_data',
|
|
|
|
],
|
2016-01-15 14:51:27 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccount() {
|
|
|
|
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public static function instantiate($row) {
|
|
|
|
$type = ArrayHelper::getValue($row, 'type');
|
|
|
|
if ($type === null) {
|
2016-07-27 04:28:06 +05:30
|
|
|
return parent::instantiate($row);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$classMap = self::getClassMap();
|
|
|
|
if (!isset($classMap[$type])) {
|
|
|
|
throw new InvalidConfigException('Unexpected type');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $classMap[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getClassMap() {
|
|
|
|
return [
|
|
|
|
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
|
2016-07-27 04:28:06 +05:30
|
|
|
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
|
|
|
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
|
|
|
|
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
|
2016-05-11 01:10:06 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|