70 lines
2.1 KiB
PHP
70 lines
2.1 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;
|
|
use App\Models\System;
|
|
|
|
// @todo rename to SystemRegisterRequest
|
|
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->users->count() ? 'update' : 'register',$this->so);
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
if ((! $request->isMethod('post')) || ($request->action === 'register'))
|
|
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_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,'.($this->so->exists ? $this->so->id : 0),
|
|
'pkt_type' => ['required',Rule::in(array_keys(Packet::PACKET_TYPES))],
|
|
] : [],
|
|
$this->so->exists ? [
|
|
'active' => 'required|boolean',
|
|
'hold' => 'required|boolean',
|
|
] : [],
|
|
));
|
|
}
|
|
} |