200 lines
7.8 KiB
PHP
200 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Modules\Settings;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Validation\ValidationException;
|
|
use IriksIT\Manager\Support\Menu;
|
|
use IriksIT\Manager\Support\ModuleRegistry;
|
|
use IriksIT\Manager\Support\Names;
|
|
use IriksIT\Manager\Support\Settings\Checks;
|
|
use IriksIT\Manager\Support\Settings\Definitions;
|
|
use IriksIT\Manager\Support\Settings\EnvFile;
|
|
use IriksIT\Manager\Support\Settings\Store;
|
|
use IriksIT\Manager\Support\Settings\Writer;
|
|
|
|
/**
|
|
* The settings screen. Only in the whole face of the manager (a simple account gets a 403, whatever its rights). What changes
|
|
* how a password or a file travels (secrets, addresses, accounts, disks) also needs `settings.secrets` and the password of the
|
|
* account that saves it. A secret is never sent to the browser: it is only said to be set.
|
|
*/
|
|
class SettingsController
|
|
{
|
|
public function __construct(
|
|
private readonly Definitions $definitions,
|
|
private readonly Store $store,
|
|
private readonly EnvFile $env,
|
|
private readonly Writer $writer,
|
|
private readonly Checks $checks,
|
|
private readonly ModuleRegistry $modules,
|
|
private readonly Menu $menu,
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$this->onlyFull($request);
|
|
|
|
$groups = [];
|
|
$origin = [];
|
|
|
|
foreach ($this->definitions->all() as $key => $definition) {
|
|
if (! $this->mayChange($request, $definition)) {
|
|
continue;
|
|
}
|
|
|
|
$value = config($key);
|
|
|
|
$origin[__($definition['group'])] = $definition['group'];
|
|
$groups[__($definition['group'])][] = [
|
|
'key' => $key,
|
|
'label' => __($definition['label']),
|
|
'type' => $definition['type'],
|
|
'help' => $definition['help'] === null ? null : __($definition['help']),
|
|
'options' => array_map(fn ($label) => __($label), $definition['options']),
|
|
'sensitive' => $definition['sensitive'],
|
|
// A secret says only whether it is set.
|
|
'value' => $definition['type'] === 'secret' ? null : $value,
|
|
'set' => $definition['type'] === 'secret' ? filled($value) : null,
|
|
'saved' => $this->store->has($key),
|
|
];
|
|
}
|
|
|
|
$mayTry = Gate::forUser($request->user())->allows('settings.secrets');
|
|
|
|
return response()->json([
|
|
'groups' => collect($groups)->map(fn (array $items, string $name) => ['name' => $name, 'items' => $items, 'check' => $mayTry && $this->checks->has($origin[$name])])->values(),
|
|
'write_env' => (bool) config('manager.settings.write_env'),
|
|
'env_writable' => $this->env->writable(),
|
|
'config_cached' => app()->configurationIsCached(),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request): JsonResponse
|
|
{
|
|
$this->onlyFull($request);
|
|
$request->validate(['values' => ['present', 'array'], 'reset' => ['nullable', 'array'], 'reset.*' => ['string'], 'password' => ['nullable', 'string', 'max:200']]);
|
|
|
|
$changes = [];
|
|
$sensitive = false;
|
|
|
|
foreach ($request->array('values') as $key => $raw) {
|
|
$definition = $this->definitions->find((string) $key);
|
|
|
|
if ($definition === null || ! $this->mayChange($request, $definition)) {
|
|
throw ValidationException::withMessages(['values' => __('This setting does not exist or you may not change it.')]);
|
|
}
|
|
|
|
// A secret that is left empty is left alone (the browser sends an empty field as null): clearing one is `reset`.
|
|
if ($definition['type'] === 'secret' && ($raw === '' || $raw === null)) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$value = $raw === null ? null : $this->definitions->cast($definition, $raw);
|
|
} catch (\InvalidArgumentException) {
|
|
throw ValidationException::withMessages(['values.'.$key => __('This value is not valid.')]);
|
|
}
|
|
|
|
if ($value !== null && $value === config($key) && ! $this->store->has($key)) {
|
|
continue;
|
|
}
|
|
|
|
$changes[$key] = $value;
|
|
$sensitive = $sensitive || $definition['sensitive'];
|
|
}
|
|
|
|
// `reset`: back to what the config (and .env) says.
|
|
foreach ($request->array('reset') as $key) {
|
|
$definition = $this->definitions->find((string) $key);
|
|
|
|
if ($definition === null || ! $this->mayChange($request, $definition)) {
|
|
throw ValidationException::withMessages(['reset' => __('This setting does not exist or you may not change it.')]);
|
|
}
|
|
|
|
$changes[$key] = null;
|
|
$sensitive = $sensitive || $definition['sensitive'];
|
|
}
|
|
|
|
if ($sensitive && ! Hash::check((string) $request->input('password'), (string) $request->user()->getAuthPassword())) {
|
|
throw ValidationException::withMessages(['password' => __('Type your own password to change this.')]);
|
|
}
|
|
|
|
$result = $this->writer->apply($changes, $request->user()->getAuthIdentifier());
|
|
|
|
return response()->json([
|
|
'saved' => true,
|
|
'changed' => count($changes),
|
|
...$result,
|
|
'migrate' => $this->pendingMigrations(array_keys($changes)),
|
|
]);
|
|
}
|
|
|
|
/** Writes the compiled file again and empties what caches the old values. */
|
|
public function refresh(Request $request): JsonResponse
|
|
{
|
|
$this->onlyFull($request);
|
|
|
|
$written = $this->writer->refresh();
|
|
|
|
return response()->json(['refreshed' => true, 'file_written' => $written]);
|
|
}
|
|
|
|
/** Tries the connection of one group of settings with what is saved. Needs `settings.secrets`: it uses the keys. */
|
|
public function check(Request $request): JsonResponse
|
|
{
|
|
$this->onlyFull($request);
|
|
abort_unless(Gate::forUser($request->user())->allows('settings.secrets'), 403);
|
|
|
|
$group = (string) $request->validate(['group' => ['required', 'string', 'max:100']])['group'];
|
|
// The name that comes in is the translated one, as the screen shows it; the checks are keyed by the untranslated one.
|
|
$key = collect($this->definitions->all())->pluck('group')->unique()->first(fn (string $name) => __($name) === $group);
|
|
|
|
abort_unless($key !== null && $this->checks->has($key), 404);
|
|
|
|
return response()->json($this->checks->run($key));
|
|
}
|
|
|
|
private function onlyFull(Request $request): void
|
|
{
|
|
abort_if($this->menu->view($request->user()) === Menu::SIMPLE, 403);
|
|
}
|
|
|
|
/** @param array<string, mixed> $definition */
|
|
private function mayChange(Request $request, array $definition): bool
|
|
{
|
|
$user = $request->user();
|
|
|
|
return (! $definition['sensitive'] || Gate::forUser($user)->allows('settings.secrets'))
|
|
&& ($definition['permission'] === null || Gate::forUser($user)->allows($definition['permission']));
|
|
}
|
|
|
|
/**
|
|
* The parts that were switched on and miss a table: the screen tells to run `php artisan migrate`.
|
|
*
|
|
* @param list<string> $keys
|
|
* @return list<string>
|
|
*/
|
|
private function pendingMigrations(array $keys): array
|
|
{
|
|
$labels = [];
|
|
|
|
foreach ($keys as $key) {
|
|
if (! str_starts_with($key, 'manager.modules.') || ! config($key)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($this->modules->all() as $module) {
|
|
if ('manager.modules.'.$module->key() === $key && collect($module->tables())->contains(fn (string $table) => ! Schema::hasTable(Names::table($table)))) {
|
|
$labels[] = $module->label();
|
|
}
|
|
}
|
|
}
|
|
|
|
return $labels;
|
|
}
|
|
}
|