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-03-20 05:03:49 +05:30
|
|
|
use api\models\base\PasswordProtectedForm;
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\helpers\Amqp;
|
|
|
|
use common\models\amqp\UsernameChanged;
|
|
|
|
use common\models\UsernameHistory;
|
|
|
|
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
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
class ChangeUsernameForm extends PasswordProtectedForm {
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
|
|
public $username;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return ArrayHelper::merge(parent::rules(), [
|
|
|
|
[['username'], 'required', 'message' => 'error.{attribute}_required'],
|
|
|
|
[['username'], 'validateUsername'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
]);
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|