2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
|
|
|
namespace common\models;
|
|
|
|
|
2016-11-27 20:11:39 +05:30
|
|
|
use common\components\Redis\Set;
|
2016-11-30 04:49:14 +05:30
|
|
|
use Yii;
|
2017-09-19 22:36:16 +05:30
|
|
|
use yii\base\NotSupportedException;
|
|
|
|
use yii\db\ActiveQuery;
|
2016-02-14 23:20:10 +05:30
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Поля:
|
2016-11-30 14:49:10 +05:30
|
|
|
* @property integer $id
|
2017-09-19 22:36:16 +05:30
|
|
|
* @property string $owner_type содержит одну из констант OauthOwnerType
|
|
|
|
* @property string|null $owner_id
|
2016-11-30 14:49:10 +05:30
|
|
|
* @property string $client_id
|
|
|
|
* @property string $client_redirect_uri
|
2016-02-14 23:20:10 +05:30
|
|
|
*
|
|
|
|
* Отношения
|
2016-11-30 14:49:10 +05:30
|
|
|
* @property OauthClient $client
|
|
|
|
* @property Account $account
|
|
|
|
* @property Set $scopes
|
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 {
|
2016-02-14 23:20:10 +05:30
|
|
|
return '{{%oauth_sessions}}';
|
|
|
|
}
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getScopes(): Set {
|
2016-11-01 22:06:39 +05:30
|
|
|
return new Set(static::getDb()->getSchema()->getRawTableName(static::tableName()), $this->id, 'scopes');
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAccessTokens() {
|
|
|
|
throw new NotSupportedException('This method is possible, but not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeDelete(): bool {
|
2016-02-14 23:20:10 +05:30
|
|
|
if (!$result = parent::beforeDelete()) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$this->clearScopes();
|
|
|
|
$this->removeRefreshToken();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeRefreshToken(): void {
|
2016-11-30 04:49:14 +05:30
|
|
|
/** @var \api\components\OAuth2\Storage\RefreshTokenStorage $refreshTokensStorage */
|
2017-09-19 22:36:16 +05:30
|
|
|
$refreshTokensStorage = Yii::$app->oauth->getRefreshTokenStorage();
|
2016-11-30 04:49:14 +05:30
|
|
|
$refreshTokensSet = $refreshTokensStorage->sessionHash($this->id);
|
|
|
|
foreach ($refreshTokensSet->members() as $refreshTokenId) {
|
|
|
|
$refreshTokensStorage->delete($refreshTokensStorage->get($refreshTokenId));
|
|
|
|
}
|
|
|
|
|
|
|
|
$refreshTokensSet->delete();
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function clearScopes(): void {
|
|
|
|
$this->getScopes()->delete();
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|