From afaa7d8bc7cba58dc95566716a75892940cdc9bd Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 2 Jan 2022 01:52:21 +1100 Subject: [PATCH] Internal enhancements to system registration and editing --- .gitignore | 1 + app/Http/Controllers/SystemController.php | 37 +++--- app/Http/Requests/SystemRegister.php | 62 +++++++++ app/Models/System.php | 23 +++- app/Policies/SystemPolicy.php | 22 +++- resources/views/setup.blade.php | 2 +- resources/views/system/form-system.blade.php | 5 +- resources/views/system/home.blade.php | 10 +- resources/views/system/moveaddr.blade.php | 2 +- .../views/system/widget/form-system.blade.php | 32 ++--- .../views/user/system/register.blade.php | 124 +++++++++++++----- resources/views/zone/addedit.blade.php | 2 +- 12 files changed, 235 insertions(+), 87 deletions(-) create mode 100644 app/Http/Requests/SystemRegister.php diff --git a/.gitignore b/.gitignore index 169c5ee..0dae787 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ npm-debug.log yarn-error.log .env .phpunit.result.cache +/config/ssl/ diff --git a/app/Http/Controllers/SystemController.php b/app/Http/Controllers/SystemController.php index 158db4c..820cff6 100644 --- a/app/Http/Controllers/SystemController.php +++ b/app/Http/Controllers/SystemController.php @@ -7,8 +7,10 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\ViewErrorBag; +use App\Http\Requests\SystemRegister; use App\Models\{Address,Echoarea,System,SystemZone,Zone}; use App\Rules\{FidoInteger,TwoByteInteger}; @@ -253,26 +255,9 @@ class SystemController extends Controller /** * Add or edit a node */ - public function add_edit(Request $request,System $o) + public function add_edit(SystemRegister $request,System $o) { if ($request->post()) { - $this->authorize('admin',$o); - - $request->validate([ - 'name' => 'required|min:3', - 'location' => 'required|min:3', - 'sysop' => 'required|min:3', - 'phone' => 'nullable|regex:/^([0-9-]+)$/', - 'address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i', - 'port' => 'nullable|digits_between:2,5', - 'method' => 'nullable|numeric', - 'mailer_type' => 'nullable|numeric', - 'mailer_address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i', - 'mailer_port' => 'nullable|digits_between:2,5', - 'active' => 'required|boolean', - 'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($o->exists ? $o->id : 0), - ]); - foreach (['name','location','sysop','phone','address','port','active','method','notes','mailer_type','mailer_address','mailer_port','zt_id'] as $key) $o->{$key} = $request->post($key); @@ -283,8 +268,11 @@ class SystemController extends Controller $o->load(['addresses.zone.domain']); - return view('system.addedit') - ->with('o',$o); + return Gate::check('update',$o) + ? view('system.addedit') + ->with('action',$o->exists ? 'update' : 'create') + ->with('o',$o) + : redirect()->to('user/system/register'); } /** @@ -448,7 +436,7 @@ class SystemController extends Controller /** * register system */ - public function system_register(Request $request) + public function system_register(SystemRegister $request) { $o = System::findOrNew($request->system_id); @@ -465,12 +453,17 @@ class SystemController extends Controller if ($request->post('submit')) { Auth::user()->systems()->save($o); - // @todo if the system already exists and part of one of our nextworks, we'll need to send the registration email to confirm the address. + // @todo if the system already exists and part of one of our networks, we'll need to send the registration email to confirm the address. // @todo mark the system (or addresses) as "pending" at this stage until it is confirmed return redirect()->to(url('ftn/system/addedit',$o->id)); } + // Re-flash our previously input data + if ($request->old) + session()->flashInput($request->old); + return view('system.widget.form-system') + ->with('action',$request->action) ->with('o',$o) ->with('errors',new ViewErrorBag); } diff --git a/app/Http/Requests/SystemRegister.php b/app/Http/Requests/SystemRegister.php new file mode 100644 index 0000000..8203de2 --- /dev/null +++ b/app/Http/Requests/SystemRegister.php @@ -0,0 +1,62 @@ +so = System::findOrNew($request->system_id); + + return Gate::allows($this->so->exists ? 'update' : 'create',$this->so); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(Request $request) + { + if (! $request->isMethod('post')) + return []; + + if ((! $this->so->exists) && ($request->action == 'create')) { + return [ + 'name' => 'required|min:3', + ]; + } + + return array_filter(array_merge( + [ + 'name' => 'required|min:3', + ], + ($this->so->exists || ($request->action != 'create')) ? [ + 'location' => 'required|min:3', + 'sysop' => 'required|min:3', + 'phone' => 'nullable|regex:/^([0-9-]+)$/', + 'address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i', + 'port' => 'nullable|digits_between:2,5', + 'method' => 'nullable|numeric', + 'mailer_type' => 'nullable|numeric', + 'mailer_address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i', + 'mailer_port' => 'nullable|digits_between:2,5', + 'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($this->so->exists ? $this->so->id : 0), + ] : [], + $this->so->exists ? ['active' => 'required|boolean'] : [], + )); + } +} \ No newline at end of file diff --git a/app/Models/System.php b/app/Models/System.php index 3ce9f16..1bdcc36 100644 --- a/app/Models/System.php +++ b/app/Models/System.php @@ -4,17 +4,34 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Auth; use App\Http\Controllers\DomainController; -use App\Traits\ScopeActive; -use Illuminate\Support\Collection; class System extends Model { - use HasFactory,ScopeActive; + use HasFactory; protected $dates = ['last_session']; + /* SCOPES */ + + /** + * Only query active records + */ + public function scopeActive($query) + { + $uo = Auth::user(); + + return $query + ->when(! $uo->isAdmin(),function($query) use ($uo) { + return $query->whereIn('id',$uo->systems->pluck('id')) + ->orWhere($this->getTable().'.active',TRUE); + }) + ->orderBy('name'); + } + /* RELATIONS */ public function addresses() diff --git a/app/Policies/SystemPolicy.php b/app/Policies/SystemPolicy.php index e66c1a9..6721094 100644 --- a/app/Policies/SystemPolicy.php +++ b/app/Policies/SystemPolicy.php @@ -10,6 +10,22 @@ class SystemPolicy { use HandlesAuthorization; + /** + * Determine whether the user can create the model. + * + * A user can create a system if it doesnt exist. + * + * @param User $user + * @param System $system + * @return bool + */ + public function create(User $user, System $system): bool + { + // Site Admins can always create + // If it doesnt exist, then a user can create it. + return ($user->isAdmin() || (! $system->exists)); + } + /** * Determine whether the user can update the model. * @@ -17,9 +33,9 @@ class SystemPolicy * If it has addresses, at least one of the addresses must have been validated. * (The assumption is, if a system has multiple addresses, they would be valid, or an admin can remove them.) * - * @param \App\Models\User $user - * @param \App\Models\System $system - * @return \Illuminate\Auth\Access\Response|bool + * @param User $user + * @param System $system + * @return bool */ public function update(User $user, System $system): bool { diff --git a/resources/views/setup.blade.php b/resources/views/setup.blade.php index e1d1529..1807b1b 100644 --- a/resources/views/setup.blade.php +++ b/resources/views/setup.blade.php @@ -30,7 +30,7 @@ use App\Models\Setup; diff --git a/resources/views/system/form-system.blade.php b/resources/views/system/form-system.blade.php index 13aedc7..a937b2c 100644 --- a/resources/views/system/form-system.blade.php +++ b/resources/views/system/form-system.blade.php @@ -1,9 +1,6 @@ -@php - use App\Models\Setup; -@endphp -
@csrf +
diff --git a/resources/views/system/home.blade.php b/resources/views/system/home.blade.php index 215c1ec..5883e69 100644 --- a/resources/views/system/home.blade.php +++ b/resources/views/system/home.blade.php @@ -13,10 +13,10 @@
-

This system is aware of the following systems @can('admin',(new \App\Models\System))(you can add more)@endcan:

+

This system is aware of the following systems @can('create',(new \App\Models\System))(you can add more)@endcan:

- @if (\App\Models\System::count() == 0) - @can('admin',(new \App\Models\System)) + @if (\App\Models\System::active()->count() == 0) + @can('create',(new \App\Models\System))

There are no systems setup, to set up your first.

@else

There are no systems - you need to ask an admin to create one for you.

@@ -37,10 +37,10 @@ - @foreach (\App\Models\System::active()->orderBy('name')->with(['addresses.zone.domain'])->get() as $oo) + @foreach (\App\Models\System::active()->with(['addresses.zone.domain'])->get() as $oo) {{ $oo->id }} - {{ $oo->name }} + {{ $oo->name }} @if(! $oo->active)[i]@endif {{ $oo->sysop }} {{ $oo->location }} diff --git a/resources/views/system/moveaddr.blade.php b/resources/views/system/moveaddr.blade.php index dbed049..905cbca 100644 --- a/resources/views/system/moveaddr.blade.php +++ b/resources/views/system/moveaddr.blade.php @@ -29,7 +29,7 @@ Move Address diff --git a/resources/views/system/widget/form-system.blade.php b/resources/views/system/widget/form-system.blade.php index 903a2a9..78fd127 100644 --- a/resources/views/system/widget/form-system.blade.php +++ b/resources/views/system/widget/form-system.blade.php @@ -5,10 +5,10 @@
- +
- - + + @error('name') {{ $message }} @@ -22,7 +22,7 @@
@if($o->exists) - @can('admin',$o) + @can('update',$o)
@@ -42,7 +42,7 @@
- + @error('zt_id') {{ $message }} @@ -58,7 +58,7 @@
- + @error('sysop') {{ $message }} @@ -74,7 +74,7 @@
- + @error('location') {{ $message }} @@ -96,7 +96,7 @@
- @@ -108,8 +108,8 @@
- - + + @error('mailer_address') {{ $message }} @@ -127,7 +127,7 @@
- + @error('phone') {{ $message }} @@ -149,7 +149,7 @@
- @@ -162,8 +162,8 @@
- - + + @error('address') {{ $message }} @@ -191,7 +191,7 @@
@if($o->exists) - @can('update',$o) + @can($action,$o) Cancel @else @@ -200,7 +200,7 @@ @endcan @else - + @endif
\ No newline at end of file diff --git a/resources/views/user/system/register.blade.php b/resources/views/user/system/register.blade.php index 1d31261..86c1314 100644 --- a/resources/views/user/system/register.blade.php +++ b/resources/views/user/system/register.blade.php @@ -13,17 +13,17 @@

Register System

-
+
- +
- @error('zone_id') + @error('name') {{ $message }} @else BBS Name is required. @@ -33,21 +33,40 @@
-
-
- + @if (old('submit') != 'create') +
+
+ +
-
+ @endif
+ + @endsection @section('page-css')