You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
3.1 KiB
88 lines
3.1 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Services\DatabaseConnectionManager; |
|
use Illuminate\Http\RedirectResponse; |
|
use Illuminate\Http\Request; |
|
use Illuminate\View\View; |
|
use InvalidArgumentException; |
|
use Throwable; |
|
|
|
class DatabaseController extends Controller |
|
{ |
|
public function __construct( |
|
private readonly DatabaseConnectionManager $connections |
|
) { |
|
} |
|
|
|
public function index(Request $request): View |
|
{ |
|
$sort = $request->string('sort', 'created')->toString(); |
|
$dir = $request->string('dir', 'desc')->toString(); |
|
$search = $request->string('q')->trim()->toString(); |
|
$connectionId = $request->integer('connection') ?: null; |
|
|
|
$allowedSort = ['db_name', 'created', 'active_days', 'state', 'last_index_activity', 'last_backup']; |
|
if (! in_array($sort, $allowedSort, true)) { |
|
$sort = 'created'; |
|
} |
|
$dir = $dir === 'asc' ? 'asc' : 'desc'; |
|
|
|
$lister = $this->connections->listerFor($connectionId); |
|
$result = $lister->listDatabases($sort, $dir, $search); |
|
|
|
$activeConnection = $connectionId |
|
? $this->connections->find($connectionId) |
|
: ($this->connections->allActive()[0] ?? null); |
|
|
|
return view('databases.index', [ |
|
'databases' => $result['rows'], |
|
'meta' => $result['meta'], |
|
'sort' => $sort, |
|
'dir' => $dir, |
|
'search' => $search, |
|
'error' => $result['error'] ?? null, |
|
'connections' => $this->connections->allActive(), |
|
'activeConnection' => $activeConnection, |
|
'supportsOffline' => $lister->supportsOffline(), |
|
]); |
|
} |
|
|
|
public function offline(Request $request, string $database): RedirectResponse |
|
{ |
|
$request->validate([ |
|
'confirm' => ['required', 'accepted'], |
|
'connection' => ['nullable', 'integer'], |
|
]); |
|
|
|
$connectionId = $request->integer('connection') ?: null; |
|
|
|
try { |
|
$lister = $this->connections->listerFor($connectionId); |
|
|
|
if (! $lister->supportsOffline()) { |
|
throw new InvalidArgumentException('Offline action is not supported for this database type.'); |
|
} |
|
|
|
$lister->makeOffline($database); |
|
|
|
$request->attributes->set('audit_metadata', [ |
|
'database' => $database, |
|
'connection_id' => $connectionId, |
|
]); |
|
|
|
return redirect() |
|
->route('databases.index', $request->only(['sort', 'dir', 'q', 'connection'])) |
|
->with('success', "Database [{$database}] is now restricted/offline."); |
|
} catch (InvalidArgumentException $e) { |
|
return redirect() |
|
->route('databases.index', $request->only(['sort', 'dir', 'q', 'connection'])) |
|
->with('error', $e->getMessage()); |
|
} catch (Throwable) { |
|
return redirect() |
|
->route('databases.index', $request->only(['sort', 'dir', 'q', 'connection'])) |
|
->with('error', 'Failed to restrict database.'); |
|
} |
|
} |
|
}
|
|
|