2016-11-24 02:11:33 +05:30
|
|
|
<?php
|
|
|
|
namespace console\controllers;
|
|
|
|
|
2019-09-02 01:46:13 +05:30
|
|
|
use Carbon\Carbon;
|
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;
|
2018-02-28 03:57:35 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\tasks\ClearOauthSessions;
|
2017-11-15 02:33:38 +05:30
|
|
|
use Yii;
|
2016-11-24 02:11:33 +05:30
|
|
|
use yii\console\Controller;
|
2018-02-28 03:57:35 +05:30
|
|
|
use yii\console\ExitCode;
|
2016-11-24 02:11:33 +05:30
|
|
|
|
|
|
|
class CleanupController extends Controller {
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
public function actionEmailKeys(): int {
|
2017-03-07 22:44:54 +05:30
|
|
|
$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-15 02:33:38 +05:30
|
|
|
foreach ($query->each(100, Yii::$app->unbufferedDb) as $email) {
|
2017-11-14 22:19:51 +05:30
|
|
|
/** @var EmailActivation $email */
|
2017-03-07 22:44:54 +05:30
|
|
|
$email->delete();
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
return ExitCode::OK;
|
2017-03-07 22:44:54 +05:30
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
public function actionMinecraftSessions(): int {
|
2017-11-14 22:19:51 +05:30
|
|
|
$expiredMinecraftSessionsQuery = MinecraftAccessKey::find()
|
2019-09-02 01:46:13 +05:30
|
|
|
->andWhere(['<', 'updated_at', Carbon::now()->subMonths(3)->getTimestamp()]);
|
2017-09-27 22:22:28 +05:30
|
|
|
|
2017-11-15 02:33:38 +05:30
|
|
|
foreach ($expiredMinecraftSessionsQuery->each(100, Yii::$app->unbufferedDb) as $minecraftSession) {
|
2017-11-14 22:19:51 +05:30
|
|
|
/** @var MinecraftAccessKey $minecraftSession */
|
2017-09-27 22:22:28 +05:30
|
|
|
$minecraftSession->delete();
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
return ExitCode::OK;
|
2017-09-27 22:22:28 +05:30
|
|
|
}
|
|
|
|
|
2017-09-29 04:34:16 +05:30
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* Sessions that have not been refreshed for 90 days and those
|
|
|
|
* that have not been refreshed since they were issued more than 2 weeks ago
|
|
|
|
* should be deleted.
|
2017-09-29 04:34:16 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* The AccountSession model doesn't have any relations,
|
|
|
|
* so the records can be deleted just with mass delete operation.
|
2017-09-29 04:34:16 +05:30
|
|
|
*/
|
2018-02-28 03:57:35 +05:30
|
|
|
public function actionWebSessions(): int {
|
2017-09-29 04:34:16 +05:30
|
|
|
AccountSession::deleteAll([
|
|
|
|
'OR',
|
|
|
|
['<', 'last_refreshed_at', time() - 7776000], // 90 days
|
|
|
|
[
|
|
|
|
'AND',
|
|
|
|
'created_at = last_refreshed_at',
|
|
|
|
['<', 'created_at', time() - 1209600], // 2 weeks
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
return ExitCode::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionOauthClients(): int {
|
|
|
|
/** @var OauthClient[] $clients */
|
|
|
|
$clients = OauthClient::find()
|
|
|
|
->onlyDeleted()
|
|
|
|
->all();
|
|
|
|
foreach ($clients as $client) {
|
|
|
|
if ($client->getSessions()->exists()) {
|
|
|
|
Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExitCode::OK;
|
2017-09-29 04:34:16 +05:30
|
|
|
}
|
|
|
|
|
2017-03-07 22:44:54 +05:30
|
|
|
private function getEmailActivationsDurationsMap(): array {
|
|
|
|
$durationsMap = [];
|
|
|
|
foreach (EmailActivation::getClassMap() as $typeId => $className) {
|
|
|
|
/** @var EmailActivation $object */
|
2018-04-18 02:17:25 +05:30
|
|
|
$object = new $className();
|
2017-03-07 22:44:54 +05:30
|
|
|
/** @var \common\behaviors\EmailActivationExpirationBehavior $behavior */
|
|
|
|
$behavior = $object->getBehavior('expirationBehavior');
|
2017-11-15 02:33:38 +05:30
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
2019-07-15 04:29:56 +05:30
|
|
|
$expiration = $behavior->expirationTimeout ?? 1123200; // 13d by default
|
|
|
|
// We increment 1 day so that users can still receive notifications about the expiry of the activation code
|
2017-03-07 22:44:54 +05:30
|
|
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
|
|
|
$durationsMap[$typeId] = $expiration + 86400;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $durationsMap;
|
|
|
|
}
|
|
|
|
|
2016-11-24 02:11:33 +05:30
|
|
|
}
|