2016-04-12 03:16:44 +05:30
|
|
|
<?php
|
|
|
|
namespace console\controllers;
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\components\Mojang\Api as MojangApi;
|
|
|
|
use common\components\Mojang\exceptions\NoContentException;
|
2016-12-29 04:31:26 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\amqp\AccountBanned;
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\models\amqp\UsernameChanged;
|
|
|
|
use common\models\MojangUsername;
|
2016-11-16 01:15:30 +05:30
|
|
|
use Ely\Amqp\Builder\Configurator;
|
2016-10-26 00:50:44 +05:30
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2016-12-29 04:31:26 +05:30
|
|
|
use Yii;
|
2016-04-12 03:16:44 +05:30
|
|
|
|
|
|
|
class AccountQueueController extends AmqpController {
|
|
|
|
|
|
|
|
public function getExchangeName() {
|
2016-07-17 20:55:24 +05:30
|
|
|
return 'events';
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|
|
|
|
|
2016-11-16 01:15:30 +05:30
|
|
|
public function configure(Configurator $configurator) {
|
|
|
|
$configurator->exchange->topic()->durable();
|
|
|
|
$configurator->queue->name('accounts-accounts-events')->durable();
|
2016-12-29 04:31:26 +05:30
|
|
|
$configurator->bind->routingKey('accounts.username-changed')
|
|
|
|
->add()->routingKey('account.account-banned');
|
2016-07-17 20:55:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoutesMap() {
|
|
|
|
return [
|
|
|
|
'accounts.username-changed' => 'routeUsernameChanged',
|
2016-12-29 04:31:26 +05:30
|
|
|
'accounts.account-banned' => 'routeAccountBanned',
|
2016-07-17 20:55:24 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-29 04:31:26 +05:30
|
|
|
public function routeUsernameChanged(UsernameChanged $body): bool {
|
2016-10-31 05:17:18 +05:30
|
|
|
$mojangApi = $this->createMojangApi();
|
2016-04-24 00:14:10 +05:30
|
|
|
try {
|
|
|
|
$response = $mojangApi->usernameToUUID($body->newUsername);
|
|
|
|
} catch (NoContentException $e) {
|
|
|
|
$response = false;
|
2016-10-26 00:50:44 +05:30
|
|
|
} catch (RequestException $e) {
|
|
|
|
return true;
|
2016-04-24 00:14:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne($body->newUsername);
|
|
|
|
if ($response === false) {
|
|
|
|
if ($mojangUsername !== null) {
|
|
|
|
$mojangUsername->delete();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($mojangUsername === null) {
|
|
|
|
$mojangUsername = new MojangUsername();
|
|
|
|
$mojangUsername->username = $response->name;
|
|
|
|
$mojangUsername->uuid = $response->id;
|
|
|
|
} else {
|
|
|
|
$mojangUsername->uuid = $response->id;
|
|
|
|
$mojangUsername->touch('last_pulled_at');
|
|
|
|
}
|
|
|
|
|
|
|
|
$mojangUsername->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|
|
|
|
|
2016-12-29 04:31:26 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-31 05:17:18 +05:30
|
|
|
/**
|
|
|
|
* @return MojangApi
|
|
|
|
*/
|
2016-12-29 04:31:26 +05:30
|
|
|
protected function createMojangApi(): MojangApi {
|
2016-10-31 05:17:18 +05:30
|
|
|
return new MojangApi();
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|