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;
|
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 {
|
2024-12-02 15:40:55 +05:30
|
|
|
$query = EmailActivation::find()
|
|
|
|
->andWhere(['<', 'created_at', time() - 60 * 60 * 24 * 14]); // 14d
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-24 02:11:33 +05:30
|
|
|
}
|