| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets; |
| 4 |
|
| 5 |
use FluentSupport\App\Modules\PermissionManager; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
|
| 8 |
class AgentTicketAccess |
| 9 |
{ |
| 10 |
public function currentAgentCanAccess($ticket) |
| 11 |
{ |
| 12 |
return $this->canAccess(Helper::getAgentByUserId(), $ticket); |
| 13 |
} |
| 14 |
|
| 15 |
public function canAccess($agent, $ticket, $restrictions = null) |
| 16 |
{ |
| 17 |
if (!$agent || empty($agent->user_id) || !$ticket || empty($ticket->id)) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
|
| 21 |
if ($this->isMailboxRestricted($agent, $ticket, $restrictions)) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
$visibility = PermissionManager::getAgentTicketVisibility($agent->user_id); |
| 26 |
|
| 27 |
if ($visibility === PermissionManager::VISIBILITY_ALL) { |
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
if ((int) $ticket->agent_id === (int) $agent->id) { |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
return !$ticket->agent_id && $visibility === PermissionManager::VISIBILITY_ASSIGNED_AND_UNASSIGNED; |
| 36 |
} |
| 37 |
|
| 38 |
public function applyAccessScope($query, $agent = null, $restrictions = null) |
| 39 |
{ |
| 40 |
$agent = $agent ?: Helper::getAgentByUserId(); |
| 41 |
|
| 42 |
if (!$agent || empty($agent->user_id)) { |
| 43 |
$query->where('id', 0); |
| 44 |
return $query; |
| 45 |
} |
| 46 |
|
| 47 |
$this->applyVisibilityScope($query, $agent); |
| 48 |
$this->applyMailboxRestrictionScope($query, $agent, $restrictions); |
| 49 |
|
| 50 |
return $query; |
| 51 |
} |
| 52 |
|
| 53 |
public function applyVisibilityScope($query, $agent = null) |
| 54 |
{ |
| 55 |
$agent = $agent ?: Helper::getAgentByUserId(); |
| 56 |
|
| 57 |
if (!$agent || empty($agent->user_id)) { |
| 58 |
$query->where('id', 0); |
| 59 |
return $query; |
| 60 |
} |
| 61 |
|
| 62 |
$visibility = PermissionManager::getAgentTicketVisibility($agent->user_id); |
| 63 |
|
| 64 |
if ($visibility === PermissionManager::VISIBILITY_ALL) { |
| 65 |
return $query; |
| 66 |
} |
| 67 |
|
| 68 |
if ($visibility === PermissionManager::VISIBILITY_ASSIGNED_ONLY) { |
| 69 |
$query->where('agent_id', $agent->id); |
| 70 |
return $query; |
| 71 |
} |
| 72 |
|
| 73 |
$query->where(function ($q) use ($agent) { |
| 74 |
$q->where('agent_id', $agent->id); |
| 75 |
$q->orWhereNull('agent_id'); |
| 76 |
}); |
| 77 |
|
| 78 |
return $query; |
| 79 |
} |
| 80 |
|
| 81 |
public function applyMailboxRestrictionScope($query, $agent = null, $restrictions = null) |
| 82 |
{ |
| 83 |
$agent = $agent ?: Helper::getAgentByUserId(); |
| 84 |
$restrictedBoxes = $this->getRestrictedMailboxIds($agent, $restrictions); |
| 85 |
|
| 86 |
if ($restrictedBoxes) { |
| 87 |
$query->where(function ($q) use ($restrictedBoxes) { |
| 88 |
$q->whereNotIn('mailbox_id', $restrictedBoxes); |
| 89 |
$q->orWhereNull('mailbox_id'); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
return $query; |
| 94 |
} |
| 95 |
|
| 96 |
public function getRestrictedMailboxIds($agent = null, $restrictions = null) |
| 97 |
{ |
| 98 |
$agent = $agent ?: Helper::getAgentByUserId(); |
| 99 |
|
| 100 |
if (!$agent) { |
| 101 |
return []; |
| 102 |
} |
| 103 |
|
| 104 |
if ($restrictions === null) { |
| 105 |
$restrictions = $agent->getMeta('agent_restrictions', []); |
| 106 |
} |
| 107 |
|
| 108 |
if (!empty($restrictions['businessBoxRestrictions']) && !empty($restrictions['restrictedBusinessBoxes'])) { |
| 109 |
return array_values(array_unique(array_map('intval', $restrictions['restrictedBusinessBoxes']))); |
| 110 |
} |
| 111 |
|
| 112 |
return []; |
| 113 |
} |
| 114 |
|
| 115 |
protected function isMailboxRestricted($agent, $ticket, $restrictions = null) |
| 116 |
{ |
| 117 |
$restrictedBoxes = $this->getRestrictedMailboxIds($agent, $restrictions); |
| 118 |
|
| 119 |
return !empty($ticket->mailbox_id) && in_array((int) $ticket->mailbox_id, $restrictedBoxes, true); |
| 120 |
} |
| 121 |
} |
| 122 |
|