user()?->can('device.create') ?? false; } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { return self::deviceRules() + [ 'initial_username' => ['required', 'string', 'max:255'], 'initial_password' => ['nullable', 'string', 'max:1000'], 'initial_enable_password' => ['nullable', 'string', 'max:1000'], 'remove_legacy_users_on_activation' => ['sometimes', 'boolean'], ]; } public static function deviceRules(): array { return [ 'name' => ['required', 'string', 'max:255'], 'device_vendor_id' => ['required', 'exists:device_vendors,id'], 'device_type_id' => ['required', 'exists:device_types,id'], 'device_model_id' => ['nullable', 'exists:device_models,id'], 'management_address' => ['required', 'string', 'max:255'], 'connection_type' => ['required', 'in:routeros_api,ssh,telnet,web_http,snmp,vendor_api'], 'management_port' => ['required', 'integer', 'between:1,65535'], 'connection_ports' => ['required', 'array'], 'connection_ports.routeros_api' => ['nullable', 'integer', 'between:1,65535'], 'connection_ports.ssh' => ['nullable', 'integer', 'between:1,65535'], 'connection_ports.telnet' => ['nullable', 'integer', 'between:1,65535'], 'connection_ports.web_http' => ['nullable', 'integer', 'between:1,65535'], 'connection_ports.snmp' => ['nullable', 'integer', 'between:1,65535'], 'connection_ports.vendor_api' => ['nullable', 'integer', 'between:1,65535'], 'location' => ['nullable', 'string', 'max:255'], 'notes' => ['nullable', 'string', 'max:5000'], 'is_active' => ['sometimes', 'boolean'], ]; } protected function prepareForValidation(): void { $ports = collect($this->input('connection_ports', [])) ->map(fn ($port) => $port === '' || $port === null ? null : (int) $port) ->filter() ->all(); $slug = DeviceVendor::find($this->input('device_vendor_id'))?->slug; $order = match ($slug) { 'mikrotik' => ['routeros_api'], 'zte' => ['ssh'], 'hsgq' => ['ssh', 'telnet'], 'hisfocus' => ['telnet', 'web_http'], default => ['ssh', 'telnet', 'routeros_api', 'web_http', 'snmp', 'vendor_api'], }; $primary = collect($order)->first(fn ($protocol) => isset($ports[$protocol])); $this->merge([ 'connection_ports' => $ports, 'connection_type' => $primary ?? 'vendor_api', 'management_port' => $primary ? $ports[$primary] : 1, ]); } public function after(): array { return [function (Validator $validator): void { $slug = DeviceVendor::find($this->input('device_vendor_id'))?->slug; $ports = $this->input('connection_ports', []); $valid = match ($slug) { 'mikrotik' => isset($ports['routeros_api']), 'zte' => isset($ports['ssh']), 'hsgq' => isset($ports['ssh']) || isset($ports['telnet']), 'hisfocus' => isset($ports['telnet']) || isset($ports['web_http']), default => count($ports) > 0, }; if (! $valid) { $validator->errors()->add('connection_ports', 'Isi minimal satu port protokol yang didukung vendor.'); } }]; } }