2016-03-20 04:55:26 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\profile;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
use api\models\AccountIdentity;
|
|
|
|
use api\models\base\ApiForm;
|
|
|
|
use api\validators\PasswordRequiredValidator;
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\helpers\Amqp;
|
|
|
|
use common\models\amqp\UsernameChanged;
|
|
|
|
use common\models\UsernameHistory;
|
2016-11-02 01:57:38 +05:30
|
|
|
use common\validators\UsernameValidator;
|
2016-07-17 21:12:37 +05:30
|
|
|
use Exception;
|
2016-04-24 00:14:10 +05:30
|
|
|
use PhpAmqpLib\Message\AMQPMessage;
|
2016-03-20 04:55:26 +05:30
|
|
|
use Yii;
|
2016-04-24 00:14:10 +05:30
|
|
|
use yii\base\ErrorException;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
class ChangeUsernameForm extends ApiForm {
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
|
|
public $username;
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
public $password;
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
public function rules() {
|
2016-07-25 16:37:14 +05:30
|
|
|
return [
|
2016-11-02 01:57:38 +05:30
|
|
|
['username', UsernameValidator::class, 'accountCallback' => function() {
|
|
|
|
return $this->getAccount()->id;
|
|
|
|
}],
|
2016-07-25 16:37:14 +05:30
|
|
|
['password', PasswordRequiredValidator::class],
|
|
|
|
];
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function change() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
2016-03-20 04:55:26 +05:30
|
|
|
$account = $this->getAccount();
|
2016-04-24 00:14:10 +05:30
|
|
|
$oldNickname = $account->username;
|
|
|
|
try {
|
|
|
|
$account->username = $this->username;
|
2016-10-27 03:52:39 +05:30
|
|
|
if (!$account->save(false)) {
|
2016-04-24 00:14:10 +05:30
|
|
|
throw new ErrorException('Cannot save account model with new username');
|
|
|
|
}
|
2016-03-20 04:55:26 +05:30
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
$usernamesHistory = new UsernameHistory();
|
|
|
|
$usernamesHistory->account_id = $account->id;
|
|
|
|
$usernamesHistory->username = $account->username;
|
|
|
|
if (!$usernamesHistory->save()) {
|
|
|
|
throw new ErrorException('Cannot save username history record');
|
|
|
|
}
|
|
|
|
|
2016-07-17 21:12:37 +05:30
|
|
|
$this->createEventTask($account->id, $account->username, $oldNickname);
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
$transaction->commit();
|
2016-07-17 21:12:37 +05:30
|
|
|
} catch (Exception $e) {
|
2016-04-24 00:14:10 +05:30
|
|
|
$transaction->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: вынести это в отдельную сущность, т.к. эта команда используется внутри формы регистрации
|
|
|
|
*
|
|
|
|
* @param integer $accountId
|
|
|
|
* @param string $newNickname
|
|
|
|
* @param string $oldNickname
|
2016-07-17 21:12:37 +05:30
|
|
|
* @throws \PhpAmqpLib\Exception\AMQPExceptionInterface
|
2016-04-24 00:14:10 +05:30
|
|
|
*/
|
2016-07-17 20:55:24 +05:30
|
|
|
public function createEventTask($accountId, $newNickname, $oldNickname) {
|
2016-07-17 21:12:37 +05:30
|
|
|
$model = new UsernameChanged;
|
|
|
|
$model->accountId = $accountId;
|
2016-10-26 00:50:44 +05:30
|
|
|
$model->oldUsername = $oldNickname;
|
2016-07-17 21:12:37 +05:30
|
|
|
$model->newUsername = $newNickname;
|
2016-04-24 00:14:10 +05:30
|
|
|
|
2016-07-17 20:55:24 +05:30
|
|
|
$message = Amqp::getInstance()->prepareMessage($model, [
|
|
|
|
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
|
2016-04-24 00:14:10 +05:30
|
|
|
]);
|
2016-07-17 20:55:24 +05:30
|
|
|
|
|
|
|
Amqp::sendToEventsExchange('accounts.username-changed', $message);
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
protected function getAccount() : AccountIdentity {
|
|
|
|
return Yii::$app->user->identity;
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|