2016-11-24 02:11:33 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace console\controllers;
|
|
|
|
|
|
2017-09-29 04:34:16 +05:30
|
|
|
|
use common\models\AccountSession;
|
2017-03-07 22:44:54 +05:30
|
|
|
|
use common\models\EmailActivation;
|
2017-09-27 22:22:28 +05:30
|
|
|
|
use common\models\MinecraftAccessKey;
|
2017-11-14 22:19:51 +05:30
|
|
|
|
use Generator;
|
2016-11-24 02:11:33 +05:30
|
|
|
|
use yii\console\Controller;
|
2017-11-14 22:19:51 +05:30
|
|
|
|
use yii\db\ActiveQueryInterface;
|
2016-11-24 02:11:33 +05:30
|
|
|
|
|
|
|
|
|
class CleanupController extends Controller {
|
|
|
|
|
|
2017-03-07 22:44:54 +05:30
|
|
|
|
public function actionEmailKeys() {
|
|
|
|
|
$query = EmailActivation::find();
|
|
|
|
|
foreach ($this->getEmailActivationsDurationsMap() as $typeId => $expiration) {
|
2017-11-14 22:19:51 +05:30
|
|
|
|
$query->orWhere([
|
2017-03-07 22:44:54 +05:30
|
|
|
|
'AND',
|
|
|
|
|
['type' => $typeId],
|
|
|
|
|
['<', 'created_at', time() - $expiration],
|
2017-11-14 22:19:51 +05:30
|
|
|
|
]);
|
2017-03-07 22:44:54 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 22:19:51 +05:30
|
|
|
|
foreach ($this->each($query) as $email) {
|
|
|
|
|
/** @var EmailActivation $email */
|
2017-03-07 22:44:54 +05:30
|
|
|
|
$email->delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::EXIT_CODE_NORMAL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 22:22:28 +05:30
|
|
|
|
public function actionMinecraftSessions() {
|
2017-11-14 22:19:51 +05:30
|
|
|
|
$expiredMinecraftSessionsQuery = MinecraftAccessKey::find()
|
|
|
|
|
->andWhere(['<', 'updated_at', time() - 1209600]); // 2 weeks
|
2017-09-27 22:22:28 +05:30
|
|
|
|
|
2017-11-14 22:19:51 +05:30
|
|
|
|
foreach ($this->each($expiredMinecraftSessionsQuery) as $minecraftSession) {
|
|
|
|
|
/** @var MinecraftAccessKey $minecraftSession */
|
2017-09-27 22:22:28 +05:30
|
|
|
|
$minecraftSession->delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::EXIT_CODE_NORMAL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 04:34:16 +05:30
|
|
|
|
/**
|
|
|
|
|
* Нужно удалить те сессии, которые не рефрешились 90 дней,
|
|
|
|
|
* а также сессии, которые ни разу не рефрешились с момента своей выписки
|
|
|
|
|
* более чем 2 недели назад.
|
|
|
|
|
*
|
|
|
|
|
* У модели AccountSession нет внешних связей, так что целевые записи
|
|
|
|
|
* могут быть удалены без использования циклов.
|
|
|
|
|
*/
|
|
|
|
|
public function actionWebSessions() {
|
|
|
|
|
AccountSession::deleteAll([
|
|
|
|
|
'OR',
|
|
|
|
|
['<', 'last_refreshed_at', time() - 7776000], // 90 days
|
|
|
|
|
[
|
|
|
|
|
'AND',
|
|
|
|
|
'created_at = last_refreshed_at',
|
|
|
|
|
['<', 'created_at', time() - 1209600], // 2 weeks
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return self::EXIT_CODE_NORMAL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 22:19:51 +05:30
|
|
|
|
/**
|
|
|
|
|
* Each function implementation, that allows you to iterate over values,
|
|
|
|
|
* when in each iteration row removing from database. If you do not remove
|
|
|
|
|
* value in iteration, then this will cause infinite loop.
|
|
|
|
|
*
|
|
|
|
|
* @param ActiveQueryInterface $query
|
|
|
|
|
* @param int $size
|
|
|
|
|
*
|
|
|
|
|
* @return Generator
|
|
|
|
|
*/
|
|
|
|
|
private function each(ActiveQueryInterface $query, int $size = 100): Generator {
|
|
|
|
|
$query = clone $query;
|
|
|
|
|
$query->limit($size);
|
|
|
|
|
while (true) {
|
|
|
|
|
$rows = $query->all();
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
yield $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 22:44:54 +05:30
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 02:11:33 +05:30
|
|
|
|
}
|