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-05-16 04:03:19 +05:30
|
|
|
use common\components\UserFriendlyRandomKey;
|
2016-01-15 14:51:27 +05:30
|
|
|
use Yii;
|
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-05-11 01:10:06 +05:30
|
|
|
'expirationBehavior' => [
|
|
|
|
'class' => EmailActivationExpirationBehavior::class,
|
|
|
|
'repeatTimeout' => 5 * 60,
|
|
|
|
'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) {
|
|
|
|
return new static;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-05-11 01:58:04 +05:30
|
|
|
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
2016-05-16 04:03:19 +05:30
|
|
|
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-05-16 04:03:19 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|