Добавлен обработчик для события блокировки аккаунта

This commit is contained in:
ErickSkrauch 2016-12-29 02:01:26 +03:00
parent 79bbc12206
commit b9e5e3a679
8 changed files with 89 additions and 23 deletions

View File

@ -73,15 +73,6 @@ return [
'response' => [ 'response' => [
'format' => yii\web\Response::FORMAT_JSON, 'format' => yii\web\Response::FORMAT_JSON,
], ],
'oauth' => [
'class' => api\components\OAuth2\Component::class,
'grantTypes' => ['authorization_code', 'client_credentials'],
'grantMap' => [
'authorization_code' => api\components\OAuth2\Grants\AuthCodeGrant::class,
'refresh_token' => api\components\OAuth2\Grants\RefreshTokenGrant::class,
'client_credentials' => api\components\OAuth2\Grants\ClientCredentialsGrant::class,
],
],
'errorHandler' => [ 'errorHandler' => [
'class' => api\components\ErrorHandler::class, 'class' => api\components\ErrorHandler::class,
], ],

View File

@ -16,12 +16,13 @@ class Yii extends \yii\BaseYii {
* Class BaseApplication * Class BaseApplication
* Used for properties that are identical for both WebApplication and ConsoleApplication * Used for properties that are identical for both WebApplication and ConsoleApplication
* *
* @property \yii\swiftmailer\Mailer $mailer * @property \yii\swiftmailer\Mailer $mailer
* @property \common\components\Redis\Connection $redis * @property \common\components\Redis\Connection $redis
* @property \common\components\RabbitMQ\Component $amqp * @property \common\components\RabbitMQ\Component $amqp
* @property \GuzzleHttp\Client $guzzle * @property \GuzzleHttp\Client $guzzle
* @property \common\components\EmailRenderer $emailRenderer * @property \common\components\EmailRenderer $emailRenderer
* @property \mito\sentry\Component $sentry * @property \mito\sentry\Component $sentry
* @property \api\components\OAuth2\Component $oauth
*/ */
abstract class BaseApplication extends yii\base\Application { abstract class BaseApplication extends yii\base\Application {
} }
@ -33,7 +34,6 @@ abstract class BaseApplication extends yii\base\Application {
* @property \api\components\User\Component $user User component. * @property \api\components\User\Component $user User component.
* @property \api\components\ApiUser\Component $apiUser Api User component. * @property \api\components\ApiUser\Component $apiUser Api User component.
* @property \api\components\ReCaptcha\Component $reCaptcha * @property \api\components\ReCaptcha\Component $reCaptcha
* @property \api\components\OAuth2\Component $oauth
* *
* @method \api\components\User\Component getUser() * @method \api\components\User\Component getUser()
*/ */

View File

@ -69,6 +69,15 @@ return [
'class' => common\components\EmailRenderer::class, 'class' => common\components\EmailRenderer::class,
'basePath' => '/images/emails', 'basePath' => '/images/emails',
], ],
'oauth' => [
'class' => api\components\OAuth2\Component::class,
'grantTypes' => ['authorization_code', 'client_credentials'],
'grantMap' => [
'authorization_code' => api\components\OAuth2\Grants\AuthCodeGrant::class,
'refresh_token' => api\components\OAuth2\Grants\RefreshTokenGrant::class,
'client_credentials' => api\components\OAuth2\Grants\ClientCredentialsGrant::class,
],
],
], ],
'aliases' => [ 'aliases' => [
'@bower' => '@vendor/bower-asset', '@bower' => '@vendor/bower-asset',

View File

@ -29,10 +29,11 @@ use const common\LATEST_RULES_VERSION;
* @property string $profileLink ссылка на профиль на Ely без поддержки static url (только для записи) * @property string $profileLink ссылка на профиль на Ely без поддержки static url (только для записи)
* *
* Отношения: * Отношения:
* @property EmailActivation[] $emailActivations * @property EmailActivation[] $emailActivations
* @property OauthSession[] $oauthSessions * @property OauthSession[] $oauthSessions
* @property UsernameHistory[] $usernameHistory * @property UsernameHistory[] $usernameHistory
* @property AccountSession[] $sessions * @property AccountSession[] $sessions
* @property MinecraftAccessKey[] $minecraftAccessKeys
* *
* Поведения: * Поведения:
* @mixin TimestampBehavior * @mixin TimestampBehavior
@ -99,7 +100,7 @@ class Account extends ActiveRecord {
} }
public function getOauthSessions() { public function getOauthSessions() {
return $this->hasMany(OauthSession::class, ['owner_id' => 'id']); return $this->hasMany(OauthSession::class, ['owner_id' => 'id'])->andWhere(['owner_type' => 'user']);
} }
public function getUsernameHistory() { public function getUsernameHistory() {
@ -110,6 +111,10 @@ class Account extends ActiveRecord {
return $this->hasMany(AccountSession::class, ['account_id' => 'id']); return $this->hasMany(AccountSession::class, ['account_id' => 'id']);
} }
public function getMinecraftAccessKeys() {
return $this->hasMany(MinecraftAccessKey::class, ['account_id' => 'id']);
}
/** /**
* Выполняет проверку, принадлежит ли этому нику аккаунт у Mojang * Выполняет проверку, принадлежит ли этому нику аккаунт у Mojang
* *

View File

@ -3,10 +3,13 @@ namespace console\controllers;
use common\components\Mojang\Api as MojangApi; use common\components\Mojang\Api as MojangApi;
use common\components\Mojang\exceptions\NoContentException; use common\components\Mojang\exceptions\NoContentException;
use common\models\Account;
use common\models\amqp\AccountBanned;
use common\models\amqp\UsernameChanged; use common\models\amqp\UsernameChanged;
use common\models\MojangUsername; use common\models\MojangUsername;
use Ely\Amqp\Builder\Configurator; use Ely\Amqp\Builder\Configurator;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Yii;
class AccountQueueController extends AmqpController { class AccountQueueController extends AmqpController {
@ -17,16 +20,18 @@ class AccountQueueController extends AmqpController {
public function configure(Configurator $configurator) { public function configure(Configurator $configurator) {
$configurator->exchange->topic()->durable(); $configurator->exchange->topic()->durable();
$configurator->queue->name('accounts-accounts-events')->durable(); $configurator->queue->name('accounts-accounts-events')->durable();
$configurator->bind->routingKey('accounts.username-changed'); $configurator->bind->routingKey('accounts.username-changed')
->add()->routingKey('account.account-banned');
} }
public function getRoutesMap() { public function getRoutesMap() {
return [ return [
'accounts.username-changed' => 'routeUsernameChanged', 'accounts.username-changed' => 'routeUsernameChanged',
'accounts.account-banned' => 'routeAccountBanned',
]; ];
} }
public function routeUsernameChanged(UsernameChanged $body) { public function routeUsernameChanged(UsernameChanged $body): bool {
$mojangApi = $this->createMojangApi(); $mojangApi = $this->createMojangApi();
try { try {
$response = $mojangApi->usernameToUUID($body->newUsername); $response = $mojangApi->usernameToUUID($body->newUsername);
@ -58,10 +63,32 @@ class AccountQueueController extends AmqpController {
return true; return true;
} }
public function routeAccountBanned(AccountBanned $body): bool {
$account = Account::findOne($body->accountId);
if ($account === null) {
Yii::warning('Cannot find banned account ' . $body->accountId . '. Skipping.');
return true;
}
foreach ($account->sessions as $authSession) {
$authSession->delete();
}
foreach ($account->minecraftAccessKeys as $key) {
$key->delete();
}
foreach ($account->oauthSessions as $oauthSession) {
$oauthSession->delete();
}
return true;
}
/** /**
* @return MojangApi * @return MojangApi
*/ */
protected function createMojangApi() : MojangApi { protected function createMojangApi(): MojangApi {
return new MojangApi(); return new MojangApi();
} }

View File

@ -16,4 +16,12 @@ return [
'created_at' => time(), 'created_at' => time(),
'last_refreshed_at' => time(), 'last_refreshed_at' => time(),
], ],
'banned-user-session' => [
'id' => 3,
'account_id' => 10,
'refresh_token' => 'Af7fIuV6eL61tRUHn40yhmDRXN1OQxKR',
'last_used_ip' => ip2long('182.123.234.123'),
'created_at' => time(),
'last_refreshed_at' => time(),
],
]; ];

