diff --git a/console/controllers/CleanupController.php b/console/controllers/CleanupController.php index 0f8cb83..33e9293 100644 --- a/console/controllers/CleanupController.php +++ b/console/controllers/CleanupController.php @@ -1,8 +1,45 @@ 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; + } + } diff --git a/docker/cron/.gitkeep b/docker/cron/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/docker/cron/cleanup b/docker/cron/cleanup new file mode 100644 index 0000000..b4aa202 --- /dev/null +++ b/docker/cron/cleanup @@ -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 diff --git a/tests/codeception/common/fixtures/data/email-activations.php b/tests/codeception/common/fixtures/data/email-activations.php index cf79bff..c2e3213 100644 --- a/tests/codeception/common/fixtures/data/email-activations.php +++ b/tests/codeception/common/fixtures/data/email-activations.php @@ -37,4 +37,10 @@ return [ '_data' => serialize(['newEmail' => 'my-new-email@ely.by']), 'created_at' => time() - 10, ], + 'deeplyExpiredConfirmation' => [ + 'key' => 'H29HBDCHHAG2HGHGHS', + 'account_id' => 1, + 'type' => \common\models\EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION, + 'created_at' => 1487695872, + ], ]; diff --git a/tests/codeception/console/unit/controllers/CleanupControllerTest.php b/tests/codeception/console/unit/controllers/CleanupControllerTest.php index d13e470..04e2d51 100644 --- a/tests/codeception/console/unit/controllers/CleanupControllerTest.php +++ b/tests/codeception/console/unit/controllers/CleanupControllerTest.php @@ -1,8 +1,28 @@ 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]); + } + }