ldc-com-app-bo/app/Http/Requests/Bo/AppUser/StoreAppUserRequest.php

94 lines
2.8 KiB
PHP
Raw Normal View History

2023-03-14 16:17:23 +01:00
<?php
namespace App\Http\Requests\Bo\AppUser;
2023-03-15 10:41:01 +01:00
use App\Enums\AppUserRolesEnum;
use App\Models\AppUser;
use Illuminate\Database\Query\Builder;
2023-03-14 16:17:23 +01:00
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
2025-05-20 16:20:23 +02:00
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;
2023-07-25 12:55:22 +02:00
use Illuminate\Validation\Rules\Password;
2023-03-14 16:17:23 +01:00
class StoreAppUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
2023-03-15 10:41:01 +01:00
* @return boolean
2023-03-14 16:17:23 +01:00
*/
public function authorize(): bool
2023-03-14 16:17:23 +01:00
{
return Gate::check('create', AppUser::class);
2023-03-14 16:17:23 +01:00
}
2023-03-31 15:24:32 +02:00
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
2023-03-31 15:24:32 +02:00
{
$this->merge([
'status' => $this->status ? true : false,
2023-03-31 15:24:32 +02:00
'authorizedFolders' => $this->authorizedFolders ? $this->authorizedFolders : [],
]);
}
2023-03-14 16:17:23 +01:00
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
2023-03-14 16:17:23 +01:00
{
return [
'lastname' => 'required|min:3|max:255',
'firstname' => 'required|min:3|max:255',
2025-05-20 16:20:23 +02:00
'email' => [
'required',
'email:rfc,strict,dns,spoof,filter',
2025-06-17 12:29:15 +02:00
Rule::unique('app_users', 'email')
->where(fn(Builder $query) => $query->where('tenant_id', tenant('id')))
2025-05-20 16:20:23 +02:00
],
'password' => [
'required',
'required_with:password_confirmation',
'confirmed',
'max:255',
2023-07-25 12:55:22 +02:00
Password::min(8)
->letters()
->mixedCase()
->numbers()
->symbols()
],
2023-03-31 15:24:32 +02:00
'authorizedFolders' => 'present|array',
'role' => [
2023-03-15 10:41:01 +01:00
'required',
new Enum(AppUserRolesEnum::class)
2023-03-15 10:41:01 +01:00
],
'status' => 'required|boolean'
2023-03-15 10:41:01 +01:00
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
2023-03-15 10:41:01 +01:00
{
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')
2023-03-14 16:17:23 +01:00
];
}
}