2022-01-01 14:52:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
2023-06-26 09:19:42 +00:00
|
|
|
use Illuminate\Validation\Rule;
|
2022-01-01 14:52:21 +00:00
|
|
|
|
2023-06-26 09:19:42 +00:00
|
|
|
use App\Classes\FTN\Packet;
|
2023-10-05 00:11:21 +00:00
|
|
|
use App\Models\{Setup,System};
|
2022-01-01 14:52:21 +00:00
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
// @todo rename to SystemRegisterRequest
|
2022-01-01 14:52:21 +00:00
|
|
|
class SystemRegister extends FormRequest
|
|
|
|
{
|
|
|
|
private System $so;
|
|
|
|
|
2023-04-14 09:47:33 +00:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(Request $request)
|
|
|
|
{
|
2023-10-05 00:11:21 +00:00
|
|
|
$this->so = new System;
|
2022-01-01 14:52:21 +00:00
|
|
|
|
2023-10-05 00:11:21 +00:00
|
|
|
if (is_numeric($request->name)) {
|
|
|
|
$this->so = System::findOrNew($request->name);
|
|
|
|
|
|
|
|
// Cannot claim this site
|
|
|
|
if ($this->so->id === Setup::findOrFail(config('app.id'))->system_id)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Gate::allows(is_numeric($request->name) && $this->so->users->count() ? 'update' : 'register',$this->so);
|
2023-04-14 09:47:33 +00:00
|
|
|
}
|
2022-01-01 14:52:21 +00:00
|
|
|
|
2023-07-26 09:44:07 +00:00
|
|
|
public function messages(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'hold' => 'Must be Yes or No',
|
|
|
|
'pollmode' => 'Must be Hold, Normal or Crash',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-04-14 09:47:33 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2024-04-26 02:23:55 +00:00
|
|
|
// When the user first select register/link (get)
|
|
|
|
if (! $request->isMethod('post'))
|
2022-01-01 14:52:21 +00:00
|
|
|
return [];
|
|
|
|
|
2023-11-26 02:10:23 +00:00
|
|
|
$so = $this->route('o');
|
2024-04-26 02:23:55 +00:00
|
|
|
if ((! $so) && ($request->action === 'register'))
|
|
|
|
return [];
|
2023-11-26 02:10:23 +00:00
|
|
|
|
2024-04-26 02:23:55 +00:00
|
|
|
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' => 'required|boolean',
|
|
|
|
'pollmode' => 'required|integer|min:0|max:2',
|
|
|
|
'heartbeat' => 'nullable|integer|min:0|max:48',
|
|
|
|
] : []));
|
2023-04-14 09:47:33 +00:00
|
|
|
}
|
2022-01-01 14:52:21 +00:00
|
|
|
}
|