mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Merge branch 'master' into profile
This commit is contained in:
@@ -11,6 +11,7 @@ class BaseKeyConfirmationForm extends BaseApiForm {
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
// TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу
|
||||
['key', 'required', 'message' => 'error.key_is_required'],
|
||||
['key', 'validateKey'],
|
||||
];
|
||||
|
@@ -68,7 +68,7 @@ class LoginForm extends BaseApiForm {
|
||||
/**
|
||||
* @return Account|null
|
||||
*/
|
||||
protected function getAccount() {
|
||||
public function getAccount() {
|
||||
if ($this->_account === NULL) {
|
||||
$attribute = strpos($this->login, '@') ? 'email' : 'username';
|
||||
$this->_account = Account::findOne([$attribute => $this->login]);
|
||||
|
@@ -81,25 +81,7 @@ class RegistrationForm extends BaseApiForm {
|
||||
throw new ErrorException('Unable save email-activation model.');
|
||||
}
|
||||
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose(
|
||||
[
|
||||
'html' => '@app/mails/registration-confirmation-html',
|
||||
'text' => '@app/mails/registration-confirmation-text',
|
||||
],
|
||||
[
|
||||
'key' => $emailActivation->key,
|
||||
]
|
||||
)
|
||||
->setTo([$account->email => $account->username])
|
||||
->setFrom([Yii::$app->params['fromEmail'] => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account registration');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
$this->sendMail($emailActivation, $account);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
@@ -110,4 +92,24 @@ class RegistrationForm extends BaseApiForm {
|
||||
return $account;
|
||||
}
|
||||
|
||||
// TODO: подумать, чтобы вынести этот метод в какую-то отдельную конструкцию, т.к. используется и внутри NewAccountActivationForm
|
||||
public function sendMail(EmailActivation $emailActivation, Account $account) {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose([
|
||||
'html' => '@app/mails/registration-confirmation-html',
|
||||
'text' => '@app/mails/registration-confirmation-text',
|
||||
], [
|
||||
'key' => $emailActivation->key,
|
||||
])
|
||||
->setTo([$account->email => $account->username])
|
||||
->setFrom([Yii::$app->params['fromEmail'] => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account registration');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
101
api/models/RepeatAccountActivationForm.php
Normal file
101
api/models/RepeatAccountActivationForm.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class RepeatAccountActivationForm extends BaseApiForm {
|
||||
|
||||
// Частота повтора отправки нового письма
|
||||
const REPEAT_FREQUENCY = 5 * 60;
|
||||
|
||||
public $email;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
['email', 'filter', 'filter' => 'trim'],
|
||||
['email', 'required', 'message' => 'error.email_required'],
|
||||
['email', 'validateEmailForAccount'],
|
||||
['email', 'validateExistsActivation'],
|
||||
];
|
||||
}
|
||||
|
||||
public function validateEmailForAccount($attribute) {
|
||||
if (!$this->hasErrors($attribute)) {
|
||||
$account = $this->getAccount();
|
||||
if ($account === null) {
|
||||
$this->addError($attribute, "error.{$attribute}_not_found");
|
||||
} elseif ($account->status === Account::STATUS_ACTIVE) {
|
||||
$this->addError($attribute, "error.account_already_activated");
|
||||
} elseif ($account->status !== Account::STATUS_REGISTERED) {
|
||||
// TODO: такие аккаунты следует логировать за попытку к саботажу
|
||||
$this->addError($attribute, "error.account_cannot_resend_message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function validateExistsActivation($attribute) {
|
||||
if (!$this->hasErrors($attribute)) {
|
||||
if ($this->getActiveActivation() !== null) {
|
||||
$this->addError($attribute, 'error.recently_sent_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function sendRepeatMessage() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$account = $this->getAccount();
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
EmailActivation::deleteAll([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
||||
]);
|
||||
|
||||
$activation = new EmailActivation();
|
||||
$activation->account_id = $account->id;
|
||||
$activation->type = EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION;
|
||||
$activation->key = UserFriendlyRandomKey::make();
|
||||
if (!$activation->save()) {
|
||||
throw new ErrorException('Unable save email-activation model.');
|
||||
}
|
||||
|
||||
$regForm = new RegistrationForm();
|
||||
$regForm->sendMail($activation, $account);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account|null
|
||||
*/
|
||||
public function getAccount() {
|
||||
return Account::find()
|
||||
->andWhere(['email' => $this->email])
|
||||
->one();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EmailActivation|null
|
||||
*/
|
||||
public function getActiveActivation() {
|
||||
return $this->getAccount()
|
||||
->getEmailActivations()
|
||||
->andWhere(['type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION])
|
||||
->andWhere(['>=', 'created_at', time() - self::REPEAT_FREQUENCY])
|
||||
->one();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user