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 $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 $keys * @return list */ 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; } }