Add settings page

This commit is contained in:
Miguel Nogueira 2020-08-30 23:06:01 +01:00
parent 535a2c3973
commit ca82f5882d
10 changed files with 318 additions and 4 deletions

13
app/Facades/Options.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App\Facades;
use \Illuminate\Support\Facades\Facade;
class Options extends Facade
{
public static function getFacadeAccessor()
{
return 'sm-options';
}
}

83
app/Helpers/Options.php Normal file
View File

@ -0,0 +1,83 @@
<?php
namespace App\Helpers;
use App\Options as Option;
use Illuminate\Support\Facades\Cache;
class Options
{
public function getOption(string $option): string
{
$value = Cache::get($option);
if (is_null($value))
{
$value = Option::where('option_name', $option)->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);
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers;
use App\Helpers\Options;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class OptionsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
*/
public function index()
{
$options = Options::all();
return view('dashboard.administration.positions')
->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();
}
}

13
app/Options.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Options extends Model
{
public $fillable = [
'option_name',
'option_value'
];
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App;
class OptionsProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
App::bind('sm-options', function (){
return new App\Helpers\Options();
});
}
}

View File

@ -331,10 +331,10 @@ return [
'can' => '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',

View File

@ -0,0 +1,27 @@
<?php
use App\Facades\Options;
use Illuminate\Database\Seeder;
class DefaultOptionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Options::setOption('notify_new_application_email', true, 'Notify when a new application comes through');
Options::setOption('notify_application_comment', false, 'Notify when someone comments on an application');
Options::setOption('notify_new_user', true, 'Notify when someone signs up');
Options::setOption('notify_application_status_change', true, 'Notify when an application changes status');
Options::setOption('notify_applicant_approved', true, 'Notify when an applicant is approved');
Options::setOption('notify_vacancystatus_change', false, 'Notify when a vacancy\'s status changes');
Options::setOption('enable_slack_notifications', true, 'Enable slack notifications');
Options::setOption('enable_email_notifications', true, 'Enable e-mail notifications');
}
}

View File

@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Seeder;
class NewPermissions extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$developer = \Spatie\Permission\Models\Role::create([
'name' => 'developer'
]);
Permission::create(['name' => 'admin.settings.view']);
Permission::create(['name' => 'admin.settings.edit']);
$developer->givePermissionTo('admin.developertools.use');
}
}

View File

@ -0,0 +1,61 @@
@extends('adminlte::page')
@section('title', 'Raspberry Network | Open Positions')
@section('content_header')
@if (Auth::user()->hasAnyRole('admin'))
<h4>Administration / Settings</h4>
@else
<h4>Application Access Denied</h4>
@endif
@stop
@section('js')
@if (session()->has('success'))
<script>
toastr.success("{{session('success')}}")
</script>
@endif
@if($errors->any())
@foreach ($errors->all() as $error)
<script>toastr.error('{{$error}}', 'Validation error!')</script>
@endforeach
@endif
@stop
@section('content')
<div class="row">
<div class="col">
<div class="card">
<div class="card-header">
<h3>Notification Settings</h3>
<p>Change which notifications are sent here.</p>
</div>
<div class="card-body">
<form name="settings" id="settings" method="post" action="{{route('saveSettings')}}">
@foreach($notificationOptions as $option)
<div class="form-group">
<label for="{{$option->option_name}}">{{$option->friendly_name}}</label>
<input type="checkbox" name="{{$option->option_name}}" id="{{$option->option_name}}" class="form-control" checked>
</div>
@endforeach
</form>
</div>
</div>
</div>
</div>
@stop

View File

@ -159,6 +159,12 @@ Route::group(['middleware' => ['auth', 'forcelogout', '2fa']], function(){
Route::group(['prefix' => 'admin'], function (){
Route::get('settings', 'OptionsController@index')
->name('showSettings');
Route::get('settings/save', 'OptionsController@saveSettings')
->name('saveSettings');
Route::post('players/ban/{user}', 'BanController@insert')
->name('banUser');