Добавлено событие о изменении email адреса пользователя, вызовы методов для генерации уведомлений внесены внутрь транзакции бд

This commit is contained in:
ErickSkrauch 2016-07-17 18:42:37 +03:00
parent 6d3db89140
commit dd0c4fcc9e
4 changed files with 55 additions and 13 deletions

View File

@ -11,6 +11,7 @@ use common\models\confirmations\RegistrationConfirmation;
use common\models\EmailActivation;
use common\validators\LanguageValidator;
use common\validators\PasswordValidate;
use Exception;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\base\ErrorException;
@ -68,6 +69,7 @@ class RegistrationForm extends ApiForm {
/**
* @return Account|null the saved model or null if saving fails
* @throws Exception
*/
public function signup() {
if (!$this->validate()) {
@ -97,15 +99,15 @@ class RegistrationForm extends ApiForm {
$this->sendMail($emailActivation, $account);
$changeUsernameForm = new ChangeUsernameForm();
$changeUsernameForm->createEventTask($account->id, $account->username, null);
$transaction->commit();
} catch (ErrorException $e) {
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
$changeUsernameForm = new ChangeUsernameForm();
$changeUsernameForm->createEventTask($account->id, $account->username, null);
return $account;
}

View File

@ -2,10 +2,13 @@
namespace api\models\profile\ChangeEmail;
use api\models\base\KeyConfirmationForm;
use common\helpers\Amqp;
use common\models\Account;
use common\models\amqp\EmailChanged;
use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use Yii;
use yii\base\ErrorException;
use yii\base\Exception;
class ConfirmNewEmailForm extends KeyConfirmationForm {
@ -38,11 +41,14 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
$activation->delete();
$account = $this->getAccount();
$oldEmail = $account->email;
$account->email = $activation->newEmail;
if (!$account->save()) {
throw new ErrorException('Cannot save new account email value');
}
$this->createTask($account->id, $account->email, $oldEmail);
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
@ -52,4 +58,23 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
return true;
}
/**
* @param integer $accountId
* @param string $newEmail
* @param string $oldEmail
* @throws \PhpAmqpLib\Exception\AMQPExceptionInterface
*/
public function createTask($accountId, $newEmail, $oldEmail) {
$model = new EmailChanged;
$model->accountId = $accountId;
$model->oldEmail = $oldEmail;
$model->newEmail = $newEmail;
$message = Amqp::getInstance()->prepareMessage($model, [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
]);
Amqp::sendToEventsExchange('accounts.email-changed', $message);
}
}

View File

@ -6,6 +6,7 @@ use common\helpers\Error;
use common\helpers\Amqp;
use common\models\amqp\UsernameChanged;
use common\models\UsernameHistory;
use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use Yii;
use yii\base\ErrorException;
@ -51,14 +52,14 @@ class ChangeUsernameForm extends PasswordProtectedForm {
throw new ErrorException('Cannot save username history record');
}
$this->createEventTask($account->id, $account->username, $oldNickname);
$transaction->commit();
} catch (ErrorException $e) {
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
$this->createEventTask($account->id, $account->username, $oldNickname);
return true;
}
@ -68,13 +69,13 @@ class ChangeUsernameForm extends PasswordProtectedForm {
* @param integer $accountId
* @param string $newNickname
* @param string $oldNickname
* @throws \PhpAmqpLib\Exception\AMQPExceptionInterface
*/
public function createEventTask($accountId, $newNickname, $oldNickname) {
$model = new UsernameChanged([
'accountId' => $accountId,
'oldUsername' => $oldNickname,
'newUsername' => $newNickname,
]);
$model = new UsernameChanged;
$model->accountId = $accountId;
$model-> oldUsername = $oldNickname;
$model->newUsername = $newNickname;
$message = Amqp::getInstance()->prepareMessage($model, [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,

View File

@ -0,0 +1,14 @@
<?php
namespace common\models\amqp;
use yii\base\Object;
class EmailChanged extends Object {
public $accountId;
public $oldEmail;
public $newEmail;
}