ldc-com-app-bo/app/Http/Requests/Bo/AppUser/StoreAppUserRequest.php
2025-06-12 16:50:56 +02:00

92 lines
2.8 KiB
PHP

<?php
namespace App\Http\Requests\Bo\AppUser;
use App\Enums\AppUserRolesEnum;
use App\Models\AppUser;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\Rules\Password;
class StoreAppUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return Gate::check('create', AppUser::class);
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->merge([
'status' => $this->status ? true : false,
'authorizedFolders' => $this->authorizedFolders ? $this->authorizedFolders : [],
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'lastname' => 'required|min:3|max:255',
'firstname' => 'required|min:3|max:255',
'email' => [
'required',
'email:rfc,strict,dns,spoof,filter',
Rule::unique('app_users', 'email')->where(fn (Builder $query) => $query->where('tenant_id', tenant('id')))
],
'password' => [
'required',
'required_with:password_confirmation',
'confirmed',
'max:255',
Password::min(8)
->letters()
->mixedCase()
->numbers()
->symbols()
],
'authorizedFolders' => 'present|array',
'role' => [
'required',
new Enum(AppUserRolesEnum::class)
],
'status' => 'required|boolean'
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'lastname' => \trans('validation.attributes.last_name'),
'firstname' => \trans('validation.attributes.first_name'),
'email' => \trans('validation.attributes.email'),
'password' => \trans('validation.attributes.password'),
'authorizedFolders' => Str::of(\trans('models.classes.directory'))->plural(),
'role' => \trans('validation.custom.role'),
'status' => \trans('validation.custom.status')
];
}
}