2016-01-03 03:18:37 +03:00
|
|
|
|
<?php
|
|
|
|
|
namespace common\models;
|
|
|
|
|
|
2016-01-15 12:21:27 +03:00
|
|
|
|
use common\components\UserPass;
|
2016-01-03 03:18:37 +03:00
|
|
|
|
use Yii;
|
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
use yii\behaviors\TimestampBehavior;
|
|
|
|
|
use yii\db\ActiveRecord;
|
2016-08-06 18:52:03 +03:00
|
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-01-03 03:18:37 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Поля модели:
|
|
|
|
|
* @property integer $id
|
|
|
|
|
* @property string $uuid
|
2016-01-04 18:31:14 +03:00
|
|
|
|
* @property string $username
|
2016-05-12 11:50:30 +03:00
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string $password_hash
|
|
|
|
|
* @property integer $password_hash_strategy
|
2016-05-13 12:03:00 +03:00
|
|
|
|
* @property string $lang
|
2016-05-12 11:50:30 +03:00
|
|
|
|
* @property integer $status
|
2016-08-06 18:52:03 +03:00
|
|
|
|
* @property integer $rules_agreement_version
|
2016-08-18 02:55:52 +03:00
|
|
|
|
* @property string $registration_ip
|
2017-01-21 01:54:30 +03:00
|
|
|
|
* @property string $otp_secret
|
|
|
|
|
* @property integer $is_otp_enabled
|
2016-05-12 11:50:30 +03:00
|
|
|
|
* @property integer $created_at
|
|
|
|
|
* @property integer $updated_at
|
|
|
|
|
* @property integer $password_changed_at
|
2016-01-03 03:18:37 +03:00
|
|
|
|
*
|
|
|
|
|
* Геттеры-сеттеры:
|
2016-08-04 01:07:21 +03:00
|
|
|
|
* @property string $password пароль пользователя (только для записи)
|
|
|
|
|
* @property string $profileLink ссылка на профиль на Ely без поддержки static url (только для записи)
|
2016-01-15 12:21:27 +03:00
|
|
|
|
*
|
|
|
|
|
* Отношения:
|
2016-12-29 02:01:26 +03:00
|
|
|
|
* @property EmailActivation[] $emailActivations
|
|
|
|
|
* @property OauthSession[] $oauthSessions
|
|
|
|
|
* @property UsernameHistory[] $usernameHistory
|
|
|
|
|
* @property AccountSession[] $sessions
|
|
|
|
|
* @property MinecraftAccessKey[] $minecraftAccessKeys
|
2016-01-15 12:21:27 +03:00
|
|
|
|
*
|
|
|
|
|
* Поведения:
|
|
|
|
|
* @mixin TimestampBehavior
|
2016-01-03 03:18:37 +03:00
|
|
|
|
*/
|
2016-05-12 11:50:30 +03:00
|
|
|
|
class Account extends ActiveRecord {
|
2016-06-16 00:38:43 +03:00
|
|
|
|
|
2016-01-03 03:18:37 +03:00
|
|
|
|
const STATUS_DELETED = -10;
|
2016-08-21 02:21:39 +03:00
|
|
|
|
const STATUS_BANNED = -1;
|
2016-01-03 03:18:37 +03:00
|
|
|
|
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 11:50:30 +03:00
|
|
|
|
TimestampBehavior::class,
|
2016-01-03 03:18:37 +03:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 14:07:14 +03:00
|
|
|
|
public function validatePassword($password, $passwordHashStrategy = NULL) : bool {
|
2016-01-03 03:18:37 +03:00
|
|
|
|
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 01:22:09 +03:00
|
|
|
|
$this->password_hash_strategy = self::PASS_HASH_STRATEGY_YII2;
|
|
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
|
2016-03-12 00:55:46 +03:00
|
|
|
|
$this->password_changed_at = time();
|
2016-01-03 03:18:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 12:21:27 +03:00
|
|
|
|
public function getEmailActivations() {
|
2016-03-13 02:19:00 +03:00
|
|
|
|
return $this->hasMany(EmailActivation::class, ['account_id' => 'id']);
|
2016-01-15 12:21:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 02:44:17 +03:00
|
|
|
|
public function getOauthSessions() {
|
2016-12-29 02:01:26 +03:00
|
|
|
|
return $this->hasMany(OauthSession::class, ['owner_id' => 'id'])->andWhere(['owner_type' => 'user']);
|
2016-02-14 20:50:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 21:44:10 +03:00
|
|
|
|
public function getUsernameHistory() {
|
|
|
|
|
return $this->hasMany(UsernameHistory::class, ['account_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 02:44:17 +03:00
|
|
|
|
public function getSessions() {
|
|
|
|
|
return $this->hasMany(AccountSession::class, ['account_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 02:01:26 +03:00
|
|
|
|
public function getMinecraftAccessKeys() {
|
|
|
|
|
return $this->hasMany(MinecraftAccessKey::class, ['account_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 21:44:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Выполняет проверку, принадлежит ли этому нику аккаунт у Mojang
|
2016-08-06 18:52:03 +03:00
|
|
|
|
*
|
2016-04-23 21:44:10 +03:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-08-04 01:07:21 +03:00
|
|
|
|
public function hasMojangUsernameCollision() : bool {
|
2016-04-23 21:44:10 +03:00
|
|
|
|
return MojangUsername::find()
|
|
|
|
|
->andWhere(['username' => $this->username])
|
|
|
|
|
->exists();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 18:52:03 +03:00
|
|
|
|
/**
|
|
|
|
|
* Т.к. у нас нет инфы по static_url пользователя, то пока генерируем самый простой вариант
|
|
|
|
|
* с ссылкой на профиль по id. На Ely он всё равно редиректнется на static, а мы так или
|
|
|
|
|
* иначе обеспечим отдачу этой инфы.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-08-04 01:07:21 +03:00
|
|
|
|
public function getProfileLink() : string {
|
|
|
|
|
return 'http://ely.by/u' . $this->id;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 18:52:03 +03:00
|
|
|
|
/**
|
|
|
|
|
* При создании структуры БД все аккаунты получают null значение в это поле, однако оно
|
|
|
|
|
* обязательно для заполнения. Все мигрировавшие с Ely аккаунты будут иметь null значение,
|
|
|
|
|
* а актуальной версией будет 1 версия правил сайта (т.к. раньше их просто не было). Ну а
|
|
|
|
|
* дальше уже будем инкрементить.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isAgreedWithActualRules() : bool {
|
|
|
|
|
return $this->rules_agreement_version === LATEST_RULES_VERSION;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 02:55:52 +03:00
|
|
|
|
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 03:18:37 +03:00
|
|
|
|
}
|