2016-01-03 05:48:37 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace common\models;
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
use common\components\UserPass;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
use Yii;
|
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
use yii\behaviors\TimestampBehavior;
|
|
|
|
|
use yii\db\ActiveRecord;
|
2016-08-06 21:22:03 +05:30
|
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Поля модели:
|
|
|
|
|
* @property integer $id
|
|
|
|
|
* @property string $uuid
|
2016-01-04 21:01:14 +05:30
|
|
|
|
* @property string $username
|
2016-05-12 14:20:30 +05:30
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string $password_hash
|
|
|
|
|
* @property integer $password_hash_strategy
|
2016-05-13 14:33:00 +05:30
|
|
|
|
* @property string $lang
|
2016-05-12 14:20:30 +05:30
|
|
|
|
* @property integer $status
|
2016-08-06 21:22:03 +05:30
|
|
|
|
* @property integer $rules_agreement_version
|
2016-08-18 05:25:52 +05:30
|
|
|
|
* @property string $registration_ip
|
2017-01-21 04:24:30 +05:30
|
|
|
|
* @property string $otp_secret
|
|
|
|
|
* @property integer $is_otp_enabled
|
2016-05-12 14:20:30 +05:30
|
|
|
|
* @property integer $created_at
|
|
|
|
|
* @property integer $updated_at
|
|
|
|
|
* @property integer $password_changed_at
|
2016-01-03 05:48:37 +05:30
|
|
|
|
*
|
|
|
|
|
* Геттеры-сеттеры:
|
2016-08-04 03:37:21 +05:30
|
|
|
|
* @property string $password пароль пользователя (только для записи)
|
|
|
|
|
* @property string $profileLink ссылка на профиль на Ely без поддержки static url (только для записи)
|
2016-01-15 14:51:27 +05:30
|
|
|
|
*
|
|
|
|
|
* Отношения:
|
2016-12-29 04:31:26 +05:30
|
|
|
|
* @property EmailActivation[] $emailActivations
|
|
|
|
|
* @property OauthSession[] $oauthSessions
|
|
|
|
|
* @property UsernameHistory[] $usernameHistory
|
|
|
|
|
* @property AccountSession[] $sessions
|
|
|
|
|
* @property MinecraftAccessKey[] $minecraftAccessKeys
|
2016-01-15 14:51:27 +05:30
|
|
|
|
*
|
|
|
|
|
* Поведения:
|
|
|
|
|
* @mixin TimestampBehavior
|
2016-01-03 05:48:37 +05:30
|
|
|
|
*/
|
2016-05-12 14:20:30 +05:30
|
|
|
|
class Account extends ActiveRecord {
|
2016-06-16 03:08:43 +05:30
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
|
const STATUS_DELETED = -10;
|
2016-08-21 04:51:39 +05:30
|
|
|
|
const STATUS_BANNED = -1;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
const STATUS_REGISTERED = 0;
|
|
|
|
|
const STATUS_ACTIVE = 10;
|
|
|
|
|
|
|
|
|
|
const PASS_HASH_STRATEGY_OLD_ELY = 0;
|
|
|
|
|
const PASS_HASH_STRATEGY_YII2 = 1;
|
|
|
|
|
|
|
|
|
|
public static function tableName() {
|
|
|
|
|
return '{{%accounts}}';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
|
return [
|
2016-05-12 14:20:30 +05:30
|
|
|
|
TimestampBehavior::class,
|
2016-01-03 05:48:37 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validates password
|
|
|
|
|
*
|
|
|
|
|
* @param string $password password to validate
|
|
|
|
|
* @param integer $passwordHashStrategy
|
|
|
|
|
*
|
|
|
|
|
* @return bool if password provided is valid for current user
|
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
|
*/
|
2016-07-25 16:37:14 +05:30
|
|
|
|
public function validatePassword($password, $passwordHashStrategy = NULL) : bool {
|
2016-01-03 05:48:37 +05:30
|
|
|
|
if ($passwordHashStrategy === NULL) {
|
|
|
|
|
$passwordHashStrategy = $this->password_hash_strategy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch($passwordHashStrategy) {
|
|
|
|
|
case self::PASS_HASH_STRATEGY_OLD_ELY:
|
|
|
|
|
$hashedPass = UserPass::make($this->email, $password);
|
|
|
|
|
return $hashedPass === $this->password_hash;
|
|
|
|
|
|
|
|
|
|
case self::PASS_HASH_STRATEGY_YII2:
|
|
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new InvalidConfigException('You must set valid password_hash_strategy before you can validate password');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $password
|
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
|
*/
|
|
|
|
|
public function setPassword($password) {
|
2016-02-27 03:52:09 +05:30
|
|
|
|
$this->password_hash_strategy = self::PASS_HASH_STRATEGY_YII2;
|
|
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
|
2016-03-12 03:25:46 +05:30
|
|
|
|
$this->password_changed_at = time();
|
2016-01-03 05:48:37 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public function getEmailActivations() {
|
2016-03-13 04:49:00 +05:30
|
|
|
|
return $this->hasMany(EmailActivation::class, ['account_id' => 'id']);
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
|
public function getOauthSessions() {
|
2016-12-29 04:31:26 +05:30
|
|
|
|
return $this->hasMany(OauthSession::class, ['owner_id' => 'id'])->andWhere(['owner_type' => 'user']);
|
2016-02-14 23:20:10 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
|
public function getUsernameHistory() {
|
|
|
|
|
return $this->hasMany(UsernameHistory::class, ['account_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
|
public function getSessions() {
|
|
|
|
|
return $this->hasMany(AccountSession::class, ['account_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 04:31:26 +05:30
|
|
|
|
public function getMinecraftAccessKeys() {
|
|
|
|
|
return $this->hasMany(MinecraftAccessKey::class, ['account_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
|
/**
|
|
|
|
|
* Выполняет проверку, принадлежит ли этому нику аккаунт у Mojang
|
2016-08-06 21:22:03 +05:30
|
|
|
|
*
|
2016-04-24 00:14:10 +05:30
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-08-04 03:37:21 +05:30
|
|
|
|
public function hasMojangUsernameCollision() : bool {
|
2016-04-24 00:14:10 +05:30
|
|
|
|
return MojangUsername::find()
|
|
|
|
|
->andWhere(['username' => $this->username])
|
|
|
|
|
->exists();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 21:22:03 +05:30
|
|
|
|
/**
|
|
|
|
|
* Т.к. у нас нет инфы по static_url пользователя, то пока генерируем самый простой вариант
|
|
|
|
|
* с ссылкой на профиль по id. На Ely он всё равно редиректнется на static, а мы так или
|
|
|
|
|
* иначе обеспечим отдачу этой инфы.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-08-04 03:37:21 +05:30
|
|
|
|
public function getProfileLink() : string {
|
|
|
|
|
return 'http://ely.by/u' . $this->id;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 21:22:03 +05:30
|
|
|
|
/**
|
|
|
|
|
* При создании структуры БД все аккаунты получают null значение в это поле, однако оно
|
|
|
|
|
* обязательно для заполнения. Все мигрировавшие с Ely аккаунты будут иметь null значение,
|
|
|
|
|
* а актуальной версией будет 1 версия правил сайта (т.к. раньше их просто не было). Ну а
|
|
|
|
|
* дальше уже будем инкрементить.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isAgreedWithActualRules() : bool {
|
|
|
|
|
return $this->rules_agreement_version === LATEST_RULES_VERSION;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 05:25:52 +05:30
|
|
|
|
public function setRegistrationIp($ip) {
|
|
|
|
|
$this->registration_ip = $ip === null ? null : inet_pton($ip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getRegistrationIp() {
|
|
|
|
|
return $this->registration_ip === null ? null : inet_ntop($this->registration_ip);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
|
}
|