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-09-18 04:44:05 +05:30
|
|
|
* @property int $account_id
|
|
|
|
* @property string $client_id
|
|
|
|
* @property int $legacy_id
|
|
|
|
* @property array $scopes
|
|
|
|
* @property integer $created_at
|
2016-02-14 23:20:10 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Relations:
|
2019-09-22 02:47:21 +05:30
|
|
|
* @property-read OauthClient $client
|
|
|
|
* @property-read Account $account
|
|
|
|
* @property-read OauthRefreshToken[] $refreshTokens
|
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,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this->hasOne(Account::class, ['id' => 'owner_id']);
|
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
public function getRefreshTokens(): ActiveQuery {
|
|
|
|
return $this->hasMany(OauthRefreshToken::class, ['account_id' => 'account_id', 'client_id' => 'client_id']);
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|