64 lines
1.5 KiB
PHP
Executable File
64 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Policies;
|
|
|
|
use App\Models\User;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
|
|
final class TicketPolicy
|
|
{
|
|
public function viewAny(User $user): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function view(User $user, Ticket $ticket): bool
|
|
{
|
|
if ($this->isAdmin($user) || $this->isAgent($user)) {
|
|
return true;
|
|
}
|
|
|
|
return (int) $ticket->user_id === (int) $user->id;
|
|
}
|
|
|
|
public function create(User $user): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function update(User $user, Ticket $ticket): bool
|
|
{
|
|
if ($this->isAdmin($user) || $this->isAgent($user)) {
|
|
return true;
|
|
}
|
|
|
|
return (int) $ticket->user_id === (int) $user->id;
|
|
}
|
|
|
|
public function comment(User $user, Ticket $ticket): bool
|
|
{
|
|
return $this->view($user, $ticket);
|
|
}
|
|
|
|
public function delete(User $user, Ticket $ticket): bool
|
|
{
|
|
return $this->isAdmin($user);
|
|
}
|
|
|
|
private function isAdmin(User $user): bool
|
|
{
|
|
return (method_exists($user, 'isHelpdeskAdmin') && $user->isHelpdeskAdmin())
|
|
|| (bool) ($user->helpdesk_admin ?? false)
|
|
|| (method_exists($user, 'isAdmin') && $user->isAdmin());
|
|
}
|
|
|
|
private function isAgent(User $user): bool
|
|
{
|
|
return (method_exists($user, 'isHelpdeskAgent') && $user->isHelpdeskAgent())
|
|
|| (bool) ($user->helpdesk_agent ?? false)
|
|
|| (method_exists($user, 'isAgent') && $user->isAgent());
|
|
}
|
|
}
|