2016-02-14 20:50:10 +03:00
|
|
|
<?php
|
2019-09-18 02:14:05 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-02-14 20:50:10 +03:00
|
|
|
namespace common\models;
|
|
|
|
|
2016-11-30 02:19:14 +03:00
|
|
|
use Yii;
|
2018-02-28 01:27:35 +03:00
|
|
|
use yii\behaviors\TimestampBehavior;
|
2017-09-19 20:06:16 +03:00
|
|
|
use yii\db\ActiveQuery;
|
2016-02-14 20:50:10 +03:00
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
|
|
|
|
/**
|
2019-07-15 01:59:56 +03:00
|
|
|
* Fields:
|
2019-12-10 01:38:09 +03:00
|
|
|
* @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
|
2016-02-14 20:50:10 +03:00
|
|
|
*
|
2019-07-15 01:59:56 +03:00
|
|
|
* Relations:
|
2019-09-22 00:17:21 +03:00
|
|
|
* @property-read OauthClient $client
|
|
|
|
* @property-read Account $account
|
2016-02-14 20:50:10 +03:00
|
|
|
*/
|
|
|
|
class OauthSession extends ActiveRecord {
|
|
|
|
|
2017-09-19 20:06:16 +03:00
|
|
|
public static function tableName(): string {
|
2019-09-18 02:14:05 +03:00
|
|
|
return 'oauth_sessions';
|
2019-08-08 02:47:36 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 02:14:05 +03:00
|
|
|
public function behaviors(): array {
|
2018-02-28 01:27:35 +03:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'updatedAtAttribute' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:06:16 +03:00
|
|
|
public function getClient(): ActiveQuery {
|
2016-02-14 20:50:10 +03:00
|
|
|
return $this->hasOne(OauthClient::class, ['id' => 'client_id']);
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:06:16 +03:00
|
|
|
public function getAccount(): ActiveQuery {
|
2019-12-15 18:27:31 +03:00
|
|
|
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 02:14:05 +03:00
|
|
|
public function getScopes(): array {
|
|
|
|
if (empty($this->scopes) && $this->legacy_id !== null) {
|
|
|
|
return Yii::$app->redis->smembers($this->getLegacyRedisScopesKey());
|
|
|
|
}
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2019-09-18 02:14:05 +03:00
|
|
|
return (array)$this->scopes;
|
2017-09-19 20:06:16 +03:00
|
|
|
}
|
|
|
|
|
2019-09-22 02:42:08 +03:00
|
|
|
/**
|
|
|
|
* 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 01:38:09 +03:00
|
|
|
// TODO: it seems that this method isn't used anywhere
|
2019-09-22 02:42:08 +03:00
|
|
|
if ($this->legacy_id === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Yii::$app->redis->smembers($this->getLegacyRedisRefreshTokensKey());
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:06:16 +03:00
|
|
|
public function beforeDelete(): bool {
|
2019-09-18 02:14:05 +03:00
|
|
|
if (!parent::beforeDelete()) {
|
|
|
|
return false;
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 02:14:05 +03:00
|
|
|
if ($this->legacy_id !== null) {
|
|
|
|
Yii::$app->redis->del($this->getLegacyRedisScopesKey());
|
2019-09-22 02:42:08 +03:00
|
|
|
Yii::$app->redis->del($this->getLegacyRedisRefreshTokensKey());
|
2016-11-30 02:19:14 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 02:14:05 +03:00
|
|
|
return true;
|
2017-09-19 20:06:16 +03:00
|
|
|
}
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2019-09-18 02:14:05 +03:00
|
|
|
private function getLegacyRedisScopesKey(): string {
|
|
|
|
return "oauth:sessions:{$this->legacy_id}:scopes";
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
2019-09-22 02:42:08 +03:00
|
|
|
private function getLegacyRedisRefreshTokensKey(): string {
|
|
|
|
return "oauth:sessions:{$this->legacy_id}:refresh:tokens";
|
|
|
|
}
|
|
|
|
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|