ldc-supportweb/app/Http/Requests/Bo/CreateMessage.php
2025-04-23 12:24:31 +02:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Http\Requests\Bo;
use App\Enums\MessageTypeEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rules\Enum;
class CreateMessage extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
/** @var \App\Models\User|null $user */
$user = Auth::user();
if (!($user)) {
return false;
}
if (
!$user->can('createPrivateMessage', $this->route()->ticket)
&& $this->is_private == MessageTypeEnum::private
) {
return false;
}
return Auth::check() and $user->sites and ($user->isAdmin() or $user->sites->contains(
$this->route()->ticket->site_id
));
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->merge([
'subscribe' => $this->subscribe ? true : false,
'type' => $this->is_private ? MessageTypeEnum::private->value : MessageTypeEnum::public->value
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
/** @var \App\Models\User $user */
$user = Auth::user();
return [
'subscribe' => 'required|boolean',
'message' => 'required|min:' . ($user->isAdmin() ? '5' : '20'),
'file_messages' => 'sometimes|array',
'file_messages.*' => 'mimes:pptx,docx,xlsx,csv,txt,jpeg,jpg,png,pdf|max:2048',
'type' => ['required', new Enum(MessageTypeEnum::class)]
];
}
}