Made system/reset API endpoint sub-user aware.
This commit is contained in:
@@ -33,16 +33,15 @@
|
||||
<Message message_class="is-background-warning-80 has-text-dark" title="Important information"
|
||||
icon="fas fa-exclamation-triangle">
|
||||
<p>
|
||||
Are you sure you want to reset the system state? This operation will remove all records and metadata from
|
||||
the database. This action is irreversible.
|
||||
Are you sure you want to reset <span class="has-text-danger is-bold is-underlined">{{ api_user }}</span> user local state?
|
||||
</p>
|
||||
|
||||
<h5 class="has-text-dark">This operation will do the following</h5>
|
||||
<h5 class="has-text-dark">This operation will do the following:</h5>
|
||||
|
||||
<ul>
|
||||
<li>Remove all data from local database.</li>
|
||||
<li>Attempt to flush the cache.</li>
|
||||
<li>Reset the backends last sync date.</li>
|
||||
<li>Remove all data from user local database.</li>
|
||||
<li>Attempt to flush the cache related to user.</li>
|
||||
<li>Reset the user backends last sync date.</li>
|
||||
</ul>
|
||||
|
||||
<p>There is no undo operation. This action is irreversible.</p>
|
||||
@@ -51,7 +50,7 @@
|
||||
|
||||
<div class="column is-12">
|
||||
<Confirm @confirmed="resetSystem()"
|
||||
title="Perform system reset"
|
||||
:title="`Perform local state reset for: ${api_user}`"
|
||||
title-icon="fa-redo"
|
||||
warning="Depending on your hardware speed, the reset operation might take long time. do not interrupt the process, or close the browser tab. You will be redirected to the index page automatically once the process is complete. Otherwise, you might end up with a corrupted database and/or state."
|
||||
/>
|
||||
@@ -62,6 +61,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import {useStorage} from '@vueuse/core'
|
||||
import 'assets/css/bulma-switch.css'
|
||||
import request from '~/utils/request'
|
||||
import Message from '~/components/Message'
|
||||
@@ -71,6 +71,7 @@ import {useSessionCache} from '~/utils/cache'
|
||||
|
||||
const error = ref()
|
||||
const isResetting = ref(false)
|
||||
const api_user = useStorage('api_user', 'main')
|
||||
|
||||
const resetSystem = async () => {
|
||||
if (!confirm('Last chance! Are you sure you want to reset the system state?')) {
|
||||
|
||||
@@ -5,41 +5,55 @@ declare(strict_types=1);
|
||||
namespace App\API\System;
|
||||
|
||||
use App\Libs\Attributes\Route\Delete;
|
||||
use App\Libs\Config;
|
||||
use App\Libs\ConfigFile;
|
||||
use App\Libs\Database\DatabaseInterface as iDB;
|
||||
use App\Libs\Exceptions\RuntimeException;
|
||||
use App\Libs\Enums\Http\Status;
|
||||
use Psr\Http\Message\ResponseInterface as iResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface as iRequest;
|
||||
use Redis;
|
||||
use RedisException;
|
||||
use App\Libs\Mappers\ImportInterface as iImport;
|
||||
use App\Libs\Traits\APITraits;
|
||||
use Psr\Log\LoggerInterface as iLogger;
|
||||
|
||||
final class Reset
|
||||
{
|
||||
use APITraits;
|
||||
|
||||
public const string URL = '%{api.prefix}/system/reset';
|
||||
|
||||
public function __construct(private Redis $redis, private iDB $db)
|
||||
{
|
||||
}
|
||||
|
||||
#[Delete(self::URL . '[/]', name: 'system.reset')]
|
||||
public function __invoke(iRequest $request, array $args = []): iResponse
|
||||
public function __invoke(iRequest $request, Redis $redis, iImport $mapper, iLogger $logger): iResponse
|
||||
{
|
||||
$this->db->reset();
|
||||
try {
|
||||
$userContext = $this->getUserContext($request, $mapper, $logger);
|
||||
$user = $userContext->name;
|
||||
} catch (RuntimeException $e) {
|
||||
return api_error($e->getMessage(), Status::NOT_FOUND);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->redis->flushDB();
|
||||
$ns = getAppVersion();
|
||||
|
||||
if (true === isValidName($user)) {
|
||||
$ns .= isValidName($user) ? '.' . $user : '.' . md5($user);
|
||||
}
|
||||
|
||||
$keys = $redis->keys("{$ns}*");
|
||||
|
||||
if ($keys && is_array($keys)) {
|
||||
$redis->del($keys);
|
||||
}
|
||||
} catch (RedisException) {
|
||||
}
|
||||
|
||||
$list = ConfigFile::open(Config::get('backends_file'), 'yaml', autoCreate: true);
|
||||
$userContext->db->reset();
|
||||
|
||||
foreach ($list->getAll() as $name => $backend) {
|
||||
$list->set("{$name}.import.lastSync", null);
|
||||
$list->set("{$name}.export.lastSync", null);
|
||||
foreach (array_keys($userContext->config->getAll()) as $name) {
|
||||
$userContext->config->set("{$name}.import.lastSync", null);
|
||||
$userContext->config->set("{$name}.export.lastSync", null);
|
||||
}
|
||||
|
||||
$list->persist();
|
||||
$userContext->config->persist();
|
||||
|
||||
return api_response(Status::OK, ['message' => 'System reset.']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user