mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Образован trait AccountFinder для поиска пользователя по его нику\мылу
Модель EmailActivation теперь умеет автоматически создавать своих правильных потомков по соответствующему типу Добавлена форма восстановления пароля и её обработчик (без контроллера)
This commit is contained in:
122
api/models/ForgotPasswordForm.php
Normal file
122
api/models/ForgotPasswordForm.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use api\traits\AccountFinder;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RecoverPassword;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class ForgotPasswordForm extends ApiForm {
|
||||
use AccountFinder;
|
||||
|
||||
public $login;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
['login', 'required', 'message' => 'error.login_required'],
|
||||
['login', 'validateLogin'],
|
||||
['login', 'validateActivity'],
|
||||
['login', 'validateFrequency'],
|
||||
];
|
||||
}
|
||||
|
||||
public function validateLogin($attribute) {
|
||||
if (!$this->hasErrors()) {
|
||||
if ($this->getAccount() === null) {
|
||||
$this->addError($attribute, 'error.' . $attribute . '_not_exist');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function validateActivity($attribute) {
|
||||
if (!$this->hasErrors()) {
|
||||
$account = $this->getAccount();
|
||||
if ($account->status !== Account::STATUS_ACTIVE) {
|
||||
$this->addError($attribute, 'error.account_not_activated');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function validateFrequency($attribute) {
|
||||
if (!$this->hasErrors()) {
|
||||
$emailConfirmation = $this->getEmailActivation();
|
||||
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
|
||||
$this->addError($attribute, 'error.email_frequency');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function forgotPassword() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$account = $this->getAccount();
|
||||
$emailActivation = $this->getEmailActivation();
|
||||
if ($emailActivation === null) {
|
||||
$emailActivation = new RecoverPassword();
|
||||
$emailActivation->account_id = $account->id;
|
||||
} else {
|
||||
$emailActivation->created_at = time();
|
||||
}
|
||||
|
||||
$emailActivation->key = UserFriendlyRandomKey::make();
|
||||
if (!$emailActivation->save()) {
|
||||
throw new ErrorException('Cannot create email activation for forgot password form');
|
||||
}
|
||||
|
||||
$this->sendMail($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');
|
||||
}
|
||||
|
||||
$acceptor = $emailActivation->account;
|
||||
/** @var \yii\swiftmailer\Message $message */
|
||||
$message = $mailer->compose([
|
||||
'html' => '@app/mails/forgot-password-html',
|
||||
'text' => '@app/mails/forgot-password-text',
|
||||
], [
|
||||
'key' => $emailActivation->key,
|
||||
])
|
||||
->setTo([$acceptor->email => $acceptor->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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EmailActivation|null
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function getEmailActivation() {
|
||||
$account = $this->getAccount();
|
||||
if ($account === null) {
|
||||
throw new ErrorException('Account not founded');
|
||||
}
|
||||
|
||||
return $account->getEmailActivations()
|
||||
->andWhere(['type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY])
|
||||
->one();
|
||||
}
|
||||
|
||||
}
|
@@ -2,17 +2,17 @@
|
||||
namespace api\models;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use api\traits\AccountFinder;
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
|
||||
class LoginForm extends ApiForm {
|
||||
use AccountFinder;
|
||||
|
||||
public $login;
|
||||
public $password;
|
||||
public $rememberMe = true;
|
||||
|
||||
private $_account;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
['login', 'required', 'message' => 'error.login_required'],
|
||||
@@ -55,6 +55,10 @@ class LoginForm extends ApiForm {
|
||||
}
|
||||
}
|
||||
|
||||
public function getLogin() {
|
||||
return $this->login;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|string JWT с информацией об аккаунте
|
||||
*/
|
||||
@@ -66,19 +70,4 @@ class LoginForm extends ApiForm {
|
||||
return $this->getAccount()->getJWT();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account|null
|
||||
*/
|
||||
public function getAccount() {
|
||||
if ($this->_account === NULL) {
|
||||
$this->_account = Account::findOne([$this->getLoginAttribute() => $this->login]);
|
||||
}
|
||||
|
||||
return $this->_account;
|
||||
}
|
||||
|
||||
public function getLoginAttribute() {
|
||||
return strpos($this->login, '@') ? 'email' : 'username';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
||||
use api\models\base\ApiForm;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Yii;
|
||||
@@ -78,9 +79,8 @@ class RegistrationForm extends ApiForm {
|
||||
throw new ErrorException('Account not created.');
|
||||
}
|
||||
|
||||
$emailActivation = new EmailActivation();
|
||||
$emailActivation = new RegistrationConfirmation();
|
||||
$emailActivation->account_id = $account->id;
|
||||
$emailActivation->type = EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION;
|
||||
$emailActivation->key = UserFriendlyRandomKey::make();
|
||||
|
||||
if (!$emailActivation->save()) {
|
||||
|
@@ -4,17 +4,17 @@ namespace api\models;
|
||||
use api\models\base\ApiForm;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class RepeatAccountActivationForm extends ApiForm {
|
||||
|
||||
// Частота повтора отправки нового письма
|
||||
const REPEAT_FREQUENCY = 5 * 60;
|
||||
|
||||
public $email;
|
||||
|
||||
private $emailActivation;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
['email', 'filter', 'filter' => 'trim'],
|
||||
@@ -40,7 +40,8 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
|
||||
public function validateExistsActivation($attribute) {
|
||||
if (!$this->hasErrors($attribute)) {
|
||||
if ($this->getActiveActivation() !== null) {
|
||||
$activation = $this->getActivation();
|
||||
if ($activation !== null && !$activation->canRepeat()) {
|
||||
$this->addError($attribute, 'error.recently_sent_message');
|
||||
}
|
||||
}
|
||||
@@ -59,9 +60,8 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
||||
]);
|
||||
|
||||
$activation = new EmailActivation();
|
||||
$activation = new RegistrationConfirmation();
|
||||
$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.');
|
||||
@@ -84,19 +84,22 @@ class RepeatAccountActivationForm extends ApiForm {
|
||||
*/
|
||||
public function getAccount() {
|
||||
return Account::find()
|
||||
->andWhere(['email' => $this->email])
|
||||
->one();
|
||||
->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();
|
||||
public function getActivation() {
|
||||
if ($this->emailActivation === null) {
|
||||
$this->emailActivation = $this->getAccount()
|
||||
->getEmailActivations()
|
||||
->andWhere(['type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION])
|
||||
->one();
|
||||
}
|
||||
|
||||
return $this->emailActivation;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user