2019-12-11 03:59:57 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace console\controllers;
|
|
|
|
|
|
|
|
use common\models\OauthSession;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Yii;
|
|
|
|
use yii\console\Controller;
|
|
|
|
use yii\console\ExitCode;
|
|
|
|
use yii\helpers\Console;
|
|
|
|
|
|
|
|
class ManualMigrateController extends Controller {
|
|
|
|
|
|
|
|
public function actionOauthSessions(bool $removeKeys = false): int {
|
|
|
|
$cursor = 0;
|
|
|
|
$totalCount = 0;
|
|
|
|
while (true) {
|
|
|
|
$response = Yii::$app->redis->scan($cursor, 'MATCH', 'oauth:sessions:*:scopes', 'COUNT', 500);
|
|
|
|
$cursor = (int)$response[0];
|
|
|
|
$keys = $response[1];
|
|
|
|
if (!empty($keys)) {
|
2024-12-02 15:40:55 +05:30
|
|
|
$sessionsIds = array_map(fn(string $key): int => (int)explode(':', $key)[2], $keys);
|
2019-12-11 03:59:57 +05:30
|
|
|
/** @var OauthSession[] $sessions */
|
2019-12-11 17:47:38 +05:30
|
|
|
$sessions = OauthSession::find()->andWhere(['legacy_id' => $sessionsIds])->all();
|
2019-12-11 03:59:57 +05:30
|
|
|
foreach ($sessions as $session) {
|
2019-12-11 15:53:56 +05:30
|
|
|
if (empty($session->scopes)) {
|
2019-12-11 03:59:57 +05:30
|
|
|
$session->scopes = Yii::$app->redis->smembers("oauth:sessions:{$session->legacy_id}:scopes");
|
|
|
|
Assert::true($session->save());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removeKeys) {
|
|
|
|
Yii::$app->redis->del(...$keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalCount += count($keys);
|
|
|
|
Console::output("Processed {$totalCount} keys.");
|
|
|
|
|
|
|
|
if ($cursor === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExitCode::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|