diff --git a/app/Facades/Options.php b/app/Facades/Options.php new file mode 100644 index 0000000..74e5421 --- /dev/null +++ b/app/Facades/Options.php @@ -0,0 +1,13 @@ +first(); + if (is_null($value)) + throw new \Exception('This option does not exist.'); + + Cache::put($option, $value); + Cache::put($option . '_desc', 'Undefined description'); + } + + return $value; + } + + public function setOption(string $option, string $value, string $description) + { + Option::create([ + 'option_name' => $option, + 'option_value' => $value, + 'friendly_name' => $description + ]); + + Cache::put($option, $value, now()->addDay()); + Cache::put($option . '_desc', $description, now()->addDay()); + } + + public function pullOption($option): array + { + $oldOption = Option::where('option_name', $option)->first(); + Option::find($oldOption->id)->delete(); + + // putMany is overkill here + return [ + Cache::pull($option), + Cache::pull($option . '_desc') + ]; + } + + public function changeOption($option, $newValue) + { + $dbOption = Option::where('option_name', $option); + + if ($dbOption->first()) + { + $dbOptionInstance = Option::find($dbOption->id); + Cache::forget($option); + + + $dbOptionInstance->option_value = $newValue; + $dbOptionInstance->save(); + + Cache::put('option_name', $newValue, now()->addDay()); + } + else + { + throw new \Exception('This option does not exist.'); + } + } + + + public function optionExists(string $option): bool + { + $dbOption = Option::where('option_name', $option)->first(); + $locallyCachedOption = Cache::get($option); + + return !is_null($dbOption) || !is_null($locallyCachedOption); + } + +} diff --git a/app/Http/Controllers/OptionsController.php b/app/Http/Controllers/OptionsController.php new file mode 100644 index 0000000..c8860ed --- /dev/null +++ b/app/Http/Controllers/OptionsController.php @@ -0,0 +1,55 @@ +with('options'); + } + + public function saveSettings(Request $request) + { + if (Auth::user()->hasPermission('admin.settings.edit')) + { + foreach($request->all() as $optionName => $option) + { + try + { + if (Options::optionExists($option)) + { + Options::changeOption($optionName, $option); + } + } + catch(\Exception $ex) + { + // Silently ignore, because the only way this would happen is if someone manipulates the page, + // and obviously we can't save arbitrary option values even if the user has permission to do so. + continue; + } + } + + $request->session()->flash('success', 'Settings saved successfully!'); + } + else + { + $request->session()->flash('error', 'You do not have permission to update this resource.'); + } + + return redirect()->back(); + } +} diff --git a/app/Options.php b/app/Options.php new file mode 100644 index 0000000..e650bbc --- /dev/null +++ b/app/Options.php @@ -0,0 +1,13 @@ + 'admin.notificationsettings', 'submenu' => [ [ - 'text' => 'Global Notification Settings', - 'icon' => 'far fa-bell', - 'url' => '/admin/notifications', - 'can' => 'admin.notificationsettings.edit' + 'text' => 'Global Application Settings', + 'icon' => 'fas fa-cogs', + 'url' => '/admin/settings', + 'can' => 'admin.settings.view' ], [ 'text' => 'Developer Tools', diff --git a/database/seeds/DefaultOptionsSeeder.php b/database/seeds/DefaultOptionsSeeder.php new file mode 100644 index 0000000..c7fbebc --- /dev/null +++ b/database/seeds/DefaultOptionsSeeder.php @@ -0,0 +1,27 @@ + 'developer' + ]); + + Permission::create(['name' => 'admin.settings.view']); + Permission::create(['name' => 'admin.settings.edit']); + + $developer->givePermissionTo('admin.developertools.use'); + + + } +} diff --git a/resources/views/dashboard/administration/settings.blade.php b/resources/views/dashboard/administration/settings.blade.php new file mode 100644 index 0000000..c910ebe --- /dev/null +++ b/resources/views/dashboard/administration/settings.blade.php @@ -0,0 +1,61 @@ +@extends('adminlte::page') + +@section('title', 'Raspberry Network | Open Positions') + +@section('content_header') + + @if (Auth::user()->hasAnyRole('admin')) +
Change which notifications are sent here.
+