Добавлена команда для зачистки хранилища ключей активации по E-mail

This commit is contained in:
ErickSkrauch 2017-03-07 20:14:54 +03:00
parent f9842acf29
commit fe03bedc3a
5 changed files with 65 additions and 0 deletions

View File

@ -1,8 +1,45 @@
<?php <?php
namespace console\controllers; namespace console\controllers;
use common\models\EmailActivation;
use yii\console\Controller; use yii\console\Controller;
class CleanupController extends Controller { class CleanupController extends Controller {
public function actionEmailKeys() {
$query = EmailActivation::find();
$conditions = ['OR'];
foreach ($this->getEmailActivationsDurationsMap() as $typeId => $expiration) {
$conditions[] = [
'AND',
['type' => $typeId],
['<', 'created_at', time() - $expiration],
];
}
/** @var EmailActivation[] $expiredEmails */
$expiredEmails = $query->andWhere($conditions)->all();
foreach ($expiredEmails as $email) {
$email->delete();
}
return self::EXIT_CODE_NORMAL;
}
private function getEmailActivationsDurationsMap(): array {
$durationsMap = [];
foreach (EmailActivation::getClassMap() as $typeId => $className) {
/** @var EmailActivation $object */
$object = new $className;
/** @var \common\behaviors\EmailActivationExpirationBehavior $behavior */
$behavior = $object->getBehavior('expirationBehavior');
$expiration = $behavior->expirationTimeout ?? 1123200; // 13d по умолчанию
// Приращаем 1 день, чтобы пользователи ещё могли получать сообщения об истечении кода активации
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
$durationsMap[$typeId] = $expiration + 86400;
}
return $durationsMap;
}
} }

View File

2
docker/cron/cleanup Normal file
View File

@ -0,0 +1,2 @@
# https://crontab.guru/every-day
0 0 * * * root /usr/local/bin/php /var/www/html/yii cleanup/email-keys >/dev/null 2>&1

View File

@ -37,4 +37,10 @@ return [
'_data' => serialize(['newEmail' => 'my-new-email@ely.by']), '_data' => serialize(['newEmail' => 'my-new-email@ely.by']),
'created_at' => time() - 10, 'created_at' => time() - 10,
], ],
'deeplyExpiredConfirmation' => [
'key' => 'H29HBDCHHAG2HGHGHS',
'account_id' => 1,
'type' => \common\models\EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
'created_at' => 1487695872,
],
]; ];

View File

@ -1,8 +1,28 @@
<?php <?php
namespace codeception\console\unit\controllers; namespace codeception\console\unit\controllers;
use common\models\EmailActivation;
use console\controllers\CleanupController;
use tests\codeception\common\fixtures\EmailActivationFixture;
use tests\codeception\console\unit\TestCase; use tests\codeception\console\unit\TestCase;
use Yii;
class CleanupControllerTest extends TestCase { class CleanupControllerTest extends TestCase {
public function _fixtures() {
return [
'emailActivations' => EmailActivationFixture::class,
];
}
public function testActionAccessTokens() {
/** @var EmailActivation $expiredConfirmation */
$expiredConfirmation = $this->tester->grabFixture('emailActivations', 'deeplyExpiredConfirmation');
$controller = new CleanupController('cleanup', Yii::$app);
$this->assertEquals(0, $controller->actionEmailKeys());
$this->tester->cantSeeRecord(EmailActivation::className(), ['key' => $expiredConfirmation->key]);
}
} }