Internal enhancements to system registration and editing

This commit is contained in:
Deon George
2022-01-02 01:52:21 +11:00
parent fa2ac9a656
commit afaa7d8bc7
12 changed files with 235 additions and 87 deletions

View File

@@ -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);
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Models\System;
class SystemRegister extends FormRequest
{
private System $so;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(Request $request)
{
$this->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'] : [],
));
}
}

View File

@@ -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()

View File

@@ -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
{