View File

@ -7,4 +7,11 @@ return [
'client_id' => 'test1', 'client_id' => 'test1',
'client_redirect_uri' => 'http://test1.net/oauth', 'client_redirect_uri' => 'http://test1.net/oauth',
], ],
'banned-account-session' => [
'id' => 2,
'owner_type' => 'user',
'owner_id' => 10,
'client_id' => 'test1',
'client_redirect_uri' => 'http://test1.net/oauth',
],
]; ];

View File

@ -4,6 +4,7 @@ namespace codeception\console\unit\controllers;
use common\components\Mojang\Api; use common\components\Mojang\Api;
use common\components\Mojang\exceptions\NoContentException; use common\components\Mojang\exceptions\NoContentException;
use common\components\Mojang\response\UsernameToUUIDResponse; use common\components\Mojang\response\UsernameToUUIDResponse;
use common\models\amqp\AccountBanned;
use common\models\amqp\UsernameChanged; use common\models\amqp\UsernameChanged;
use common\models\MojangUsername; use common\models\MojangUsername;
use console\controllers\AccountQueueController; use console\controllers\AccountQueueController;
@ -143,4 +144,22 @@ class AccountQueueControllerTest extends TestCase {
$this->assertNotEquals($mojangInfo->uuid, $mojangUsername->uuid); $this->assertNotEquals($mojangInfo->uuid, $mojangUsername->uuid);
} }
public function testRouteAccountBanned() {
/** @var \common\models\Account $bannedAccount */
$bannedAccount = $this->tester->grabFixture('accounts', 'banned-account');
$this->tester->haveFixtures([
'oauthSessions' => \tests\codeception\common\fixtures\OauthSessionFixture::class,
'minecraftAccessKeys' => \tests\codeception\common\fixtures\MinecraftAccessKeyFixture::class,
'authSessions' => \tests\codeception\common\fixtures\AccountSessionFixture::class,
]);
$body = new AccountBanned();
$body->accountId = $bannedAccount->id;
$this->controller->routeAccountBanned($body);
$this->assertEmpty($bannedAccount->sessions);
$this->assertEmpty($bannedAccount->minecraftAccessKeys);
$this->assertEmpty($bannedAccount->oauthSessions);
}
} }