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
|
|
|
|
|
|
|
use api\models\base\ApiForm;
|
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;
|
|
|
|
|
|
|
|
public $login;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
2016-06-17 02:02:23 +05:30
|
|
|
['login', 'required', 'message' => E::LOGIN_REQUIRED],
|
2016-05-11 01:10:06 +05:30
|
|
|
['login', 'validateLogin'],
|
|
|
|
['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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|