В проект внедрён RabbitMQ.

Контроллер для работы с RabbitMQ научился создавать типизированные аргументы для $body
Добавлена таблица с историей ников
Добавлена таблица Mojang ников
Добавлена проверка активированности аккаунта в AccountsController
This commit is contained in:
ErickSkrauch
2016-04-23 21:44:10 +03:00
parent 067fc1d3d6
commit cba769a1ec
24 changed files with 489 additions and 47 deletions

View File

@@ -16,10 +16,20 @@ class AccountsController extends Controller {
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['current', 'change-password', 'change-username'],
'actions' => ['current'],
'allow' => true,
'roles' => ['@'],
],
[
'actions' => ['change-password', 'change-username'],
'allow' => true,
'roles' => ['@'],
'matchCallback' => function() {
/** @var Account $account */
$account = Yii::$app->user->identity;
return $account->status > Account::STATUS_REGISTERED;
},
],
],
],
]);
@@ -29,6 +39,7 @@ class AccountsController extends Controller {
return [
'current' => ['GET'],
'change-password' => ['POST'],
'change-username' => ['POST'],
];
}
@@ -44,6 +55,7 @@ class AccountsController extends Controller {
'shouldChangePassword' => $account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY,
'isActive' => $account->status === Account::STATUS_ACTIVE,
'passwordChangedAt' => $account->password_changed_at,
'hasMojangUsernameCollision' => $account->hasMojangUsernameCollision(),
];
}

View File

@@ -2,8 +2,13 @@
namespace api\models;
use api\models\base\PasswordProtectedForm;
use common\helpers\Amqp;
use common\models\Account;
use common\models\amqp\UsernameChanged;
use common\models\UsernameHistory;
use PhpAmqpLib\Message\AMQPMessage;
use Yii;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
class ChangeUsernameForm extends PasswordProtectedForm {
@@ -30,10 +35,52 @@ class ChangeUsernameForm extends PasswordProtectedForm {
return false;
}
$transaction = Yii::$app->db->beginTransaction();
$account = $this->getAccount();
$account->username = $this->username;
$oldNickname = $account->username;
try {
$account->username = $this->username;
if (!$account->save()) {
throw new ErrorException('Cannot save account model with new username');
}
return $account->save();
$usernamesHistory = new UsernameHistory();
$usernamesHistory->account_id = $account->id;
$usernamesHistory->username = $account->username;
if (!$usernamesHistory->save()) {
throw new ErrorException('Cannot save username history record');
}
$transaction->commit();
} catch (ErrorException $e) {
$transaction->rollBack();
throw $e;
}
$this->createTask($account->id, $account->username, $oldNickname);
return true;
}
/**
* TODO: вынести это в отдельную сущность, т.к. эта команда используется внутри формы регистрации
*
* @param integer $accountId
* @param string $newNickname
* @param string $oldNickname
*/
public function createTask($accountId, $newNickname, $oldNickname) {
$message = Amqp::getInstance()->prepareMessage(new UsernameChanged([
'accountId' => $accountId,
'oldUsername' => $oldNickname,
'newUsername' => $newNickname,
]), [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
]);
Amqp::sendToExchange('account', 'username-changed', $message, [
3 => true, // durable -> true
]);
}
}

View File

@@ -95,6 +95,9 @@ class RegistrationForm extends ApiForm {
throw $e;
}
$changeUsernameForm = new ChangeUsernameForm();
$changeUsernameForm->createTask($account->id, $account->username, null);
return $account;
}