From dd0c4fcc9e06179effd5a262b2d35bf901ebaf92 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 17 Jul 2016 18:42:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BE=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B8?= =?UTF-8?q?=20email=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81=D0=B0=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F,?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=B7=D0=BE=D0=B2=D1=8B=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20=D0=B3=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D1=83=D0=B2=D0=B5?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20=D0=B2=D0=BD=D1=83=D1=82=D1=80?= =?UTF-8?q?=D1=8C=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../authentication/RegistrationForm.php | 10 ++++--- .../ChangeEmail/ConfirmNewEmailForm.php | 27 ++++++++++++++++++- api/models/profile/ChangeUsernameForm.php | 17 ++++++------ common/models/amqp/EmailChanged.php | 14 ++++++++++ 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 common/models/amqp/EmailChanged.php diff --git a/api/models/authentication/RegistrationForm.php b/api/models/authentication/RegistrationForm.php index dd82fce..67cdd62 100644 --- a/api/models/authentication/RegistrationForm.php +++ b/api/models/authentication/RegistrationForm.php @@ -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; } diff --git a/api/models/profile/ChangeEmail/ConfirmNewEmailForm.php b/api/models/profile/ChangeEmail/ConfirmNewEmailForm.php index 466dc04..d5683f9 100644 --- a/api/models/profile/ChangeEmail/ConfirmNewEmailForm.php +++ b/api/models/profile/ChangeEmail/ConfirmNewEmailForm.php @@ -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); + } + } diff --git a/api/models/profile/ChangeUsernameForm.php b/api/models/profile/ChangeUsernameForm.php index 5674049..ae42fe8 100644 --- a/api/models/profile/ChangeUsernameForm.php +++ b/api/models/profile/ChangeUsernameForm.php @@ -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, diff --git a/common/models/amqp/EmailChanged.php b/common/models/amqp/EmailChanged.php new file mode 100644 index 0000000..78cad35 --- /dev/null +++ b/common/models/amqp/EmailChanged.php @@ -0,0 +1,14 @@ +