2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2019-09-18 04:44:05 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
namespace common\models;
|
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
use Yii;
|
2018-02-28 03:57:35 +05:30
|
|
|
use yii\behaviors\TimestampBehavior;
|
2017-09-19 22:36:16 +05:30
|
|
|
use yii\db\ActiveQuery;
|
2016-02-14 23:20:10 +05:30
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Fields:
|
2019-12-10 04:08:09 +05:30
|
|
|
* @property int $account_id
|
|
|
|
* @property string $client_id
|
|
|
|
* @property int|null $legacy_id
|
|
|
|
* @property array $scopes
|
|
|
|
* @property int $created_at
|
|
|
|
* @property int|null $revoked_at
|
2020-09-30 23:00:04 +05:30
|
|
|
* @property int $last_used_at
|
2016-02-14 23:20:10 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Relations:
|
2020-09-30 23:00:04 +05:30
|
|
|
* @property-read OauthClient|null $client
|
2019-09-22 02:47:21 +05:30
|
|
|
* @property-read Account $account
|
2020-09-30 23:00:04 +05:30
|
|
|
*
|
|
|
|
* Mixins:
|
|
|
|
* @mixin TimestampBehavior
|
2016-02-14 23:20:10 +05:30
|
|
|
*/
|
|
|
|
class OauthSession extends ActiveRecord {
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public static function tableName(): string {
|
2019-09-18 04:44:05 +05:30
|
|
|
return 'oauth_sessions';
|
2019-08-08 05:17:36 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
public function behaviors(): array {
|
2018-02-28 03:57:35 +05:30
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'updatedAtAttribute' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-30 23:00:04 +05:30
|
|
|
public function isRevoked(): bool {
|
|
|
|
return $this->revoked_at > $this->last_used_at;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getClient(): ActiveQuery {
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this->hasOne(OauthClient::class, ['id' => 'client_id']);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAccount(): ActiveQuery {
|
2019-12-15 20:57:31 +05:30
|
|
|
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
public function getScopes(): array {
|
|
|
|
if (empty($this->scopes) && $this->legacy_id !== null) {
|
|
|
|
return Yii::$app->redis->smembers($this->getLegacyRedisScopesKey());
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
return (array)$this->scopes;
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 05:12:08 +05:30
|
|
|
/**
|
|
|
|
* In the early period of the project existence, the refresh tokens related to the current session
|
|
|
|
* were stored in Redis. This method allows to get a list of these tokens.
|
|
|
|
*
|
|
|
|
* @return array of refresh tokens (ids)
|
|
|
|
*/
|
|
|
|
public function getLegacyRefreshTokens(): array {
|
2019-12-10 04:08:09 +05:30
|
|
|
// TODO: it seems that this method isn't used anywhere
|
2019-09-22 05:12:08 +05:30
|
|
|
if ($this->legacy_id === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Yii::$app->redis->smembers($this->getLegacyRedisRefreshTokensKey());
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function beforeDelete(): bool {
|
2019-09-18 04:44:05 +05:30
|
|
|
if (!parent::beforeDelete()) {
|
|
|
|
return false;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
if ($this->legacy_id !== null) {
|
|
|
|
Yii::$app->redis->del($this->getLegacyRedisScopesKey());
|
2019-09-22 05:12:08 +05:30
|
|
|
Yii::$app->redis->del($this->getLegacyRedisRefreshTokensKey());
|
2016-11-30 04:49:14 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
return true;
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
private function getLegacyRedisScopesKey(): string {
|
|
|
|
return "oauth:sessions:{$this->legacy_id}:scopes";
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 05:12:08 +05:30
|
|
|
private function getLegacyRedisRefreshTokensKey(): string {
|
|
|
|
return "oauth:sessions:{$this->legacy_id}:refresh:tokens";
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|