| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules\MCP\Support; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Ticket; |
| 7 |
use FluentSupport\App\Modules\MCP\Helpers\MCPHelper; |
| 8 |
use FluentSupport\App\Modules\PermissionManager; |
| 9 |
|
| 10 |
class TicketAccessGuard |
| 11 |
{ |
| 12 |
private static $restricted = null; |
| 13 |
|
| 14 |
private static function restrictedMailboxIds() |
| 15 |
{ |
| 16 |
if (self::$restricted === null) { |
| 17 |
self::$restricted = PermissionManager::getRestrictedMailboxIds() ?: []; |
| 18 |
} |
| 19 |
return self::$restricted; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Assert the current user can read/write $ticket. |
| 24 |
* |
| 25 |
* Checks both the canAccessTicket visibility rule and any mailbox |
| 26 |
* restrictions the agent has been assigned. Returns WP_Error on the first |
| 27 |
* failure, null on success — so callers do: |
| 28 |
* |
| 29 |
* if ($err = TicketAccessGuard::assert($ticket)) { |
| 30 |
* return $err; |
| 31 |
* } |
| 32 |
*/ |
| 33 |
public static function assert(Ticket $ticket) |
| 34 |
{ |
| 35 |
if (!PermissionManager::canAccessTicket($ticket)) { |
| 36 |
return MCPHelper::error('forbidden', __('You do not have access to this ticket', 'fluent-support')); |
| 37 |
} |
| 38 |
|
| 39 |
$restricted = self::restrictedMailboxIds(); |
| 40 |
if ($restricted && in_array((int) $ticket->mailbox_id, array_map('intval', $restricted), true)) { |
| 41 |
return MCPHelper::error('forbidden', __('You do not have access to this ticket', 'fluent-support')); |
| 42 |
} |
| 43 |
|
| 44 |
return null; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Assert the current user can create tickets in / move tickets to $mailboxId. |
| 49 |
* Returns WP_Error on failure, null on success. |
| 50 |
*/ |
| 51 |
public static function assertMailboxWritable($mailboxId) |
| 52 |
{ |
| 53 |
$restricted = self::restrictedMailboxIds(); |
| 54 |
if ($restricted && in_array($mailboxId, array_map('intval', $restricted), true)) { |
| 55 |
return MCPHelper::error('forbidden', __('You do not have access to the specified mailbox', 'fluent-support')); |
| 56 |
} |
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Assert that $targetAgent is not restricted from the mailbox of $ticket. |
| 62 |
* Returns WP_Error on failure, null on success. |
| 63 |
*/ |
| 64 |
public static function assertAssignableAgent(Ticket $ticket, Agent $targetAgent) |
| 65 |
{ |
| 66 |
$meta = $targetAgent->getMeta('agent_restrictions'); |
| 67 |
$agentRestricted = (!empty($meta['businessBoxRestrictions']) && !empty($meta['restrictedBusinessBoxes'])) |
| 68 |
? $meta['restrictedBusinessBoxes'] |
| 69 |
: []; |
| 70 |
|
| 71 |
if ($agentRestricted && in_array((int) $ticket->mailbox_id, array_map('intval', $agentRestricted), true)) { |
| 72 |
return MCPHelper::error('forbidden', __("That agent is restricted from this ticket's mailbox", 'fluent-support')); |
| 73 |
} |
| 74 |
|
| 75 |
return null; |
| 76 |
} |
| 77 |
} |
| 78 |
|