mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Реализованы формы для шагов смены E-mail адреса, покрыты unit-тестами
У EmailActivation добавлено поле $_data и дописано поведение для работы с ним Упрощено подключение фикстур для EmailActivations
This commit is contained in:
55
api/models/profile/ChangeEmail/ConfirmNewEmailForm.php
Normal file
55
api/models/profile/ChangeEmail/ConfirmNewEmailForm.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Exception;
|
||||
|
||||
class ConfirmNewEmailForm extends KeyConfirmationForm {
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount() {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function changeEmail() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
/** @var \common\models\confirmations\NewEmailConfirmation $activation */
|
||||
$activation = $this->getActivationCodeModel();
|
||||
$activation->delete();
|
||||
|
||||
$account = $this->getAccount();
|
||||
$account->email = $activation->newEmail;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Cannot save new account email value');
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
101
api/models/profile/ChangeEmail/InitStateForm.php
Normal file
101
api/models/profile/ChangeEmail/InitStateForm.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
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;
|
||||
|
||||
class InitStateForm extends ApiForm {
|
||||
|
||||
public $email;
|
||||
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
public function getAccount() {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
// TODO: поверить наличие уже отправленных подтверждений смены E-mail
|
||||
return [
|
||||
['!email', 'validateAccountPasswordHashStrategy'],
|
||||
];
|
||||
}
|
||||
|
||||
public function validateAccountPasswordHashStrategy($attribute) {
|
||||
$account = $this->getAccount();
|
||||
if ($account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY) {
|
||||
$this->addError($attribute, 'error.old_hash_strategy');
|
||||
}
|
||||
}
|
||||
|
||||
public function sendCurrentEmailConfirmation() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CurrentEmailConfirmation
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function createCode() {
|
||||
$account = $this->getAccount();
|
||||
$emailActivation = new CurrentEmailConfirmation();
|
||||
$emailActivation->account_id = $account->id;
|
||||
if (!$emailActivation->save()) {
|
||||
throw new ErrorException('Cannot save email activation model');
|
||||
}
|
||||
|
||||
return $emailActivation;
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
112
api/models/profile/ChangeEmail/NewEmailForm.php
Normal file
112
api/models/profile/ChangeEmail/NewEmailForm.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Exception;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class NewEmailForm extends KeyConfirmationForm {
|
||||
|
||||
public $email;
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount() {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
return array_merge(parent::rules(), [
|
||||
['email', 'required', 'message' => 'error.email_required'],
|
||||
['email', 'validateEmail'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function validateEmail() {
|
||||
$account = new Account();
|
||||
$account->email = $this->email;
|
||||
if (!$account->validate(['email'])) {
|
||||
$this->addErrors($account->getErrors());
|
||||
}
|
||||
}
|
||||
|
||||
public function sendNewEmailConfirmation() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$previousActivation = $this->getActivationCodeModel();
|
||||
$previousActivation->delete();
|
||||
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return NewEmailConfirmation
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function createCode() {
|
||||
$emailActivation = new NewEmailConfirmation();
|
||||
$emailActivation->account_id = $this->getAccount()->id;
|
||||
$emailActivation->newEmail = $this->email;
|
||||
if (!$emailActivation->save()) {
|
||||
throw new ErrorException('Cannot save email activation model');
|
||||
}
|
||||
|
||||
return $emailActivation;
|
||||
}
|
||||
|
||||
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/new-email-confirmation-html',
|
||||
'text' => '@app/mails/new-email-confirmation-text',
|
||||
], [
|
||||
'key' => $code->key,
|
||||
'account' => $acceptor,
|
||||
])
|
||||
->setTo([$this->email => $acceptor->username])
|
||||
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
||||
->setSubject('Ely.by Account new E-mail confirmation');
|
||||
|
||||
if (!$message->send()) {
|
||||
throw new ErrorException('Unable send email with activation code.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user