| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
|
| 7 |
class PermissionManager |
| 8 |
{ |
| 9 |
public static function pluginPermissions() |
| 10 |
{ |
| 11 |
return [ |
| 12 |
'fst_view_dashboard', |
| 13 |
'fst_manage_own_tickets', |
| 14 |
'fst_manage_unassigned_tickets', |
| 15 |
'fst_manage_other_tickets', |
| 16 |
'fst_delete_tickets', |
| 17 |
'fst_manage_settings', |
| 18 |
'fst_sensitive_data', |
| 19 |
'fst_manage_workflows', |
| 20 |
'fst_run_workflows', |
| 21 |
'fst_view_all_reports', |
| 22 |
'fst_manage_saved_replies', |
| 23 |
'fst_view_activity_logs' |
| 24 |
]; |
| 25 |
} |
| 26 |
|
| 27 |
public static function attachPermissions($user, $permissions) |
| 28 |
{ |
| 29 |
if (is_numeric($user)) { |
| 30 |
$user = get_user_by('ID', $user); |
| 31 |
} |
| 32 |
|
| 33 |
if (!$user) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
if (user_can($user, 'manage_options')) { |
| 38 |
return $user; |
| 39 |
} |
| 40 |
|
| 41 |
$allPermissions = self::pluginPermissions(); |
| 42 |
foreach ($allPermissions as $permission) { |
| 43 |
$user->remove_cap($permission); |
| 44 |
} |
| 45 |
|
| 46 |
$permissions = array_intersect($allPermissions, $permissions); |
| 47 |
|
| 48 |
foreach ($permissions as $permission) { |
| 49 |
$user->add_cap($permission); |
| 50 |
} |
| 51 |
|
| 52 |
return $user; |
| 53 |
} |
| 54 |
|
| 55 |
public static function getUserPermissions($user = false) |
| 56 |
{ |
| 57 |
if (is_numeric($user)) { |
| 58 |
$user = get_user_by('ID', $user); |
| 59 |
} |
| 60 |
|
| 61 |
if (!$user) { |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
$pluginPermission = self::pluginPermissions(); |
| 66 |
|
| 67 |
if ($user->has_cap('manage_options')) { |
| 68 |
$pluginPermission[] = 'administrator'; |
| 69 |
return $pluginPermission; |
| 70 |
} |
| 71 |
|
| 72 |
return array_values(array_intersect(array_keys($user->allcaps), $pluginPermission)); |
| 73 |
} |
| 74 |
|
| 75 |
public static function currentUserPermissions($cached = true) |
| 76 |
{ |
| 77 |
static $permissions; |
| 78 |
|
| 79 |
if ($permissions && $cached) { |
| 80 |
return $permissions; |
| 81 |
} |
| 82 |
|
| 83 |
$permissions = self::getUserPermissions(get_current_user_id()); |
| 84 |
|
| 85 |
return $permissions; |
| 86 |
} |
| 87 |
|
| 88 |
public static function currentUserCan($permission) |
| 89 |
{ |
| 90 |
if (current_user_can('manage_options')) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
return current_user_can($permission); |
| 95 |
} |
| 96 |
|
| 97 |
public static function currentUserTicketsPermissionLevel() |
| 98 |
{ |
| 99 |
$permissions = self::currentUserPermissions(); |
| 100 |
|
| 101 |
if (in_array('fst_manage_other_tickets', $permissions)) { |
| 102 |
return 'all'; |
| 103 |
} |
| 104 |
|
| 105 |
if (in_array('fst_manage_unassigned_tickets', $permissions)) { |
| 106 |
return 'own_plus'; |
| 107 |
} |
| 108 |
|
| 109 |
return 'own'; |
| 110 |
} |
| 111 |
|
| 112 |
public static function agentTicketPermissionLevel($userId = false) |
| 113 |
{ |
| 114 |
if(!$userId) { |
| 115 |
$userId = get_current_user_id(); |
| 116 |
} |
| 117 |
|
| 118 |
$permissions = self::getUserPermissions($userId); |
| 119 |
|
| 120 |
if (in_array('fst_manage_other_tickets', $permissions)) { |
| 121 |
return 'all'; |
| 122 |
} |
| 123 |
|
| 124 |
if (in_array('fst_manage_unassigned_tickets', $permissions)) { |
| 125 |
return 'own_plus'; |
| 126 |
} |
| 127 |
|
| 128 |
return 'own'; |
| 129 |
} |
| 130 |
|
| 131 |
public static function hasTicketPermission($ticket) |
| 132 |
{ |
| 133 |
$permissionLevel = self::currentUserTicketsPermissionLevel(); |
| 134 |
if ($permissionLevel == 'all') { |
| 135 |
return true; |
| 136 |
} |
| 137 |
$agent = Helper::getAgentByUserId(); |
| 138 |
if ($ticket->agent_id == $agent->id) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
return !$ticket->agent_id && $permissionLevel == 'own_plus'; |
| 143 |
} |
| 144 |
|
| 145 |
public static function getReadablePermissionGroups() |
| 146 |
{ |
| 147 |
return [ |
| 148 |
[ |
| 149 |
'title' => __('Tickets Permissions', 'fluent-support'), |
| 150 |
'permissions' => [ |
| 151 |
'fst_view_dashboard' => __('View Dashboard', 'fluent-support'), |
| 152 |
'fst_manage_own_tickets' => __('Manage Own Tickets', 'fluent-support'), |
| 153 |
'fst_manage_unassigned_tickets' => __('Manage Unassigned Tickets', 'fluent-support'), |
| 154 |
'fst_manage_other_tickets' => __('Manage Others Tickets', 'fluent-support'), |
| 155 |
'fst_delete_tickets' => __('Delete Tickets', 'fluent-support'), |
| 156 |
] |
| 157 |
], |
| 158 |
[ |
| 159 |
'title' => __('Workflow Permissions', 'fluent-support'), |
| 160 |
'permissions' => [ |
| 161 |
'fst_manage_workflows' => __('Manage Workflows', 'fluent-support'), |
| 162 |
'fst_run_workflows' => __('Run workflows', 'fluent-support'), |
| 163 |
'fst_manage_saved_replies' => __('Manage Saved Replies', 'fluent-support') |
| 164 |
] |
| 165 |
], |
| 166 |
[ |
| 167 |
'title' => __('Settings', 'fluent-support'), |
| 168 |
'permissions' => [ |
| 169 |
'fst_manage_settings' => __('Manage Overall Settings', 'fluent-support'), |
| 170 |
'fst_sensitive_data' => __('Access Private Data (Customers, Agents)', 'fluent-support') |
| 171 |
] |
| 172 |
], |
| 173 |
[ |
| 174 |
'title' => __('Reporting', 'fluent-support'), |
| 175 |
'permissions' => [ |
| 176 |
'fst_view_all_reports' => __('View All Reports', 'fluent-support'), |
| 177 |
'fst_view_activity_logs' => __('View Activity Logs', 'fluent-support') |
| 178 |
] |
| 179 |
] |
| 180 |
]; |
| 181 |
} |
| 182 |
} |
| 183 |
|