mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
При попытке запроса смены E-mail теперь происходит проверка, как давно был выполнен предыдущий запрос
This commit is contained in:
@@ -20,6 +20,9 @@ use yii\web\User as YiiUserComponent;
|
||||
|
||||
/**
|
||||
* @property AccountSession|null $activeSession
|
||||
* @property AccountIdentity|null $identity
|
||||
*
|
||||
* @method AccountIdentity|null getIdentity()
|
||||
*/
|
||||
class Component extends YiiUserComponent {
|
||||
|
||||
|
@@ -7,6 +7,7 @@ use api\models\profile\ChangeEmail\NewEmailForm;
|
||||
use api\models\profile\ChangeLanguageForm;
|
||||
use api\models\profile\ChangePasswordForm;
|
||||
use api\models\profile\ChangeUsernameForm;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
use yii\filters\AccessControl;
|
||||
@@ -59,7 +60,6 @@ class AccountsController extends Controller {
|
||||
}
|
||||
|
||||
public function actionCurrent() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
|
||||
return [
|
||||
@@ -76,7 +76,6 @@ class AccountsController extends Controller {
|
||||
}
|
||||
|
||||
public function actionChangePassword() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new ChangePasswordForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
@@ -108,15 +107,24 @@ class AccountsController extends Controller {
|
||||
}
|
||||
|
||||
public function actionChangeEmailInitialize() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new InitStateForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
if (!$model->sendCurrentEmailConfirmation()) {
|
||||
return [
|
||||
$data = [
|
||||
'success' => false,
|
||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
||||
];
|
||||
|
||||
if (ArrayHelper::getValue($data['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
||||
$emailActivation = $model->getEmailActivation();
|
||||
$data['data'] = [
|
||||
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
||||
'repeatFrequency' => $emailActivation->repeatTimeout,
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return [
|
||||
@@ -125,7 +133,6 @@ class AccountsController extends Controller {
|
||||
}
|
||||
|
||||
public function actionChangeEmailSubmitNewEmail() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new NewEmailForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
@@ -142,7 +149,6 @@ class AccountsController extends Controller {
|
||||
}
|
||||
|
||||
public function actionChangeEmailConfirmNewEmail() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new ConfirmNewEmailForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
@@ -162,7 +168,6 @@ class AccountsController extends Controller {
|
||||
}
|
||||
|
||||
public function actionChangeLang() {
|
||||
/** @var Account $account */
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new ChangeLanguageForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
|
@@ -2,12 +2,9 @@
|
||||
namespace api\controllers;
|
||||
|
||||
use api\traits\ApiNormalize;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
|
||||
/**
|
||||
* @property \common\models\Account|null $account
|
||||
*
|
||||
* Поведения:
|
||||
* @mixin \yii\filters\ContentNegotiator
|
||||
* @mixin \yii\filters\VerbFilter
|
||||
@@ -31,11 +28,4 @@ class Controller extends \yii\rest\Controller {
|
||||
return $parentBehaviors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \common\models\Account|null
|
||||
*/
|
||||
public function getAccount() {
|
||||
return Yii::$app->getUser()->getIdentity();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -116,7 +116,7 @@ class OauthController extends Controller {
|
||||
$grant = $this->getGrantType();
|
||||
try {
|
||||
$authParams = $grant->checkAuthorizeParams();
|
||||
$account = $this->getAccount();
|
||||
$account = Yii::$app->user->identity;
|
||||
/** @var \League\OAuth2\Server\Entity\ClientEntity $client */
|
||||
$client = $authParams['client'];
|
||||
/** @var \common\models\OauthClient $clientModel */
|
||||
|
@@ -2,6 +2,7 @@
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\PasswordProtectedForm;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
@@ -18,6 +19,7 @@ class InitStateForm extends PasswordProtectedForm {
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
$this->email = $account->email;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
@@ -26,12 +28,20 @@ class InitStateForm extends PasswordProtectedForm {
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
// TODO: поверить наличие уже отправленных подтверждений смены E-mail
|
||||
return array_merge(parent::rules(), [
|
||||
|
||||
['email', 'validateFrequency'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function validateFrequency($attribute) {
|
||||
if (!$this->hasErrors()) {
|
||||
$emailConfirmation = $this->getEmailActivation();
|
||||
if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) {
|
||||
$this->addError($attribute, E::RECENTLY_SENT_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function sendCurrentEmailConfirmation() : bool {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
@@ -39,6 +49,7 @@ class InitStateForm extends PasswordProtectedForm {
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$this->removeOldCode();
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
|
||||
@@ -66,6 +77,18 @@ class InitStateForm extends PasswordProtectedForm {
|
||||
return $emailActivation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Удаляет старый ключ активации, если он существует
|
||||
*/
|
||||
public function removeOldCode() {
|
||||
$emailActivation = $this->getEmailActivation();
|
||||
if ($emailActivation === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$emailActivation->delete();
|
||||
}
|
||||
|
||||
public function sendCode(EmailActivation $code) {
|
||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||
$mailer = Yii::$app->mailer;
|
||||
@@ -91,4 +114,24 @@ class InitStateForm extends PasswordProtectedForm {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user