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-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error;
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\helpers\Amqp;
|
|
|
|
use common\models\amqp\UsernameChanged;
|
|
|
|
use common\models\UsernameHistory;
|
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-06-17 02:02:23 +05:30
|
|
|
['username', 'required', 'message' => Error::USERNAME_REQUIRED],
|
|
|
|
['username', 'validateUsername'],
|
2016-07-25 16:37:14 +05:30
|
|
|
['password', PasswordRequiredValidator::class],
|
|
|
|
];
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function validateUsername($attribute) {
|
2016-05-12 14:20:30 +05:30
|
|
|
$account = $this->getAccount();
|
2016-03-20 04:55:26 +05:30
|
|
|
$account->username = $this->$attribute;
|
|
|
|
if (!$account->validate(['username'])) {
|
2016-04-15 04:31:01 +05:30
|
|
|
$this->addErrors($account->getErrors());
|
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;
|
|
|
|
if (!$account->save()) {
|
|
|
|
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;
|
|
|
|
$model-> oldUsername = $oldNickname;
|
|
|
|
$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
|
|
|
}
|