86 خطوط
3.4 KiB
PHP
86 خطوط
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class StoreTaskRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'project_id' => 'required|exists:projects,id',
|
|
'assignee_id' => 'nullable|exists:users,id',
|
|
'priority' => ['nullable', Rule::in(['low', 'medium', 'high', 'urgent'])],
|
|
'status' => ['nullable', Rule::in(['waiting', 'todo', 'in_progress', 'review', 'done', 'canceled', 'cancelled'])],
|
|
'blocker_type' => 'nullable|string|max:80',
|
|
'blocker_note' => 'nullable|string',
|
|
'start_date' => 'nullable|date',
|
|
'due_date' => 'nullable|date|after_or_equal:today',
|
|
'estimated_time' => 'nullable|numeric',
|
|
'actual_time' => 'nullable|numeric',
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'title.required' => 'عنوان تسک الزامی است.',
|
|
'title.max' => 'عنوان تسک نباید بیشتر از ۲۵۵ کاراکتر باشد.',
|
|
'project_id.required' => 'انتخاب پروژه الزامی است.',
|
|
'project_id.exists' => 'پروژه انتخابشده معتبر نیست.',
|
|
'assignee_id.exists' => 'مسئول انتخابشده معتبر نیست.',
|
|
'priority.in' => 'اولویت انتخابشده معتبر نیست.',
|
|
'status.in' => 'وضعیت انتخابشده معتبر نیست.',
|
|
'due_date.date' => 'مهلت انجام معتبر نیست.',
|
|
'due_date.after_or_equal' => 'مهلت انجام نمیتواند قبل از امروز باشد.',
|
|
];
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function (Validator $validator) {
|
|
$user = $this->user()?->loadMissing('roles.permissions');
|
|
$project = Project::with('members:id')->find($this->input('project_id'));
|
|
|
|
if (!$user || !$project) {
|
|
return;
|
|
}
|
|
|
|
$canUseProject = $user->hasPermission('reports.view')
|
|
|| (int) $project->project_manager_id === (int) $user->id
|
|
|| (int) $project->created_by === (int) $user->id
|
|
|| $project->members->contains('id', $user->id);
|
|
|
|
if (!$canUseProject) {
|
|
$validator->errors()->add('project_id', 'شما به این پروژه دسترسی ندارید.');
|
|
}
|
|
|
|
if ($this->filled('assignee_id')) {
|
|
$assignee = User::find($this->input('assignee_id'));
|
|
$assigneeAllowed = $assignee && (
|
|
$user->hasPermission('reports.view')
|
|
|| (int) $assignee->id === (int) $user->id
|
|
|| $project->members->contains('id', $assignee->id)
|
|
|| (int) $project->project_manager_id === (int) $assignee->id
|
|
);
|
|
|
|
if (!$assigneeAllowed) {
|
|
$validator->errors()->add('assignee_id', 'مسئول انتخابشده برای این پروژه مجاز نیست.');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|