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-05-13 14:33:00 +05:30
|
|
|
|
use common\validators\LanguageValidator;
|
2016-05-01 22:13:28 +05:30
|
|
|
|
use Ely\Yii2\TempmailValidator;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
use Yii;
|
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
use yii\behaviors\TimestampBehavior;
|
|
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Поля модели:
|
|
|
|
|
* @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
|
|
|
|
|
* @property integer $created_at
|
|
|
|
|
* @property integer $updated_at
|
|
|
|
|
* @property integer $password_changed_at
|
2016-01-03 05:48:37 +05:30
|
|
|
|
*
|
|
|
|
|
* Геттеры-сеттеры:
|
2016-04-24 00:14:10 +05:30
|
|
|
|
* @property string $password пароль пользователя (только для записи)
|
2016-01-15 14:51:27 +05:30
|
|
|
|
*
|
|
|
|
|
* Отношения:
|
|
|
|
|
* @property EmailActivation[] $emailActivations
|
2016-05-30 05:14:17 +05:30
|
|
|
|
* @property OauthSession[] $oauthSessions
|
2016-04-24 00:14:10 +05:30
|
|
|
|
* @property UsernameHistory[] $usernameHistory
|
2016-05-30 05:14:17 +05:30
|
|
|
|
* @property AccountSession[] $sessions
|
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;
|
|
|
|
|
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
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
|
return [
|
2016-03-20 04:55:26 +05:30
|
|
|
|
[['username'], 'filter', 'filter' => 'trim'],
|
|
|
|
|
[['username'], 'required', 'message' => 'error.username_required'],
|
|
|
|
|
[['username'], 'string', 'min' => 3, 'max' => 21,
|
|
|
|
|
'tooShort' => 'error.username_too_short',
|
|
|
|
|
'tooLong' => 'error.username_too_long',
|
|
|
|
|
],
|
|
|
|
|
[['username'], 'match', 'pattern' => '/^[\p{L}\d-_\.!?#$%^&*()\[\]:;]+$/u',
|
|
|
|
|
'message' => 'error.username_invalid',
|
|
|
|
|
],
|
|
|
|
|
[['username'], 'unique', 'message' => 'error.username_not_available'],
|
|
|
|
|
|
|
|
|
|
[['email'], 'filter', 'filter' => 'trim'],
|
|
|
|
|
[['email'], 'required', 'message' => 'error.email_required'],
|
|
|
|
|
[['email'], 'string', 'max' => 255, 'tooLong' => 'error.email_too_long'],
|
|
|
|
|
[['email'], 'email', 'checkDNS' => true, 'enableIDN' => true, 'message' => 'error.email_invalid'],
|
2016-05-01 22:13:28 +05:30
|
|
|
|
[['email'], TempmailValidator::class, 'message' => 'error.email_is_tempmail'],
|
2016-03-20 04:55:26 +05:30
|
|
|
|
[['email'], 'unique', 'message' => 'error.email_not_available'],
|
2016-05-13 14:33:00 +05:30
|
|
|
|
|
|
|
|
|
[['lang'], LanguageValidator::class],
|
|
|
|
|
[['lang'], 'default', 'value' => 'en'],
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function validatePassword($password, $passwordHashStrategy = NULL) {
|
|
|
|
|
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-02-14 23:20:10 +05:30
|
|
|
|
return $this->hasMany(OauthSession::class, ['owner_id' => 'id']);
|
|
|
|
|
}
|
|
|
|
|
|
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-02-14 23:20:10 +05:30
|
|
|
|
/**
|
2016-04-24 00:14:10 +05:30
|
|
|
|
* Метод проверяет, может ли текущий пользователь быть автоматически авторизован
|
2016-02-14 23:20:10 +05:30
|
|
|
|
* для указанного клиента без запроса доступа к необходимому списку прав
|
|
|
|
|
*
|
|
|
|
|
* @param OauthClient $client
|
|
|
|
|
* @param \League\OAuth2\Server\Entity\ScopeEntity[] $scopes
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canAutoApprove(OauthClient $client, array $scopes = []) {
|
2016-03-13 04:49:00 +05:30
|
|
|
|
if ($client->is_trusted) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
|
|
/** @var OauthSession|null $session */
|
2016-05-30 05:14:17 +05:30
|
|
|
|
$session = $this->getOauthSessions()->andWhere(['client_id' => $client->id])->one();
|
2016-02-14 23:20:10 +05:30
|
|
|
|
if ($session !== null) {
|
|
|
|
|
$existScopes = $session->getScopes()->members();
|
|
|
|
|
if (empty(array_diff(array_keys($scopes), $existScopes))) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 03:52:04 +05:30
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
protected static function getSecretKey() {
|
|
|
|
|
return Yii::$app->params['jwtSecret'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-24 00:14:10 +05:30
|
|
|
|
* @inheritdoc
|
2016-02-23 03:52:04 +05:30
|
|
|
|
*/
|
|
|
|
|
protected static function getHeaderToken() {
|
|
|
|
|
return [
|
|
|
|
|
'iss' => Yii::$app->request->hostInfo,
|
|
|
|
|
'aud' => Yii::$app->request->hostInfo,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
|
/**
|
|
|
|
|
* Выполняет проверку, принадлежит ли этому нику аккаунт у Mojang
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasMojangUsernameCollision() {
|
|
|
|
|
return MojangUsername::find()
|
|
|
|
|
->andWhere(['username' => $this->username])
|
|
|
|
|
->exists();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 14:20:30 +05:30
|
|
|
|
/**
|
|
|
|
|
* TODO: нужно создать PR в UserTrait репо, чтобы этот метод сделали абстрактным
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getId() {
|
|
|
|
|
return $this->getPrimaryKey();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
|
}
|