Files
root f83f21936f
API CI/CD / Validate (composer + pint) (push) Successful in 3m30s
API CI/CD / Test (PHPUnit) (push) Failing after 5m4s
API CI/CD / Build frontend assets (push) Successful in 1m1s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix test issues
2026-07-07 09:06:42 -04:00

53 lines
1.8 KiB
PHP

<?php
namespace App\Http\Requests\Roles;
use App\Http\Requests\ApiFormRequest;
use App\Models\Role;
use Illuminate\Validation\Rule;
class RoleUpdateRequest extends ApiFormRequest
{
public function authorize(): bool
{
return $this->user() !== null;
}
public function rules(): array
{
$roleId = (int) (
$this->route('roleId')
?? $this->route('id')
?? $this->input('id')
?? 0
);
$current = $roleId > 0 ? Role::query()->find($roleId) : null;
$currentName = strtolower(trim((string) ($current?->name ?? '')));
$currentSlug = strtolower(trim((string) ($current?->slug ?? '')));
$nameInput = $this->input('name', '');
$slugInput = $this->input('slug', '');
$incomingName = is_scalar($nameInput) ? strtolower(trim((string) $nameInput)) : '';
$incomingSlug = is_scalar($slugInput) ? strtolower(trim((string) $slugInput)) : '';
$nameRules = ['sometimes', 'string', 'min:3', 'max:255'];
if ($incomingName !== '' && $incomingName !== $currentName) {
$nameRules[] = Rule::unique('roles', 'name')->ignore($roleId);
}
$slugRules = ['nullable', 'string', 'max:64'];
if ($incomingSlug !== '' && $incomingSlug !== $currentSlug) {
$slugRules[] = Rule::unique('roles', 'slug')->ignore($roleId);
}
return [
'name' => $nameRules,
'slug' => $slugRules,
'description' => ['nullable', 'string', 'max:500'],
'dashboard_route' => ['sometimes', 'string', 'min:1', 'max:255', 'regex:/^[a-z0-9_\\/\\-]+$/'],
'priority' => ['sometimes', 'integer', 'min:0', 'max:100000'],
'is_active' => ['sometimes', Rule::in([0, 1, '0', '1', true, false])],
];
}
}