ldc-supportweb/app/Http/Requests/Back/Users/Concerns/HasUserRequestRules.php
2026-06-22 15:06:21 +02:00

88 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Back\Users\Concerns;
use Uwil\LaravelBackend\Support\UserRoleRegistry;
/**
* Shared validation rules for user requests.
*/
trait HasUserRequestRules
{
/**
* Resolve role flags from the current request input.
*
* @return array{isClient:bool,isProjectManager:bool,isDevelopper:bool}
*/
private function resolveRoleFlags(): array
{
$role = $this->input('role');
return [
'isClient' => $role === UserRoleRegistry::get('client')->value,
'isProjectManager' => $role === UserRoleRegistry::get('project-manager')->value,
'isDevelopper' => $role === UserRoleRegistry::get('developper')->value,
];
}
/**
* Prepare the data for validation.
*/
protected function extraPrepareForValidation(): void
{
[
'isClient' => $isClient,
'isProjectManager' => $isProjectManager,
'isDevelopper' => $isDevelopper,
] = $this->resolveRoleFlags();
$authIsConceptor = auth('backend')->user()->role->value === UserRoleRegistry::get('conceptor')->value;
if ($authIsConceptor && ($isProjectManager || $isDevelopper)) {
$this->merge([
'gitlab_id' => $this->input('gitlab_id'),
]);
}
if ($isClient) {
$this->merge([
'sites' => $this->input('sites'),
]);
}
}
/**
* Get the extra validation rules.
*
* @return array<string,mixed>
*/
protected function extraRules(): array
{
[
'isClient' => $isClient,
'isProjectManager' => $isProjectManager,
'isDevelopper' => $isDevelopper,
] = $this->resolveRoleFlags();
$authIsConceptor = auth('backend')->user()->role->value === UserRoleRegistry::get('conceptor')->value;
return [
'gitlab_id' => $authIsConceptor && ($isProjectManager || $isDevelopper)
? 'required|integer|min:1'
: 'nullable|integer|min:1',
'sites' => $authIsConceptor && $isClient ? 'required|array' : 'nullable|array',
'sites.*' => 'integer|exists:sites,id|distinct',
];
}
/**
* Get the extra attributes.
*
* @return array<string,string>
*/
protected function extraAttributes(): array
{
return [
'gitlab_id' => trans('validation.custom.gitlab_id'),
'sites' => trans_choice('models.classes.sites', \INF),
];
}
}