Files
device-management/app/Http/Requests/StoreDeviceAccessUserRequest.php
T
Wian Drs 1e80be180e
tests / ci (push) Has been cancelled
Initial commit device-management
2026-08-25 09:42:00 +07:00

76 lines
3.8 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\Device;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Validator;
class StoreDeviceAccessUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user()?->can('device.user.create') ?? false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return $this->rulesForUser();
}
protected function rulesForUser(?int $ignoreId = null): array
{
$tenantId = $this->user()->tenant_id;
return [
'display_name' => ['required', 'string', 'max:255'],
'username' => ['required', 'regex:/^[A-Za-z0-9][A-Za-z0-9_.@#-]*[A-Za-z0-9]$/', 'max:64', Rule::unique('device_access_users')->where('tenant_id', $tenantId)->ignore($ignoreId)],
'password' => ['required', 'confirmed', Password::min(12)->mixedCase()->numbers()->symbols()],
'group_name' => ['required', 'in:RADIQ-READ,RADIQ-WRITE,RADIQ-NOC'],
'device_ids' => ['required', 'array', 'min:1'],
'device_ids.*' => ['integer', Rule::exists('devices', 'id')->where('tenant_id', $tenantId)],
'is_enabled' => ['sometimes', 'boolean'], 'notes' => ['nullable', 'string', 'max:5000'],
];
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
$hasZte = Device::whereIn('id', $this->input('device_ids', []))->whereHas('vendor', fn ($query) => $query->where('slug', 'zte'))->exists();
if ($hasZte && ! preg_match('/^[A-Za-z0-9_]{1,16}$/', (string) $this->input('username'))) {
$validator->errors()->add('username', 'Untuk ZTE C300/C320, username harus 1-16 karakter alfanumerik atau underscore.');
}
$password = (string) $this->input('password');
if ($hasZte && $password !== '' && (strlen($password) > 32 || preg_match('/\s/', $password))) {
$validator->errors()->add('password', 'Untuk ZTE C300/C320, password maksimal 32 karakter dan tidak boleh mengandung spasi.');
}
$hasHsgq = Device::whereIn('id', $this->input('device_ids', []))->whereHas('vendor', fn ($query) => $query->where('slug', 'hsgq'))->exists();
if ($hasHsgq && ! preg_match('/^[A-Za-z0-9_]{4,16}$/', (string) $this->input('username'))) {
$validator->errors()->add('username', 'Untuk HSGQ, username harus 4-16 karakter alfanumerik atau underscore.');
}
if ($hasHsgq && $password !== '' && (strlen($password) > 64 || preg_match('/\s/', $password))) {
$validator->errors()->add('password', 'Untuk HSGQ, password maksimal 64 karakter dan tidak boleh mengandung spasi.');
}
$hasHisfocus = Device::whereIn('id', $this->input('device_ids', []))->whereHas('vendor', fn ($query) => $query->where('slug', 'hisfocus'))->exists();
if ($hasHisfocus && ! preg_match('/^[A-Za-z][A-Za-z0-9_]{3,15}$/', (string) $this->input('username'))) {
$validator->errors()->add('username', 'Untuk Hisfocus, username harus 4-16 karakter, diawali huruf, dan hanya alfanumerik atau underscore.');
}
if ($hasHisfocus && $password !== '' && (strlen($password) > 64 || preg_match('/\s/', $password))) {
$validator->errors()->add('password', 'Untuk Hisfocus, password maksimal 64 karakter dan tidak boleh mengandung spasi.');
}
});
}
}