101 lines
2.4 KiB
PHP
101 lines
2.4 KiB
PHP
<?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->isHelpdeskAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->isHelpdeskAgent($user)) {
|
|
return (int) $ticket->agent_id === (int) $user->id
|
|
|| (int) $ticket->user_id === (int) $user->id;
|
|
}
|
|
|
|
return (int) $ticket->user_id === (int) $user->id;
|
|
}
|
|
|
|
public function create(User $user): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Helpdesk workflow updates.
|
|
*
|
|
* Admins can update every Helpdesk ticket.
|
|
*
|
|
* Agents can update any ticket that is visible to them under the Helpdesk
|
|
* visibility rules. This is important for Kanban drag/drop: a card must
|
|
* never be displayed as movable and then fail authorization on drop.
|
|
*
|
|
* Normal requesters cannot change workflow/status/assignment data.
|
|
*/
|
|
public function update(User $user, Ticket $ticket): bool
|
|
{
|
|
if ($this->isHelpdeskAdmin($user)) {
|
|
return true;
|
|
}
|
|
|
|
if (! $this->isHelpdeskAgent($user)) {
|
|
return false;
|
|
}
|
|
|
|
return (int) $ticket->agent_id === (int) $user->id
|
|
|| (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->isHelpdeskAdmin($user);
|
|
}
|
|
|
|
private function isHelpdeskAdmin(User $user): bool
|
|
{
|
|
if (method_exists($user, 'isHelpdeskAdmin')) {
|
|
return $user->isHelpdeskAdmin();
|
|
}
|
|
|
|
if ((bool) ($user->helpdesk_admin ?? false)) {
|
|
return true;
|
|
}
|
|
|
|
if (method_exists($user, 'isAdmin')) {
|
|
return $user->isAdmin();
|
|
}
|
|
|
|
if (method_exists($user, 'isMpbaPlatformAdmin')) {
|
|
return $user->isMpbaPlatformAdmin();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function isHelpdeskAgent(User $user): bool
|
|
{
|
|
if (method_exists($user, 'isHelpdeskAgent')) {
|
|
return $user->isHelpdeskAgent();
|
|
}
|
|
|
|
return (bool) ($user->helpdesk_agent ?? false);
|
|
}
|
|
}
|