mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Все части, отвечающие за отправку E-mail вынесены в отдельный компонент
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\emails\EmailHelper;
|
||||
use api\models\base\ApiForm;
|
||||
use api\validators\TotpValidator;
|
||||
use common\helpers\Error as E;
|
||||
@@ -9,9 +10,7 @@ use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class ForgotPasswordForm extends ApiForm {
|
||||
use AccountFinder;
|
||||
@@ -92,41 +91,11 @@ class ForgotPasswordForm extends ApiForm {
|
||||
throw new ErrorException('Cannot create email activation for forgot password form');
|
||||
}
|
||||
|
||||
$this->sendMail($emailActivation);
|
||||
EmailHelper::forgotPassword($emailActivation);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendMail(EmailActivation $emailActivation) {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$fromEmail = Yii::$app->params['fromEmail'];
|
||||
if (!$fromEmail) {
|
||||
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
||||
}
|
||||
|
||||
$account = $emailActivation->account;
|
||||
$htmlBody = Yii::$app->emailRenderer->getTemplate('forgotPassword')
|
||||
->setLocale($account->lang)
|
||||
->setParams([
|
||||
'username' => $account->username,
|
||||
'code' => $emailActivation->key,
|
||||
'link' => Yii::$app->request->getHostInfo() . '/recover-password/' . $emailActivation->key,
|
||||
])
|
||||
->render();
|
||||
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose()
|
||||
->setHtmlBody($htmlBody)
|
||||
->setTo([$account->email => $account->username])
|
||||
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account forgot password');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
}
|
||||
|
||||
public function getLogin() {
|
||||
return $this->login;
|
||||
}
|
||||
|
@@ -2,12 +2,12 @@
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use api\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\EmailActivation;
|
||||
use common\models\UsernameHistory;
|
||||
use common\validators\EmailValidator;
|
||||
use common\validators\LanguageValidator;
|
||||
@@ -17,7 +17,6 @@ use Exception;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use const common\LATEST_RULES_VERSION;
|
||||
|
||||
@@ -103,7 +102,7 @@ class RegistrationForm extends ApiForm {
|
||||
throw new ErrorException('Cannot save username history record');
|
||||
}
|
||||
|
||||
$this->sendMail($emailActivation, $account);
|
||||
EmailHelper::registration($emailActivation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
@@ -114,37 +113,6 @@ class RegistrationForm extends ApiForm {
|
||||
return $account;
|
||||
}
|
||||
|
||||
// TODO: подумать, чтобы вынести этот метод в какую-то отдельную конструкцию, т.к. используется и внутри NewAccountActivationForm
|
||||
public function sendMail(EmailActivation $emailActivation, Account $account) {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
$fromEmail = Yii::$app->params['fromEmail'];
|
||||
|
||||
if (!$fromEmail) {
|
||||
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
||||
}
|
||||
|
||||
$htmlBody = Yii::$app->emailRenderer->getTemplate('register')
|
||||
->setLocale($account->lang)
|
||||
->setParams([
|
||||
'username' => $account->username,
|
||||
'code' => $emailActivation->key,
|
||||
'link' => Yii::$app->request->getHostInfo() . '/activation/' . $emailActivation->key,
|
||||
])
|
||||
->render();
|
||||
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose()
|
||||
->setHtmlBody($htmlBody)
|
||||
->setTo([$account->email => $account->username])
|
||||
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account registration');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод проверяет, можно ли занять указанный при регистрации ник или e-mail. Так случается,
|
||||
* что пользователи вводят неправильный e-mail или ник, после замечают это и пытаются вновь
|
||||
|
@@ -2,6 +2,7 @@
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use api\emails\EmailHelper;
|
||||
use api\models\base\ApiForm;
|
||||
use common\helpers\Error as E;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
@@ -72,8 +73,7 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
throw new ErrorException('Unable save email-activation model.');
|
||||
}
|
||||
|
||||
$regForm = new RegistrationForm();
|
||||
$regForm->sendMail($activation, $account);
|
||||
EmailHelper::registration($activation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
|
Reference in New Issue
Block a user