Implementation of the backend for the OAuth2 clients management

This commit is contained in:
ErickSkrauch
2018-02-28 01:27:35 +03:00
parent ddec87e3a9
commit 673429e577
55 changed files with 1810 additions and 65 deletions

View File

@@ -34,6 +34,7 @@ use const common\LATEST_RULES_VERSION;
* Отношения:
* @property EmailActivation[] $emailActivations
* @property OauthSession[] $oauthSessions
* @property OauthClient[] $oauthClients
* @property UsernameHistory[] $usernameHistory
* @property AccountSession[] $sessions
* @property MinecraftAccessKey[] $minecraftAccessKeys
@@ -93,6 +94,11 @@ class Account extends ActiveRecord {
return $this->hasMany(OauthSession::class, ['owner_id' => 'id'])->andWhere(['owner_type' => 'user']);
}
public function getOauthClients(): OauthClientQuery {
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->hasMany(OauthClient::class, ['account_id' => 'id']);
}
public function getUsernameHistory(): ActiveQuery {
return $this->hasMany(UsernameHistory::class, ['account_id' => 'id']);
}

View File

@@ -1,48 +1,62 @@
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
/**
* Поля модели:
* @property string $id
* @property string $secret
* @property string $type
* @property string $name
* @property string $description
* @property string $redirect_uri
* @property string $website_url
* @property string $minecraft_server_ip
* @property integer $account_id
* @property bool $is_trusted
* @property bool $is_deleted
* @property integer $created_at
*
* Отношения:
* @property Account $account
* @property Account|null $account
* @property OauthSession[] $sessions
*/
class OauthClient extends ActiveRecord {
public static function tableName() {
public const TYPE_APPLICATION = 'application';
public const TYPE_MINECRAFT_SERVER = 'minecraft-server';
public static function tableName(): string {
return '{{%oauth_clients}}';
}
public function rules() {
public function behaviors(): array {
return [
[['id'], 'required', 'when' => function(self $model) {
return $model->isNewRecord;
}],
[['id'], 'unique', 'when' => function(self $model) {
return $model->isNewRecord;
}],
[['name', 'description'], 'required'],
[['name', 'description'], 'string', 'max' => 255],
[
'class' => TimestampBehavior::class,
'updatedAtAttribute' => false,
],
];
}
public function getAccount() {
public function generateSecret(): void {
$this->secret = Yii::$app->security->generateRandomString(64);
}
public function getAccount(): ActiveQuery {
return $this->hasOne(Account::class, ['id' => 'account_id']);
}
public function getSessions() {
public function getSessions(): ActiveQuery {
return $this->hasMany(OauthSession::class, ['client_id' => 'id']);
}
public static function find(): OauthClientQuery {
return Yii::createObject(OauthClientQuery::class, [static::class]);
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace common\models;
use yii\db\ActiveQuery;
use yii\db\Command;
/**
* @see OauthClient
*/
class OauthClientQuery extends ActiveQuery {
private $showDeleted = false;
public function includeDeleted(): self {
$this->showDeleted = true;
return $this;
}
public function onlyDeleted(): self {
$this->showDeleted = true;
return $this->andWhere(['is_deleted' => true]);
}
public function createCommand($db = null): Command {
if ($this->showDeleted === false) {
$this->andWhere(['is_deleted' => false]);
}
return parent::createCommand($db);
}
}

View File

@@ -4,6 +4,7 @@ namespace common\models;
use common\components\Redis\Set;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
@@ -14,6 +15,7 @@ use yii\db\ActiveRecord;
* @property string|null $owner_id
* @property string $client_id
* @property string $client_redirect_uri
* @property integer $created_at
*
* Отношения
* @property OauthClient $client
@@ -26,6 +28,15 @@ class OauthSession extends ActiveRecord {
return '{{%oauth_sessions}}';
}
public function behaviors() {
return [
[
'class' => TimestampBehavior::class,
'updatedAtAttribute' => false,
],
];
}
public function getClient(): ActiveQuery {
return $this->hasOne(OauthClient::class, ['id' => 'client_id']);
}