v0.8.0
This commit is contained in:
commit
bf5c31169a
221 files changed
+16292
No files matched your search
@@ -0,0 +1,199 @@
|
||||
<?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' => $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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Settings;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use IriksIT\Manager\Support\Permission;
|
||||
|
||||
/**
|
||||
* The settings screen: which parts and features are on, and the keys and addresses of integrations, all changeable from the
|
||||
* manager by an account with the right, in the whole face of the manager only. A change is saved (secrets encrypted) and works at
|
||||
* once; see Support\Settings.
|
||||
*/
|
||||
class SettingsModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'settings';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Settings';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function tables(): array
|
||||
{
|
||||
return ['settings'];
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
new Permission('settings.manage', 'Switch parts and features on or off and change settings', 'Settings'),
|
||||
new Permission('settings.secrets', 'Change keys, passwords and addresses of integrations', 'Settings', needs: ['settings.manage']),
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [['label' => 'Settings', 'route' => Names::route('settings.index'), 'permission' => 'settings.manage', 'icon' => 'cog', 'simple' => false]];
|
||||
}
|
||||
|
||||
public function actions(): array
|
||||
{
|
||||
return ['settings.update' => ['Changed the settings'], 'settings.refresh' => ['Refreshed the settings'], 'settings.check' => ['Tried a connection of the settings']];
|
||||
}
|
||||
|
||||
public function views(): array
|
||||
{
|
||||
return ['settings.index' => 'Opened the settings'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use IriksIT\Manager\Support\Tables;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Tables::create(Names::table('settings'), function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('key', 150)->unique();
|
||||
// JSON; a secret is encrypted.
|
||||
$table->text('value')->nullable();
|
||||
$table->boolean('is_secret')->default(false);
|
||||
$table->unsignedBigInteger('updated_by')->nullable();
|
||||
$table->dateTime('created_at')->nullable();
|
||||
$table->dateTime('updated_at')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists(Names::table('settings'));
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Settings\SettingsController;
|
||||
|
||||
Route::middleware('can:settings.manage')->group(function () {
|
||||
Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
|
||||
Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');
|
||||
Route::post('/settings/check', [SettingsController::class, 'check'])->name('settings.check');
|
||||
Route::post('/settings/refresh', [SettingsController::class, 'refresh'])->name('settings.refresh');
|
||||
});
|
||||
Reference in new issue
Block a user