2016-05-11 01:10:06 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\authentication;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
2017-04-18 21:38:11 +05:30
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
2016-05-11 01:10:06 +05:30
|
|
|
use api\models\base\ApiForm;
|
2017-01-24 02:20:13 +05:30
|
|
|
use api\validators\TotpValidator;
|
2016-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-05-11 01:10:06 +05:30
|
|
|
use api\traits\AccountFinder;
|
|
|
|
use common\components\UserFriendlyRandomKey;
|
|
|
|
use common\models\Account;
|
2016-05-11 01:58:04 +05:30
|
|
|
use common\models\confirmations\ForgotPassword;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\EmailActivation;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
|
|
|
class ForgotPasswordForm extends ApiForm {
|
|
|
|
use AccountFinder;
|
|
|
|
|
2017-04-18 21:38:11 +05:30
|
|
|
public $captcha;
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
public $login;
|
2017-04-18 21:38:11 +05:30
|
|
|
|
2017-01-24 02:20:13 +05:30
|
|
|
public $token;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
2017-04-18 21:38:11 +05:30
|
|
|
['captcha', ReCaptchaValidator::class],
|
2016-06-17 02:02:23 +05:30
|
|
|
['login', 'required', 'message' => E::LOGIN_REQUIRED],
|
2016-05-11 01:10:06 +05:30
|
|
|
['login', 'validateLogin'],
|
2017-01-24 02:20:13 +05:30
|
|
|
['token', 'required', 'when' => function(self $model) {
|
|
|
|
return !$this->hasErrors() && $model->getAccount()->is_otp_enabled;
|
|
|
|
}, 'message' => E::OTP_TOKEN_REQUIRED],
|
|
|
|
['token', 'validateTotpToken'],
|
2016-05-11 01:10:06 +05:30
|
|
|
['login', 'validateActivity'],
|
|
|
|
['login', 'validateFrequency'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateLogin($attribute) {
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
if ($this->getAccount() === null) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::LOGIN_NOT_EXIST);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:20:13 +05:30
|
|
|
public function validateTotpToken($attribute) {
|
|
|
|
if ($this->hasErrors()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
if (!$account->is_otp_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new TotpValidator(['account' => $account]);
|
2017-02-22 03:50:43 +05:30
|
|
|
$validator->window = 1;
|
2017-01-24 02:20:13 +05:30
|
|
|
$validator->validateAttribute($this, $attribute);
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
public function validateActivity($attribute) {
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
$account = $this->getAccount();
|
|
|
|
if ($account->status !== Account::STATUS_ACTIVE) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::ACCOUNT_NOT_ACTIVATED);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateFrequency($attribute) {
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
$emailConfirmation = $this->getEmailActivation();
|
|
|
|
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
|
2016-06-17 02:02:23 +05:30
|
|
|
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function forgotPassword() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
$emailActivation = $this->getEmailActivation();
|
|
|
|
if ($emailActivation === null) {
|
2016-05-11 01:58:04 +05:30
|
|
|
$emailActivation = new ForgotPassword();
|
2016-05-11 01:10:06 +05:30
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:29:24 +05:30
|
|
|
$account = $emailActivation->account;
|
|
|
|
$htmlBody = Yii::$app->emailRenderer->getTemplate('forgotPassword')
|
|
|
|
->setLocale($account->lang)
|
|
|
|
->setParams([
|
|
|
|
'username' => $account->username,
|
2016-10-04 04:15:50 +05:30
|
|
|
'code' => $emailActivation->key,
|
2016-10-03 04:29:24 +05:30
|
|
|
'link' => Yii::$app->request->getHostInfo() . '/recover-password/' . $emailActivation->key,
|
|
|
|
])
|
|
|
|
->render();
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
/** @var \yii\swiftmailer\Message $message */
|
2016-10-03 04:29:24 +05:30
|
|
|
$message = $mailer->compose()
|
|
|
|
->setHtmlBody($htmlBody)
|
|
|
|
->setTo([$account->email => $account->username])
|
2016-05-11 01:10:06 +05:30
|
|
|
->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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|