Добавлено событие о изменении 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\models\EmailActivation;
use common\validators\LanguageValidator; use common\validators\LanguageValidator;
use common\validators\PasswordValidate; use common\validators\PasswordValidate;
use Exception;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Yii; use Yii;
use yii\base\ErrorException; use yii\base\ErrorException;
@ -68,6 +69,7 @@ class RegistrationForm extends ApiForm {
/** /**
* @return Account|null the saved model or null if saving fails * @return Account|null the saved model or null if saving fails
* @throws Exception
*/ */
public function signup() { public function signup() {
if (!$this->validate()) { if (!$this->validate()) {
@ -97,15 +99,15 @@ class RegistrationForm extends ApiForm {
$this->sendMail($emailActivation, $account); $this->sendMail($emailActivation, $account);
$changeUsernameForm = new ChangeUsernameForm();
$changeUsernameForm->createEventTask($account->id, $account->username, null);
$transaction->commit(); $transaction->commit();
} catch (ErrorException $e) { } catch (Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
} }
$changeUsernameForm = new ChangeUsernameForm();
$changeUsernameForm->createEventTask($account->id, $account->username, null);
return $account; return $account;
} }

View File

@ -2,10 +2,13 @@
namespace api\models\profile\ChangeEmail; namespace api\models\profile\ChangeEmail;
use api\models\base\KeyConfirmationForm; use api\models\base\KeyConfirmationForm;
use common\helpers\Amqp;
use common\models\Account; use common\models\Account;
use common\models\amqp\EmailChanged;
use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use Yii; use Yii;
use yii\base\ErrorException; use yii\base\ErrorException;
use yii\base\Exception;
class ConfirmNewEmailForm extends KeyConfirmationForm { class ConfirmNewEmailForm extends KeyConfirmationForm {
@ -38,11 +41,14 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
$activation->delete(); $activation->delete();
$account = $this->getAccount(); $account = $this->getAccount();
$oldEmail = $account->email;
$account->email = $activation->newEmail; $account->email = $activation->newEmail;
if (!$account->save()) { if (!$account->save()) {
throw new ErrorException('Cannot save new account email value'); throw new ErrorException('Cannot save new account email value');
} }
$this->createTask($account->id, $account->email, $oldEmail);
$transaction->commit(); $transaction->commit();
} catch (Exception $e) { } catch (Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
@ -52,4 +58,23 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
return true; 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\helpers\Amqp;
use common\models\amqp\UsernameChanged; use common\models\amqp\UsernameChanged;
use common\models\UsernameHistory; use common\models\UsernameHistory;
use Exception;
use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Message\AMQPMessage;
use Yii; use Yii;
use yii\base\ErrorException; use yii\base\ErrorException;
@ -51,14 +52,14 @@ class ChangeUsernameForm extends PasswordProtectedForm {
throw new ErrorException('Cannot save username history record'); throw new ErrorException('Cannot save username history record');
} }
$this->createEventTask($account->id, $account->username, $oldNickname);
$transaction->commit(); $transaction->commit();
} catch (ErrorException $e) { } catch (Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
} }
$this->createEventTask($account->id, $account->username, $oldNickname);
return true; return true;
} }
@ -68,13 +69,13 @@ class ChangeUsernameForm extends PasswordProtectedForm {
* @param integer $accountId * @param integer $accountId
* @param string $newNickname * @param string $newNickname
* @param string $oldNickname * @param string $oldNickname
* @throws \PhpAmqpLib\Exception\AMQPExceptionInterface
*/ */
public function createEventTask($accountId, $newNickname, $oldNickname) { public function createEventTask($accountId, $newNickname, $oldNickname) {
$model = new UsernameChanged([ $model = new UsernameChanged;
'accountId' => $accountId, $model->accountId = $accountId;
'oldUsername' => $oldNickname, $model-> oldUsername = $oldNickname;
'newUsername' => $newNickname, $model->newUsername = $newNickname;
]);
$message = Amqp::getInstance()->prepareMessage($model, [ $message = Amqp::getInstance()->prepareMessage($model, [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT, '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;
}