clrghouz/app/Http/Requests/SystemRegisterRequest.php
Deon George 27cdb02b06
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 41s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 2m8s
Create Docker Image / Final Docker Image Manifest (push) Successful in 11s
Fixes for saving a new system
2024-06-03 22:31:38 +10:00

76 lines
2.3 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use App\Classes\FTN\Packet;
class SystemRegisterRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(Request $request)
{
if (! $request->post() || (! $this->route('o')))
return TRUE;
// @todo Also disallow claiming this hosts system
return Gate::allows($this->route('o')->users->count() ? 'update_nn' : 'register',$this->route('o'));
}
public function messages(): array
{
return [
'hold' => 'Must be Yes or No',
'pollmode' => 'Must be Hold, Normal or Crash',
];
}
/**
* Get the validation rules that apply to the request.
*
* If the system exists (POST & action="register" & system_id=<value>), then no validation required
* If the system doesnt exist (POST & action="register" & system_id undefined) then we need just a name to start the process (action="create")
* Then, full validation
* @return array
*/
public function rules(Request $request)
{
// When the user first select register/link (get)
if (! $request->isMethod('post'))
return [];
$so = $this->route('o');
if ((! $so) && ($request->action === 'register'))
return [];
return array_filter(array_merge([
'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_details.*' => 'nullable|array',
'mailer_details.*.port' => 'nullable|digits_between:2,5',
'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($so ? $so->id : 0),
'pkt_type' => ['required',Rule::in(array_keys(Packet::PACKET_TYPES))],
],($so && $so->exists) ? [
'users' => 'nullable|array|min:1|max:2',
'active' => 'required|boolean',
'hold' => 'sometimes|boolean',
'pollmode' => 'required|integer|min:0|max:2',
'heartbeat' => 'nullable|integer|min:0|max:48',
'pkt_msgs' => 'nullable|integer|min:5',
] : []));
}
}