ldc-gen-game-web/app/Http/Requests/Bo/Users/StoreUserRequest.php

79 lines
2.4 KiB
PHP
Raw Normal View History

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;
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
*/
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
*/
protected function prepareForValidation(): void
2023-11-23 17:31:28 +01:00
{
$this->merge([
'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
{
$isAdmin = $this->input('role') == RoleEnum::admin->value();
2023-11-23 17:31:28 +01:00
return [
'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
],
'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'),
'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
];
}
}