mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 02:32:26 +05:30
При попытке запроса смены E-mail теперь происходит проверка, как давно был выполнен предыдущий запрос
This commit is contained in:
parent
e756dbacd6
commit
681996740d
@ -20,6 +20,9 @@ use yii\web\User as YiiUserComponent;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @property AccountSession|null $activeSession
|
* @property AccountSession|null $activeSession
|
||||||
|
* @property AccountIdentity|null $identity
|
||||||
|
*
|
||||||
|
* @method AccountIdentity|null getIdentity()
|
||||||
*/
|
*/
|
||||||
class Component extends YiiUserComponent {
|
class Component extends YiiUserComponent {
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use api\models\profile\ChangeEmail\NewEmailForm;
|
|||||||
use api\models\profile\ChangeLanguageForm;
|
use api\models\profile\ChangeLanguageForm;
|
||||||
use api\models\profile\ChangePasswordForm;
|
use api\models\profile\ChangePasswordForm;
|
||||||
use api\models\profile\ChangeUsernameForm;
|
use api\models\profile\ChangeUsernameForm;
|
||||||
|
use common\helpers\Error as E;
|
||||||
use common\models\Account;
|
use common\models\Account;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\filters\AccessControl;
|
use yii\filters\AccessControl;
|
||||||
@ -59,7 +60,6 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionCurrent() {
|
public function actionCurrent() {
|
||||||
/** @var Account $account */
|
|
||||||
$account = Yii::$app->user->identity;
|
$account = Yii::$app->user->identity;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@ -76,7 +76,6 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionChangePassword() {
|
public function actionChangePassword() {
|
||||||
/** @var Account $account */
|
|
||||||
$account = Yii::$app->user->identity;
|
$account = Yii::$app->user->identity;
|
||||||
$model = new ChangePasswordForm($account);
|
$model = new ChangePasswordForm($account);
|
||||||
$model->load(Yii::$app->request->post());
|
$model->load(Yii::$app->request->post());
|
||||||
@ -108,15 +107,24 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionChangeEmailInitialize() {
|
public function actionChangeEmailInitialize() {
|
||||||
/** @var Account $account */
|
|
||||||
$account = Yii::$app->user->identity;
|
$account = Yii::$app->user->identity;
|
||||||
$model = new InitStateForm($account);
|
$model = new InitStateForm($account);
|
||||||
$model->load(Yii::$app->request->post());
|
$model->load(Yii::$app->request->post());
|
||||||
if (!$model->sendCurrentEmailConfirmation()) {
|
if (!$model->sendCurrentEmailConfirmation()) {
|
||||||
return [
|
$data = [
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
'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 [
|
return [
|
||||||
@ -125,7 +133,6 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionChangeEmailSubmitNewEmail() {
|
public function actionChangeEmailSubmitNewEmail() {
|
||||||
/** @var Account $account */
|
|
||||||
$account = Yii::$app->user->identity;
|
$account = Yii::$app->user->identity;
|
||||||
$model = new NewEmailForm($account);
|
$model = new NewEmailForm($account);
|
||||||
$model->load(Yii::$app->request->post());
|
$model->load(Yii::$app->request->post());
|
||||||
@ -142,7 +149,6 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionChangeEmailConfirmNewEmail() {
|
public function actionChangeEmailConfirmNewEmail() {
|
||||||
/** @var Account $account */
|
|
||||||
$account = Yii::$app->user->identity;
|
$account = Yii::$app->user->identity;
|
||||||
$model = new ConfirmNewEmailForm($account);
|
$model = new ConfirmNewEmailForm($account);
|
||||||
$model->load(Yii::$app->request->post());
|
$model->load(Yii::$app->request->post());
|
||||||
@ -162,7 +168,6 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionChangeLang() {
|
public function actionChangeLang() {
|
||||||
/** @var Account $account */
|
|
||||||
$account = Yii::$app->user->identity;
|
$account = Yii::$app->user->identity;
|
||||||
$model = new ChangeLanguageForm($account);
|
$model = new ChangeLanguageForm($account);
|
||||||
$model->load(Yii::$app->request->post());
|
$model->load(Yii::$app->request->post());
|
||||||
|
@ -2,12 +2,9 @@
|
|||||||
namespace api\controllers;
|
namespace api\controllers;
|
||||||
|
|
||||||
use api\traits\ApiNormalize;
|
use api\traits\ApiNormalize;
|
||||||
use Yii;
|
|
||||||
use yii\filters\auth\HttpBearerAuth;
|
use yii\filters\auth\HttpBearerAuth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property \common\models\Account|null $account
|
|
||||||
*
|
|
||||||
* Поведения:
|
* Поведения:
|
||||||
* @mixin \yii\filters\ContentNegotiator
|
* @mixin \yii\filters\ContentNegotiator
|
||||||
* @mixin \yii\filters\VerbFilter
|
* @mixin \yii\filters\VerbFilter
|
||||||
@ -31,11 +28,4 @@ class Controller extends \yii\rest\Controller {
|
|||||||
return $parentBehaviors;
|
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();
|
$grant = $this->getGrantType();
|
||||||
try {
|
try {
|
||||||
$authParams = $grant->checkAuthorizeParams();
|
$authParams = $grant->checkAuthorizeParams();
|
||||||
$account = $this->getAccount();
|
$account = Yii::$app->user->identity;
|
||||||
/** @var \League\OAuth2\Server\Entity\ClientEntity $client */
|
/** @var \League\OAuth2\Server\Entity\ClientEntity $client */
|
||||||
$client = $authParams['client'];
|
$client = $authParams['client'];
|
||||||
/** @var \common\models\OauthClient $clientModel */
|
/** @var \common\models\OauthClient $clientModel */
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
namespace api\models\profile\ChangeEmail;
|
namespace api\models\profile\ChangeEmail;
|
||||||
|
|
||||||
use api\models\base\PasswordProtectedForm;
|
use api\models\base\PasswordProtectedForm;
|
||||||
|
use common\helpers\Error as E;
|
||||||
use common\models\Account;
|
use common\models\Account;
|
||||||
use common\models\confirmations\CurrentEmailConfirmation;
|
use common\models\confirmations\CurrentEmailConfirmation;
|
||||||
use common\models\EmailActivation;
|
use common\models\EmailActivation;
|
||||||
@ -18,6 +19,7 @@ class InitStateForm extends PasswordProtectedForm {
|
|||||||
|
|
||||||
public function __construct(Account $account, array $config = []) {
|
public function __construct(Account $account, array $config = []) {
|
||||||
$this->account = $account;
|
$this->account = $account;
|
||||||
|
$this->email = $account->email;
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,12 +28,20 @@ class InitStateForm extends PasswordProtectedForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function rules() {
|
public function rules() {
|
||||||
// TODO: поверить наличие уже отправленных подтверждений смены E-mail
|
|
||||||
return array_merge(parent::rules(), [
|
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 {
|
public function sendCurrentEmailConfirmation() : bool {
|
||||||
if (!$this->validate()) {
|
if (!$this->validate()) {
|
||||||
return false;
|
return false;
|
||||||
@ -39,6 +49,7 @@ class InitStateForm extends PasswordProtectedForm {
|
|||||||
|
|
||||||
$transaction = Yii::$app->db->beginTransaction();
|
$transaction = Yii::$app->db->beginTransaction();
|
||||||
try {
|
try {
|
||||||
|
$this->removeOldCode();
|
||||||
$activation = $this->createCode();
|
$activation = $this->createCode();
|
||||||
$this->sendCode($activation);
|
$this->sendCode($activation);
|
||||||
|
|
||||||
@ -66,6 +77,18 @@ class InitStateForm extends PasswordProtectedForm {
|
|||||||
return $emailActivation;
|
return $emailActivation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Удаляет старый ключ активации, если он существует
|
||||||
|
*/
|
||||||
|
public function removeOldCode() {
|
||||||
|
$emailActivation = $this->getEmailActivation();
|
||||||
|
if ($emailActivation === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$emailActivation->delete();
|
||||||
|
}
|
||||||
|
|
||||||
public function sendCode(EmailActivation $code) {
|
public function sendCode(EmailActivation $code) {
|
||||||
/** @var \yii\swiftmailer\Mailer $mailer */
|
/** @var \yii\swiftmailer\Mailer $mailer */
|
||||||
$mailer = Yii::$app->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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,8 @@ abstract class BaseApplication extends yii\base\Application {
|
|||||||
* @property \api\components\User\Component $user User component.
|
* @property \api\components\User\Component $user User component.
|
||||||
* @property \api\components\ReCaptcha\Component $reCaptcha
|
* @property \api\components\ReCaptcha\Component $reCaptcha
|
||||||
* @property \common\components\oauth\Component $oauth
|
* @property \common\components\oauth\Component $oauth
|
||||||
|
*
|
||||||
|
* @method \api\components\User\Component getUser()
|
||||||
*/
|
*/
|
||||||
class WebApplication extends yii\web\Application {
|
class WebApplication extends yii\web\Application {
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ namespace common\models;
|
|||||||
use common\behaviors\DataBehavior;
|
use common\behaviors\DataBehavior;
|
||||||
use common\behaviors\EmailActivationExpirationBehavior;
|
use common\behaviors\EmailActivationExpirationBehavior;
|
||||||
use common\components\UserFriendlyRandomKey;
|
use common\components\UserFriendlyRandomKey;
|
||||||
use Yii;
|
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\behaviors\TimestampBehavior;
|
use yii\behaviors\TimestampBehavior;
|
||||||
use yii\db\ActiveRecord;
|
use yii\db\ActiveRecord;
|
||||||
@ -45,7 +44,7 @@ class EmailActivation extends ActiveRecord {
|
|||||||
],
|
],
|
||||||
'expirationBehavior' => [
|
'expirationBehavior' => [
|
||||||
'class' => EmailActivationExpirationBehavior::class,
|
'class' => EmailActivationExpirationBehavior::class,
|
||||||
'repeatTimeout' => 5 * 60,
|
'repeatTimeout' => 5 * 60, // 5m
|
||||||
'expirationTimeout' => -1,
|
'expirationTimeout' => -1,
|
||||||
],
|
],
|
||||||
'dataBehavior' => [
|
'dataBehavior' => [
|
||||||
|
@ -9,8 +9,8 @@ class CurrentEmailConfirmation extends EmailActivation {
|
|||||||
public function behaviors() {
|
public function behaviors() {
|
||||||
return ArrayHelper::merge(parent::behaviors(), [
|
return ArrayHelper::merge(parent::behaviors(), [
|
||||||
'expirationBehavior' => [
|
'expirationBehavior' => [
|
||||||
'repeatTimeout' => 6 * 60 * 60,
|
'repeatTimeout' => 6 * 60 * 60, // 6h
|
||||||
'expirationTimeout' => 1 * 60 * 60,
|
'expirationTimeout' => 1 * 60 * 60, // 1h
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace tests\codeception\api\functional;
|
namespace tests\codeception\api\functional;
|
||||||
|
|
||||||
use Codeception\Specify;
|
|
||||||
use tests\codeception\api\_pages\AccountsRoute;
|
use tests\codeception\api\_pages\AccountsRoute;
|
||||||
use tests\codeception\api\FunctionalTester;
|
use tests\codeception\api\FunctionalTester;
|
||||||
|
|
||||||
@ -28,4 +27,19 @@ class AccountsChangeEmailInitializeCest {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testChangeEmailInitializeFrequencyError(FunctionalTester $I) {
|
||||||
|
$I->wantTo('see change email request frequency error');
|
||||||
|
$I->loggedInAsActiveAccount('ILLIMUNATI', 'password_0');
|
||||||
|
|
||||||
|
$this->route->changeEmailInitialize('password_0');
|
||||||
|
$I->canSeeResponseContainsJson([
|
||||||
|
'success' => false,
|
||||||
|
'errors' => [
|
||||||
|
'email' => 'error.recently_sent_message',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
$I->canSeeResponseJsonMatchesJsonPath('$.data.canRepeatIn');
|
||||||
|
$I->canSeeResponseJsonMatchesJsonPath('$.data.repeatFrequency');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -62,14 +62,15 @@ class NewEmailFormTest extends DbTestCase {
|
|||||||
public function testSendNewEmailConfirmation() {
|
public function testSendNewEmailConfirmation() {
|
||||||
$this->specify('send email', function() {
|
$this->specify('send email', function() {
|
||||||
/** @var Account $account */
|
/** @var Account $account */
|
||||||
$account = Account::findOne($this->accounts['admin']['id']);
|
$account = Account::findOne($this->accounts['account-with-change-email-init-state']['id']);
|
||||||
/** @var NewEmailForm $model */
|
/** @var NewEmailForm $model */
|
||||||
|
$key = $this->emailActivations['currentChangeEmailConfirmation']['key'];
|
||||||
$model = new NewEmailForm($account, [
|
$model = new NewEmailForm($account, [
|
||||||
'key' => $this->emailActivations['currentEmailConfirmation']['key'],
|
'key' => $key,
|
||||||
'email' => 'my-new-email@ely.by',
|
'email' => 'my-new-email@ely.by',
|
||||||
]);
|
]);
|
||||||
expect($model->sendNewEmailConfirmation())->true();
|
expect($model->sendNewEmailConfirmation())->true();
|
||||||
expect(EmailActivation::findOne($this->emailActivations['currentEmailConfirmation']['key']))->null();
|
expect(EmailActivation::findOne($key))->null();
|
||||||
expect(EmailActivation::findOne([
|
expect(EmailActivation::findOne([
|
||||||
'account_id' => $account->id,
|
'account_id' => $account->id,
|
||||||
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||||
|
@ -24,12 +24,6 @@ return [
|
|||||||
'type' => \common\models\EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
|
'type' => \common\models\EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
|
||||||
'created_at' => time() - (new \common\models\confirmations\ForgotPassword())->repeatTimeout - 10,
|
'created_at' => time() - (new \common\models\confirmations\ForgotPassword())->repeatTimeout - 10,
|
||||||
],
|
],
|
||||||
'currentEmailConfirmation' => [
|
|
||||||
'key' => 'H26HBDCHHAG2HGHGHS',
|
|
||||||
'account_id' => 1,
|
|
||||||
'type' => \common\models\EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
|
|
||||||
'created_at' => time() - 10,
|
|
||||||
],
|
|
||||||
'currentChangeEmailConfirmation' => [
|
'currentChangeEmailConfirmation' => [
|
||||||
'key' => 'H27HBDCHHAG2HGHGHS',
|
'key' => 'H27HBDCHHAG2HGHGHS',
|
||||||
'account_id' => 7,
|
'account_id' => 7,
|
||||||
|
Loading…
Reference in New Issue
Block a user