2016-01-03 05:48:37 +05:30
|
|
|
<?php
|
2018-07-08 20:50:19 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
namespace common\models;
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
use Carbon\Carbon;
|
2016-01-15 14:51:27 +05:30
|
|
|
use common\components\UserPass;
|
2020-10-01 04:10:28 +05:30
|
|
|
use common\notifications\AccountDeletedNotification;
|
|
|
|
use common\notifications\AccountEditNotification;
|
2018-07-08 20:50:19 +05:30
|
|
|
use common\tasks\CreateWebHooksDeliveries;
|
2020-06-12 02:57:02 +05:30
|
|
|
use DateInterval;
|
|
|
|
use Webmozart\Assert\Assert;
|
2016-01-03 05:48:37 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
use yii\behaviors\TimestampBehavior;
|
2017-06-12 17:06:20 +05:30
|
|
|
use yii\db\ActiveQuery;
|
2016-01-03 05:48:37 +05:30
|
|
|
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
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Fields:
|
2020-06-12 02:57:02 +05:30
|
|
|
* @property int $id
|
2022-01-22 01:39:46 +05:30
|
|
|
* @property string $uuid UUID with dashes
|
2020-06-12 02:57:02 +05:30
|
|
|
* @property string $username
|
|
|
|
* @property string $email
|
|
|
|
* @property string $password_hash
|
|
|
|
* @property int $password_hash_strategy
|
|
|
|
* @property string $lang
|
|
|
|
* @property int $status
|
|
|
|
* @property int|null $rules_agreement_version
|
|
|
|
* @property string|null $registration_ip
|
|
|
|
* @property string|null $otp_secret
|
|
|
|
* @property int $is_otp_enabled
|
|
|
|
* @property int $created_at
|
|
|
|
* @property int $updated_at
|
|
|
|
* @property int $password_changed_at
|
|
|
|
* @property int|null $deleted_at shows the time, when the account was marked as deleted
|
2016-01-03 05:48:37 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Getters-setters:
|
|
|
|
* @property-write string $password plain user's password
|
|
|
|
* @property-read string $profileLink link to the user's Ely.by profile
|
2016-01-15 14:51:27 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Relations:
|
2019-12-10 04:08:09 +05:30
|
|
|
* @property-read EmailActivation[] $emailActivations
|
|
|
|
* @property-read OauthSession[] $oauthSessions
|
|
|
|
* @property-read OauthClient[] $oauthClients
|
|
|
|
* @property-read UsernameHistory[] $usernameHistory
|
|
|
|
* @property-read AccountSession[] $sessions
|
2016-01-15 14:51:27 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Behaviors:
|
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
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public const STATUS_DELETED = -10;
|
|
|
|
public const STATUS_BANNED = -1;
|
|
|
|
public const STATUS_REGISTERED = 0;
|
|
|
|
public const STATUS_ACTIVE = 10;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public const PASS_HASH_STRATEGY_OLD_ELY = 0;
|
|
|
|
public const PASS_HASH_STRATEGY_YII2 = 1;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public const ACCOUNT_DELETION_DELAY = 'P7D';
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public static function tableName(): string {
|
2020-06-12 02:57:02 +05:30
|
|
|
return 'accounts';
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public static function find(): AccountQuery {
|
|
|
|
return new AccountQuery(self::class);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function behaviors(): array {
|
2016-01-03 05:48:37 +05:30
|
|
|
return [
|
2016-05-12 14:20:30 +05:30
|
|
|
TimestampBehavior::class,
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function validatePassword(string $password, int $passwordHashStrategy = null): bool {
|
|
|
|
if ($passwordHashStrategy === null) {
|
2016-01-03 05:48:37 +05:30
|
|
|
$passwordHashStrategy = $this->password_hash_strategy;
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
switch ($passwordHashStrategy) {
|
2016-01-03 05:48:37 +05:30
|
|
|
case self::PASS_HASH_STRATEGY_OLD_ELY:
|
2018-04-18 02:17:25 +05:30
|
|
|
return UserPass::make($this->email, $password) === $this->password_hash;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function setPassword(string $password): void {
|
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
|
|
|
}
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function getEmailActivations(): EmailActivationQuery {
|
2016-03-13 04:49:00 +05:30
|
|
|
return $this->hasMany(EmailActivation::class, ['account_id' => 'id']);
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
public function getOauthSessions(): ActiveQuery {
|
2019-09-18 04:44:05 +05:30
|
|
|
return $this->hasMany(OauthSession::class, ['account_id' => 'id']);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
public function getOauthClients(): OauthClientQuery {
|
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */
|
|
|
|
return $this->hasMany(OauthClient::class, ['account_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getUsernameHistory(): ActiveQuery {
|
2016-04-24 00:14:10 +05:30
|
|
|
return $this->hasMany(UsernameHistory::class, ['account_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getSessions(): ActiveQuery {
|
2016-05-30 05:14:17 +05:30
|
|
|
return $this->hasMany(AccountSession::class, ['account_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +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
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Since we don't have info about the user's static_url, we still generate the simplest
|
|
|
|
* version with a link to the profile by it's id. On Ely.by, it will be redirected to static url.
|
2016-08-06 21:22:03 +05:30
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getProfileLink(): string {
|
2016-08-04 03:37:21 +05:30
|
|
|
return 'http://ely.by/u' . $this->id;
|
|
|
|
}
|
|
|
|
|
2016-08-06 21:22:03 +05:30
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Initially, the table of users we got from the main site, where there were no rules.
|
|
|
|
* All existing users at the time of migration received an empty value in this field.
|
|
|
|
* They will have to confirm their agreement with the rules at the first login.
|
|
|
|
* All new users automatically agree with the current version of the rules.
|
2016-08-06 21:22:03 +05:30
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function isAgreedWithActualRules(): bool {
|
2016-08-06 21:22:03 +05:30
|
|
|
return $this->rules_agreement_version === LATEST_RULES_VERSION;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function setRegistrationIp($ip): void {
|
2016-08-18 05:25:52 +05:30
|
|
|
$this->registration_ip = $ip === null ? null : inet_pton($ip);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getRegistrationIp(): ?string {
|
2016-08-18 05:25:52 +05:30
|
|
|
return $this->registration_ip === null ? null : inet_ntop($this->registration_ip);
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public function getDeleteAt(): Carbon {
|
|
|
|
Assert::notNull($this->deleted_at, 'This method should not be called on not deleted records');
|
|
|
|
return Carbon::createFromTimestamp($this->deleted_at)->add(new DateInterval(Account::ACCOUNT_DELETION_DELAY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterSave($insert, $changedAttributes): void {
|
2018-07-08 20:50:19 +05:30
|
|
|
parent::afterSave($insert, $changedAttributes);
|
|
|
|
|
|
|
|
if ($insert) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
$meaningfulFields = ['username', 'email', 'uuid', 'status', 'lang', 'deleted_at'];
|
|
|
|
$meaningfulChangedAttributes = array_filter(
|
|
|
|
$changedAttributes,
|
|
|
|
fn(string $key): bool => in_array($key, $meaningfulFields, true),
|
|
|
|
ARRAY_FILTER_USE_KEY,
|
|
|
|
);
|
2018-07-08 20:50:19 +05:30
|
|
|
if (empty($meaningfulChangedAttributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-01 04:10:28 +05:30
|
|
|
$notification = new AccountEditNotification($this, $meaningfulChangedAttributes);
|
|
|
|
Yii::$app->queue->push(new CreateWebHooksDeliveries($notification));
|
2018-07-08 20:50:19 +05:30
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public function afterDelete(): void {
|
|
|
|
parent::afterDelete();
|
2020-10-02 20:44:43 +05:30
|
|
|
if ($this->status !== self::STATUS_REGISTERED) {
|
|
|
|
Yii::$app->queue->push(new CreateWebHooksDeliveries(new AccountDeletedNotification($this)));
|
|
|
|
}
|
2020-06-12 02:57:02 +05:30
|
|
|
}
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|