2016-05-16 04:03:19 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace api\models\profile\ChangeEmail;
|
|
|
|
|
|
2016-05-24 01:25:36 +05:30
|
|
|
|
use api\models\base\PasswordProtectedForm;
|
2016-07-17 23:16:04 +05:30
|
|
|
|
use common\helpers\Error as E;
|
2016-05-16 04:03:19 +05:30
|
|
|
|
use common\models\Account;
|
|
|
|
|
use common\models\confirmations\CurrentEmailConfirmation;
|
|
|
|
|
use common\models\EmailActivation;
|
|
|
|
|
use Yii;
|
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
use yii\base\Exception;
|
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
|
2016-05-24 01:25:36 +05:30
|
|
|
|
class InitStateForm extends PasswordProtectedForm {
|
2016-05-16 04:03:19 +05:30
|
|
|
|
|
|
|
|
|
public $email;
|
|
|
|
|
|
|
|
|
|
private $account;
|
|
|
|
|
|
|
|
|
|
public function __construct(Account $account, array $config = []) {
|
|
|
|
|
$this->account = $account;
|
2016-07-17 23:16:04 +05:30
|
|
|
|
$this->email = $account->email;
|
2016-05-16 04:03:19 +05:30
|
|
|
|
parent::__construct($config);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
|
public function getAccount() : Account {
|
2016-05-16 04:03:19 +05:30
|
|
|
|
return $this->account;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function rules() {
|
2016-05-23 00:06:02 +05:30
|
|
|
|
return array_merge(parent::rules(), [
|
2016-07-17 23:16:04 +05:30
|
|
|
|
['email', 'validateFrequency'],
|
2016-05-24 01:25:36 +05:30
|
|
|
|
]);
|
2016-05-16 04:03:19 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 23:16:04 +05:30
|
|
|
|
public function validateFrequency($attribute) {
|
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
|
$emailConfirmation = $this->getEmailActivation();
|
|
|
|
|
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
|
|
|
|
|
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
|
public function sendCurrentEmailConfirmation() : bool {
|
2016-05-16 04:03:19 +05:30
|
|
|
|
if (!$this->validate()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
try {
|
2016-07-17 23:16:04 +05:30
|
|
|
|
$this->removeOldCode();
|
2016-05-16 04:03:19 +05:30
|
|
|
|
$activation = $this->createCode();
|
|
|
|
|
$this->sendCode($activation);
|
|
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
$transaction->rollBack();
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return CurrentEmailConfirmation
|
|
|
|
|
* @throws ErrorException
|
|
|
|
|
*/
|
2016-06-17 02:02:23 +05:30
|
|
|
|
public function createCode() : CurrentEmailConfirmation {
|
2016-05-16 04:03:19 +05:30
|
|
|
|
$account = $this->getAccount();
|
|
|
|
|
$emailActivation = new CurrentEmailConfirmation();
|
|
|
|
|
$emailActivation->account_id = $account->id;
|
|
|
|
|
if (!$emailActivation->save()) {
|
|
|
|
|
throw new ErrorException('Cannot save email activation model');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $emailActivation;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 23:16:04 +05:30
|
|
|
|
/**
|
|
|
|
|
* Удаляет старый ключ активации, если он существует
|
|
|
|
|
*/
|
|
|
|
|
public function removeOldCode() {
|
|
|
|
|
$emailActivation = $this->getEmailActivation();
|
|
|
|
|
if ($emailActivation === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$emailActivation->delete();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 04:03:19 +05:30
|
|
|
|
public function sendCode(EmailActivation $code) {
|
|
|
|
|
/** @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 = $code->account;
|
|
|
|
|
/** @var \yii\swiftmailer\Message $message */
|
|
|
|
|
$message = $mailer->compose([
|
|
|
|
|
'html' => '@app/mails/current-email-confirmation-html',
|
|
|
|
|
'text' => '@app/mails/current-email-confirmation-text',
|
|
|
|
|
], [
|
|
|
|
|
'key' => $code->key,
|
|
|
|
|
])
|
|
|
|
|
->setTo([$acceptor->email => $acceptor->username])
|
|
|
|
|
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
|
|
|
|
->setSubject('Ely.by Account change E-mail confirmation');
|
|
|
|
|
|
|
|
|
|
if (!$message->send()) {
|
|
|
|
|
throw new ErrorException('Unable send email with activation code.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 23:16:04 +05:30
|
|
|
|
/**
|
|
|
|
|
* Возвращает E-mail активацию, которая использовалась внутри процесса для перехода на следующий шаг.
|
|
|
|
|
* Метод предназначен для проверки, не слишком ли часто отправляются письма о смене E-mail.
|
|
|
|
|
* Проверяем тип подтверждения нового E-mail, поскольку при переходе на этот этап, активация предыдущего
|
|
|
|
|
* шага удаляется.
|
|
|
|
|
* @return EmailActivation|null
|
|
|
|
|
* @throws ErrorException
|
|
|
|
|
*/
|
|
|
|
|
public function getEmailActivation() {
|
|
|
|
|
return $this->getAccount()
|
|
|
|
|
->getEmailActivations()
|
|
|
|
|
->andWhere([
|
|
|
|
|
'type' => [
|
|
|
|
|
EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
|
|
|
|
|
EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
|
|
|
|
],
|
|
|
|
|
])
|
|
|
|
|
->one();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 04:03:19 +05:30
|
|
|
|
}
|