2016-01-15 14:51:27 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace common\models;
|
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
|
use common\behaviors\EmailActivationExpirationBehavior;
|
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
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Поля модели:
|
|
|
|
|
* @property string $key
|
2016-01-21 02:44:29 +05:30
|
|
|
|
* @property integer $account_id
|
2016-01-15 14:51:27 +05:30
|
|
|
|
* @property integer $type
|
|
|
|
|
* @property integer $created_at
|
|
|
|
|
*
|
|
|
|
|
* Отношения:
|
|
|
|
|
* @property Account $account
|
|
|
|
|
*
|
|
|
|
|
* Поведения:
|
|
|
|
|
* @mixin TimestampBehavior
|
2016-05-11 01:10:06 +05:30
|
|
|
|
* @mixin EmailActivationExpirationBehavior
|
2016-01-21 02:44:29 +05:30
|
|
|
|
*
|
|
|
|
|
* TODO: у модели могут быть проблемы с уникальностью, т.к. key является первичным и не автоинкрементом
|
|
|
|
|
* TODO: мб стоит ловить beforeCreate и именно там генерировать уникальный ключ для модели.
|
|
|
|
|
* Но опять же нужно продумать, а как пробросить формат и обеспечить преемлемую уникальность.
|
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-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-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,
|
|
|
|
|
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\RecoverPassword::class,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|