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 @@ +