2023-11-23 17:31:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Bo\Users;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Users\RoleEnum;
|
2024-01-31 09:03:18 +00:00
|
|
|
use App\Models\User;
|
2023-11-23 17:31:28 +01:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2023-12-05 12:33:25 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2025-04-18 16:59:57 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
2023-11-23 17:31:28 +01:00
|
|
|
use Illuminate\Validation\Rules\Enum;
|
|
|
|
|
|
|
|
|
|
class StoreUserRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2025-04-18 16:59:57 +02:00
|
|
|
public function authorize(): bool
|
2023-11-23 17:31:28 +01:00
|
|
|
{
|
2023-12-05 12:33:25 +01:00
|
|
|
return Gate::check('create', User::class);
|
2023-11-23 17:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-18 16:59:57 +02:00
|
|
|
protected function prepareForValidation(): void
|
2023-11-23 17:31:28 +01:00
|
|
|
{
|
|
|
|
|
$this->merge([
|
2025-04-18 16:59:57 +02:00
|
|
|
'published' => $this->boolean('published'),
|
|
|
|
|
'tenants' => ($this->input('role') == RoleEnum::conceptor->value()) ? null : $this->input('tenants'),
|
2023-11-23 17:31:28 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2025-04-18 16:59:57 +02:00
|
|
|
$isAdmin = $this->input('role') == RoleEnum::admin->value();
|
2023-11-23 17:31:28 +01:00
|
|
|
return [
|
2025-04-18 16:59:57 +02:00
|
|
|
'first_name' => 'required|string|min:3|max:255',
|
|
|
|
|
'last_name' => 'required|string|min:3|max:255',
|
|
|
|
|
'email' => [
|
|
|
|
|
'required',
|
|
|
|
|
'email:rfc,strict,dns,spoof,filter',
|
|
|
|
|
Rule::unique('users', 'email')
|
2023-11-23 17:31:28 +01:00
|
|
|
],
|
2025-04-18 16:59:57 +02:00
|
|
|
'role' => ['required', new Enum(RoleEnum::class)],
|
|
|
|
|
'tenants' => $isAdmin ? 'required|array' : 'nullable|array',
|
|
|
|
|
'tenants.*' => $isAdmin ? 'required|array' : 'nullable|array',
|
|
|
|
|
'tenants.*.id' => $isAdmin ? 'required|exists:tenants,id' : 'nullable|exists:tenants,id',
|
2025-06-25 17:56:24 +02:00
|
|
|
'published' => 'required|boolean',
|
2023-11-23 17:31:28 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get custom attributes for validator errors.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function attributes(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'first_name' => trans('validation.attributes.first_name'),
|
2025-04-18 16:59:57 +02:00
|
|
|
'last_name' => trans('validation.attributes.last_name'),
|
|
|
|
|
'email' => trans('validation.attributes.email'),
|
|
|
|
|
'role' => trans('validation.attributes.role'),
|
|
|
|
|
'tenants' => Str::of(trans('models.classes.tenant'))->plural(),
|
2025-06-25 17:56:24 +02:00
|
|
|
'published' => trans('validation.attributes.published'),
|
2023-11-23 17:31:28 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|