mirror of
https://github.com/elyby/accounts.git
synced 2025-01-10 22:12:03 +05:30
Отправка всех email вынесена в очередь
This commit is contained in:
parent
4d48b38e47
commit
88175fea48
@ -2,6 +2,7 @@
|
||||
## Env приложения
|
||||
YII_DEBUG=true
|
||||
YII_ENV=dev
|
||||
DOMAIN=https://account.ely.by
|
||||
|
||||
## Параметры, отвечающие за безопасность
|
||||
JWT_USER_SECRET=
|
||||
|
@ -4,13 +4,14 @@ namespace api\models\authentication;
|
||||
use api\aop\annotations\CollectModelMetrics;
|
||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use api\models\base\ApiForm;
|
||||
use common\emails\EmailHelper;
|
||||
use common\helpers\Error as E;
|
||||
use api\traits\AccountFinder;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\models\EmailActivation;
|
||||
use common\tasks\SendPasswordRecoveryEmail;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class ForgotPasswordForm extends ApiForm {
|
||||
@ -80,7 +81,7 @@ class ForgotPasswordForm extends ApiForm {
|
||||
throw new ErrorException('Cannot create email activation for forgot password form');
|
||||
}
|
||||
|
||||
EmailHelper::forgotPassword($emailActivation);
|
||||
Yii::$app->queue->push(SendPasswordRecoveryEmail::createFromConfirmation($emailActivation));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -3,13 +3,13 @@ namespace api\models\authentication;
|
||||
|
||||
use api\aop\annotations\CollectModelMetrics;
|
||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use common\emails\EmailHelper;
|
||||
use api\models\base\ApiForm;
|
||||
use common\helpers\Error as E;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\models\UsernameHistory;
|
||||
use common\tasks\SendRegistrationEmail;
|
||||
use common\validators\EmailValidator;
|
||||
use common\validators\LanguageValidator;
|
||||
use common\validators\PasswordValidator;
|
||||
@ -104,7 +104,7 @@ class RegistrationForm extends ApiForm {
|
||||
throw new ErrorException('Cannot save username history record');
|
||||
}
|
||||
|
||||
EmailHelper::registration($emailActivation);
|
||||
Yii::$app->queue->push(SendRegistrationEmail::createFromConfirmation($emailActivation));
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
|
@ -3,15 +3,15 @@ namespace api\models\authentication;
|
||||
|
||||
use api\aop\annotations\CollectModelMetrics;
|
||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use common\emails\EmailHelper;
|
||||
use api\exceptions\ThisShouldNotHappenException;
|
||||
use api\models\base\ApiForm;
|
||||
use common\helpers\Error as E;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use common\tasks\SendRegistrationEmail;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class RepeatAccountActivationForm extends ApiForm {
|
||||
|
||||
@ -57,7 +57,6 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
/**
|
||||
* @CollectModelMetrics(prefix="signup.repeatEmail")
|
||||
* @return bool
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function sendRepeatMessage() {
|
||||
if (!$this->validate()) {
|
||||
@ -66,27 +65,23 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
|
||||
$account = $this->getAccount();
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
EmailActivation::deleteAll([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
||||
]);
|
||||
|
||||
$activation = new RegistrationConfirmation();
|
||||
$activation->account_id = $account->id;
|
||||
$activation->key = UserFriendlyRandomKey::make();
|
||||
if (!$activation->save()) {
|
||||
throw new ErrorException('Unable save email-activation model.');
|
||||
}
|
||||
EmailActivation::deleteAll([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
||||
]);
|
||||
|
||||
EmailHelper::registration($activation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
$activation = new RegistrationConfirmation();
|
||||
$activation->account_id = $account->id;
|
||||
$activation->key = UserFriendlyRandomKey::make();
|
||||
if (!$activation->save()) {
|
||||
throw new ThisShouldNotHappenException('Unable save email-activation model.');
|
||||
}
|
||||
|
||||
Yii::$app->queue->push(SendRegistrationEmail::createFromConfirmation($activation));
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,11 @@ namespace api\modules\accounts\models;
|
||||
|
||||
use api\aop\annotations\CollectModelMetrics;
|
||||
use api\exceptions\ThisShouldNotHappenException;
|
||||
use common\emails\EmailHelper;
|
||||
use api\validators\PasswordRequiredValidator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use common\tasks\SendCurrentEmailConfirmation;
|
||||
use Yii;
|
||||
|
||||
class SendEmailVerificationForm extends AccountActionForm {
|
||||
@ -48,7 +48,7 @@ class SendEmailVerificationForm extends AccountActionForm {
|
||||
$this->removeOldCode();
|
||||
$activation = $this->createCode();
|
||||
|
||||
EmailHelper::changeEmailConfirmCurrent($activation);
|
||||
Yii::$app->queue->push(SendCurrentEmailConfirmation::createFromConfirmation($activation));
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
|
@ -3,10 +3,10 @@ namespace api\modules\accounts\models;
|
||||
|
||||
use api\aop\annotations\CollectModelMetrics;
|
||||
use api\exceptions\ThisShouldNotHappenException;
|
||||
use common\emails\EmailHelper;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use common\tasks\SendNewEmailConfirmation;
|
||||
use common\validators\EmailValidator;
|
||||
use Yii;
|
||||
|
||||
@ -39,7 +39,7 @@ class SendNewEmailVerificationForm extends AccountActionForm {
|
||||
|
||||
$activation = $this->createCode();
|
||||
|
||||
EmailHelper::changeEmailConfirmNew($activation);
|
||||
Yii::$app->queue->push(SendNewEmailConfirmation::createFromConfirmation($activation));
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
|
@ -25,6 +25,7 @@ class Yii extends \yii\BaseYii {
|
||||
* @property \mito\sentry\Component $sentry
|
||||
* @property \api\components\OAuth2\Component $oauth
|
||||
* @property \common\components\StatsD $statsd
|
||||
* @property \yii\queue\Queue $queue
|
||||
*/
|
||||
abstract class BaseApplication extends yii\base\Application {
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ class EmailRenderer extends Component {
|
||||
parent::__construct($config);
|
||||
|
||||
if ($this->_baseDomain === null) {
|
||||
$this->_baseDomain = Yii::$app->request->getHostInfo();
|
||||
$this->_baseDomain = Yii::$app->urlManager->getHostInfo();
|
||||
if ($this->_baseDomain === null) {
|
||||
throw new InvalidConfigException('Cannot automatically obtain base domain');
|
||||
}
|
||||
@ -51,7 +51,7 @@ class EmailRenderer extends Component {
|
||||
* @param string $templateName
|
||||
* @return TemplateBuilder
|
||||
*/
|
||||
public function getTemplate(string $templateName) : TemplateBuilder {
|
||||
public function getTemplate(string $templateName): TemplateBuilder {
|
||||
return $this->renderer->getTemplate($templateName);
|
||||
}
|
||||
|
||||
@ -60,11 +60,11 @@ class EmailRenderer extends Component {
|
||||
* @throws \Ely\Email\RendererException
|
||||
* @return string
|
||||
*/
|
||||
public function render(TemplateBuilder $template) : string {
|
||||
public function render(TemplateBuilder $template): string {
|
||||
return $this->renderer->render($template);
|
||||
}
|
||||
|
||||
private function buildBasePath() : string {
|
||||
private function buildBasePath(): string {
|
||||
return $this->_baseDomain . $this->basePath;
|
||||
}
|
||||
|
||||
|
@ -96,6 +96,16 @@ return [
|
||||
'port' => getenv('STATSD_PORT') ?: 8125,
|
||||
'namespace' => getenv('STATSD_NAMESPACE') ?: 'ely.accounts.' . gethostname() . '.app',
|
||||
],
|
||||
'queue' => [
|
||||
'class' => yii\queue\amqp\Queue::class,
|
||||
'host' => getenv('RABBITMQ_HOST') ?: 'rabbitmq',
|
||||
'port' => getenv('RABBITMQ_PORT') ?: 5672,
|
||||
'user' => getenv('RABBITMQ_USER'),
|
||||
'password' => getenv('RABBITMQ_PASS'),
|
||||
'vhost' => getenv('RABBITMQ_VHOST'),
|
||||
'queueName' => 'worker',
|
||||
'exchangeName' => 'tasks',
|
||||
],
|
||||
],
|
||||
'container' => [
|
||||
'definitions' => [
|
||||
|
@ -1,56 +1,10 @@
|
||||
<?php
|
||||
namespace common\emails;
|
||||
|
||||
use common\emails\templates\ChangeEmailConfirmCurrentEmail;
|
||||
use common\emails\templates\ChangeEmailConfirmNewEmail;
|
||||
use common\emails\templates\ForgotPasswordEmail;
|
||||
use common\emails\templates\ForgotPasswordParams;
|
||||
use common\emails\templates\RegistrationEmail;
|
||||
use common\emails\templates\RegistrationEmailParams;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use Yii;
|
||||
|
||||
class EmailHelper {
|
||||
|
||||
public static function registration(RegistrationConfirmation $emailActivation): void {
|
||||
$account = $emailActivation->account;
|
||||
$locale = $account->lang;
|
||||
$params = new RegistrationEmailParams(
|
||||
$account->username,
|
||||
$emailActivation->key,
|
||||
Yii::$app->request->getHostInfo() . '/activation/' . $emailActivation->key
|
||||
);
|
||||
|
||||
(new RegistrationEmail(self::buildTo($account), $locale, $params))->send();
|
||||
}
|
||||
|
||||
public static function forgotPassword(ForgotPassword $emailActivation): void {
|
||||
$account = $emailActivation->account;
|
||||
$locale = $account->lang;
|
||||
$params = new ForgotPasswordParams(
|
||||
$account->username,
|
||||
$emailActivation->key,
|
||||
Yii::$app->request->getHostInfo() . '/recover-password/' . $emailActivation->key
|
||||
);
|
||||
|
||||
(new ForgotPasswordEmail(self::buildTo($account), $locale, $params))->send();
|
||||
}
|
||||
|
||||
public static function changeEmailConfirmCurrent(CurrentEmailConfirmation $emailActivation): void {
|
||||
(new ChangeEmailConfirmCurrentEmail(self::buildTo($emailActivation->account), $emailActivation->key))->send();
|
||||
}
|
||||
|
||||
public static function changeEmailConfirmNew(NewEmailConfirmation $emailActivation): void {
|
||||
$account = $emailActivation->account;
|
||||
(new ChangeEmailConfirmNewEmail(self::buildTo($account), $account->username, $emailActivation->key))->send();
|
||||
}
|
||||
|
||||
public static function buildTo(Account $account): array {
|
||||
return [$account->email => $account->username];
|
||||
public static function buildTo(string $username, string $email): array {
|
||||
return [$email => $username];
|
||||
}
|
||||
|
||||
}
|
||||
|
43
common/tasks/SendCurrentEmailConfirmation.php
Normal file
43
common/tasks/SendCurrentEmailConfirmation.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace common\tasks;
|
||||
|
||||
use common\emails\EmailHelper;
|
||||
use common\emails\templates\ChangeEmailConfirmCurrentEmail;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use yii\queue\RetryableJobInterface;
|
||||
|
||||
class SendCurrentEmailConfirmation implements RetryableJobInterface {
|
||||
|
||||
public $email;
|
||||
|
||||
public $username;
|
||||
|
||||
public $code;
|
||||
|
||||
public static function createFromConfirmation(CurrentEmailConfirmation $confirmation): self {
|
||||
$result = new self();
|
||||
$result->email = $confirmation->account->email;
|
||||
$result->username = $confirmation->account->username;
|
||||
$result->code = $confirmation->key;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getTtr() {
|
||||
return 30;
|
||||
}
|
||||
|
||||
public function canRetry($attempt, $error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \yii\queue\Queue $queue
|
||||
*/
|
||||
public function execute($queue) {
|
||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
||||
$template = new ChangeEmailConfirmCurrentEmail($to, $this->code);
|
||||
$template->send();
|
||||
}
|
||||
|
||||
}
|
43
common/tasks/SendNewEmailConfirmation.php
Normal file
43
common/tasks/SendNewEmailConfirmation.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace common\tasks;
|
||||
|
||||
use common\emails\EmailHelper;
|
||||
use common\emails\templates\ChangeEmailConfirmNewEmail;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use yii\queue\RetryableJobInterface;
|
||||
|
||||
class SendNewEmailConfirmation implements RetryableJobInterface {
|
||||
|
||||
public $email;
|
||||
|
||||
public $username;
|
||||
|
||||
public $code;
|
||||
|
||||
public static function createFromConfirmation(NewEmailConfirmation $confirmation): self {
|
||||
$result = new self();
|
||||
$result->email = $confirmation->getNewEmail();
|
||||
$result->username = $confirmation->account->username;
|
||||
$result->code = $confirmation->key;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getTtr() {
|
||||
return 30;
|
||||
}
|
||||
|
||||
public function canRetry($attempt, $error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \yii\queue\Queue $queue
|
||||
*/
|
||||
public function execute($queue) {
|
||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
||||
$template = new ChangeEmailConfirmNewEmail($to, $this->username, $this->code);
|
||||
$template->send();
|
||||
}
|
||||
|
||||
}
|
55
common/tasks/SendPasswordRecoveryEmail.php
Normal file
55
common/tasks/SendPasswordRecoveryEmail.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace common\tasks;
|
||||
|
||||
use common\emails\EmailHelper;
|
||||
use common\emails\templates\ForgotPasswordEmail;
|
||||
use common\emails\templates\ForgotPasswordParams;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use Yii;
|
||||
use yii\queue\RetryableJobInterface;
|
||||
|
||||
class SendPasswordRecoveryEmail implements RetryableJobInterface {
|
||||
|
||||
public $username;
|
||||
|
||||
public $email;
|
||||
|
||||
public $code;
|
||||
|
||||
public $link;
|
||||
|
||||
public $locale;
|
||||
|
||||
public static function createFromConfirmation(ForgotPassword $confirmation): self {
|
||||
$account = $confirmation->account;
|
||||
|
||||
$result = new self();
|
||||
$result->username = $account->username;
|
||||
$result->email = $account->email;
|
||||
$result->code = $confirmation->key;
|
||||
$result->link = Yii::$app->request->getHostInfo() . '/recover-password/' . $confirmation->key;
|
||||
$result->locale = $account->lang;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getTtr() {
|
||||
return 30;
|
||||
}
|
||||
|
||||
public function canRetry($attempt, $error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \yii\queue\Queue $queue
|
||||
* @throws \common\emails\exceptions\CannotSendEmailException
|
||||
*/
|
||||
public function execute($queue) {
|
||||
$params = new ForgotPasswordParams($this->username, $this->code, $this->link);
|
||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
||||
$template = new ForgotPasswordEmail($to, $this->locale, $params);
|
||||
$template->send();
|
||||
}
|
||||
|
||||
}
|
55
common/tasks/SendRegistrationEmail.php
Normal file
55
common/tasks/SendRegistrationEmail.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace common\tasks;
|
||||
|
||||
use common\emails\EmailHelper;
|
||||
use common\emails\templates\RegistrationEmail;
|
||||
use common\emails\templates\RegistrationEmailParams;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use Yii;
|
||||
use yii\queue\RetryableJobInterface;
|
||||
|
||||
class SendRegistrationEmail implements RetryableJobInterface {
|
||||
|
||||
public $username;
|
||||
|
||||
public $email;
|
||||
|
||||
public $code;
|
||||
|
||||
public $link;
|
||||
|
||||
public $locale;
|
||||
|
||||
public static function createFromConfirmation(RegistrationConfirmation $confirmation): self {
|
||||
$account = $confirmation->account;
|
||||
|
||||
$result = new self();
|
||||
$result->username = $account->username;
|
||||
$result->email = $account->email;
|
||||
$result->code = $confirmation->key;
|
||||
$result->link = Yii::$app->request->getHostInfo() . '/activation/' . $confirmation->key;
|
||||
$result->locale = $account->lang;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getTtr() {
|
||||
return 30;
|
||||
}
|
||||
|
||||
public function canRetry($attempt, $error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \yii\queue\Queue $queue
|
||||
* @throws \common\emails\exceptions\CannotSendEmailException
|
||||
*/
|
||||
public function execute($queue) {
|
||||
$params = new RegistrationEmailParams($this->username, $this->code, $this->link);
|
||||
$to = EmailHelper::buildTo($this->username, $this->email);
|
||||
$template = new RegistrationEmail($to, $this->locale, $params);
|
||||
$template->send();
|
||||
}
|
||||
|
||||
}
|
@ -25,7 +25,8 @@
|
||||
"paragonie/constant_time_encoding": "^2.0",
|
||||
"webmozart/assert": "^1.2.0",
|
||||
"goaop/framework": "~2.1.2",
|
||||
"domnikl/statsd": "^2.6"
|
||||
"domnikl/statsd": "^2.6",
|
||||
"yiisoft/yii2-queue": "~2.0.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"yiisoft/yii2-debug": "*",
|
||||
|
@ -1,13 +1,13 @@
|
||||
<?php
|
||||
$params = array_merge(
|
||||
require(__DIR__ . '/../../common/config/params.php'),
|
||||
require(__DIR__ . '/params.php')
|
||||
require __DIR__ . '/../../common/config/params.php',
|
||||
require __DIR__ . '/params.php'
|
||||
);
|
||||
|
||||
return [
|
||||
'id' => 'accounts-console',
|
||||
'basePath' => dirname(__DIR__),
|
||||
'bootstrap' => ['log'],
|
||||
'bootstrap' => ['log', 'queue'],
|
||||
'controllerNamespace' => 'console\controllers',
|
||||
'params' => $params,
|
||||
'components' => [
|
||||
@ -23,10 +23,13 @@ return [
|
||||
],
|
||||
],
|
||||
],
|
||||
'urlManager' => [
|
||||
'hostInfo' => getenv('DOMAIN') ?: 'https://account.ely.by',
|
||||
],
|
||||
],
|
||||
'controllerMap' => [
|
||||
'migrate' => [
|
||||
'class' => yii\console\controllers\MigrateController::class,
|
||||
'class' => yii\console\controllers\MigrateController::class,
|
||||
'templateFile' => '@console/views/migration.php',
|
||||
],
|
||||
],
|
||||
|
6
docker/supervisor/worker-queue.conf
Normal file
6
docker/supervisor/worker-queue.conf
Normal file
@ -0,0 +1,6 @@
|
||||
[program:account-queue-worker]
|
||||
directory=/var/www/html
|
||||
command=wait-for-it rabbitmq:5672 -- php yii queue/listen
|
||||
autostart=true
|
||||
autorestart=true
|
||||
priority=10
|
Loading…
x
Reference in New Issue
Block a user