| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\MailBox; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* PermissionManager class is responsible for getting/settings data related to permission |
| 10 |
* @package FluentSupport\App\Modules |
| 11 |
* |
| 12 |
* @version 1.0.0 |
| 13 |
*/ |
| 14 |
|
| 15 |
class PermissionManager |
| 16 |
{ |
| 17 |
/** |
| 18 |
* pluginPermissions method will return the list of permissions support by Fluent Support Plugin |
| 19 |
* @return string[] |
| 20 |
*/ |
| 21 |
public static function pluginPermissions() |
| 22 |
{ |
| 23 |
return [ |
| 24 |
'fst_view_dashboard', |
| 25 |
'fst_manage_own_tickets', |
| 26 |
'fst_manage_unassigned_tickets', |
| 27 |
'fst_manage_other_tickets', |
| 28 |
'fst_delete_tickets', |
| 29 |
'fst_assign_agents', |
| 30 |
'fst_manage_settings', |
| 31 |
'fst_sensitive_data', |
| 32 |
'fst_manage_workflows', |
| 33 |
'fst_run_workflows', |
| 34 |
'fst_view_all_reports', |
| 35 |
'fst_manage_saved_replies', |
| 36 |
'fst_view_activity_logs', |
| 37 |
'fst_merge_tickets', |
| 38 |
'fst_split_ticket', |
| 39 |
'fst_agent_today_performance', |
| 40 |
'fst_draft_reply', |
| 41 |
'fst_approve_draft_reply' |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* attachPermissions method will add selected permission to the user |
| 47 |
* @param $user |
| 48 |
* @param $permissions |
| 49 |
* @return false|mixed |
| 50 |
*/ |
| 51 |
public static function attachPermissions($user, $permissions) |
| 52 |
{ |
| 53 |
if (is_numeric($user)) { |
| 54 |
$user = get_user_by('ID', $user); |
| 55 |
} |
| 56 |
|
| 57 |
if (!$user) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
if (user_can($user, 'manage_options')) { |
| 62 |
return $user; |
| 63 |
} |
| 64 |
|
| 65 |
$allPermissions = self::pluginPermissions(); |
| 66 |
foreach ($allPermissions as $permission) { |
| 67 |
$user->remove_cap($permission); |
| 68 |
} |
| 69 |
|
| 70 |
$permissions = array_intersect($allPermissions, $permissions); |
| 71 |
|
| 72 |
$filterPermissionConditions = self::filterPermissionConditions(); |
| 73 |
$permissions = self::filterPermissionsByConditions($permissions, $filterPermissionConditions); |
| 74 |
|
| 75 |
foreach ($permissions as $permission) { |
| 76 |
$user->add_cap($permission); |
| 77 |
} |
| 78 |
|
| 79 |
return $user; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Filters permissions based on specified conditions. |
| 84 |
* |
| 85 |
* @param array $permissions The array of permissions to filter. |
| 86 |
* @param array $conditions The conditions used for filtering. |
| 87 |
* @return array The filtered array of permissions. |
| 88 |
*/ |
| 89 |
public static function filterPermissionsByConditions($permissions, $conditions) |
| 90 |
{ |
| 91 |
foreach ($conditions as $requiredKey => $removeKey) { |
| 92 |
if (in_array($requiredKey, $permissions) && in_array($removeKey, $permissions)) { |
| 93 |
unset($permissions[array_search($removeKey, $permissions)]); |
| 94 |
} |
| 95 |
} |
| 96 |
return $permissions; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Retrieves the permission filter conditions. |
| 101 |
* |
| 102 |
* @return array The array of permission filter conditions. |
| 103 |
*/ |
| 104 |
public static function filterPermissionConditions() |
| 105 |
{ |
| 106 |
return [ |
| 107 |
'fst_manage_unassigned_tickets' => 'fst_draft_reply', |
| 108 |
'fst_manage_other_tickets' => 'fst_draft_reply', |
| 109 |
'fst_manage_own_tickets' => 'fst_draft_reply', |
| 110 |
'fst_draft_reply' => 'fst_approve_draft_reply' |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* getUserPermissions method will get all permissions for a user |
| 116 |
* @param false $user |
| 117 |
* @return array|string[] |
| 118 |
*/ |
| 119 |
public static function getUserPermissions($user = false) |
| 120 |
{ |
| 121 |
if (is_numeric($user)) { |
| 122 |
$user = get_user_by('ID', $user); |
| 123 |
} |
| 124 |
|
| 125 |
if (!$user) { |
| 126 |
return []; |
| 127 |
} |
| 128 |
|
| 129 |
$pluginPermission = self::pluginPermissions(); |
| 130 |
|
| 131 |
if ($user->has_cap('manage_options')) { |
| 132 |
$pluginPermission[] = 'administrator'; |
| 133 |
$pluginPermission = array_values(array_diff($pluginPermission, ['fst_draft_reply'])); //Remove draft reply permission from here |
| 134 |
return $pluginPermission; |
| 135 |
} |
| 136 |
|
| 137 |
return array_values(array_intersect(array_keys($user->allcaps), $pluginPermission)); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* currentUserPermissions method will return the permission of logged-in user |
| 142 |
* @param bool $cached |
| 143 |
* @return array|mixed|string[] |
| 144 |
*/ |
| 145 |
public static function currentUserPermissions($cached = true) |
| 146 |
{ |
| 147 |
static $permissions; |
| 148 |
|
| 149 |
if ($permissions && $cached) { |
| 150 |
return $permissions; |
| 151 |
} |
| 152 |
|
| 153 |
$permissions = self::getUserPermissions(get_current_user_id()); |
| 154 |
|
| 155 |
return $permissions; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Retrieve the list of restricted business boxes for the current user. |
| 160 |
* |
| 161 |
* This method fetches the list of restricted business boxes from the metadata of the current user's agent profile. |
| 162 |
* If business box restrictions are enabled for the user, it returns the list of restricted business boxes; otherwise, |
| 163 |
* it returns an empty array. |
| 164 |
* |
| 165 |
* @return array The list of restricted business boxes for the current user. |
| 166 |
*/ |
| 167 |
public static function currentUserRestrictedBusinessBoxes() |
| 168 |
{ |
| 169 |
$agent = Helper::getAgentByUserId(); |
| 170 |
$restrictions = $agent->getMeta('agent_restrictions'); |
| 171 |
|
| 172 |
return isset($restrictions['businessBoxRestrictions']) ? $restrictions['restrictedBusinessBoxes'] : []; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* currentUserCan method will return whether a user has the selected permission or not |
| 178 |
* @param $permission |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
public static function currentUserCan($permission) |
| 182 |
{ |
| 183 |
if (current_user_can('manage_options')) { |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
return current_user_can($permission); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* currentUserTicketsPermissionLevel method will return the permission level for a user in tickets |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
public static function currentUserTicketsPermissionLevel() |
| 195 |
{ |
| 196 |
$permissions = self::currentUserPermissions(); |
| 197 |
|
| 198 |
if (in_array('fst_manage_other_tickets', $permissions)) { |
| 199 |
return 'all'; |
| 200 |
} |
| 201 |
|
| 202 |
if (in_array('fst_draft_reply', $permissions) || in_array('fst_manage_unassigned_tickets', $permissions)) { |
| 203 |
return 'own_plus'; |
| 204 |
} |
| 205 |
|
| 206 |
return 'own'; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* agentTicketPermissionLevel method will return the access level of an agent in tickets |
| 211 |
* @param false $userId |
| 212 |
* @return string |
| 213 |
*/ |
| 214 |
public static function agentTicketPermissionLevel($userId = false) |
| 215 |
{ |
| 216 |
if(!$userId) { |
| 217 |
$userId = get_current_user_id(); |
| 218 |
} |
| 219 |
|
| 220 |
$permissions = self::getUserPermissions($userId); |
| 221 |
|
| 222 |
if (in_array('fst_manage_other_tickets', $permissions)) { |
| 223 |
return 'all'; |
| 224 |
} |
| 225 |
|
| 226 |
if (in_array('fst_draft_reply', $permissions) || in_array('fst_manage_unassigned_tickets', $permissions)) { |
| 227 |
return 'own_plus'; |
| 228 |
} |
| 229 |
|
| 230 |
return 'own'; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* hasTicketPermission method will return whether the selected user has permission in selected ticket or not |
| 235 |
* @param $ticket |
| 236 |
* @return bool |
| 237 |
*/ |
| 238 |
public static function hasTicketPermission($ticket) |
| 239 |
{ |
| 240 |
$permissionLevel = self::currentUserTicketsPermissionLevel(); |
| 241 |
|
| 242 |
if ($permissionLevel == 'all') { |
| 243 |
return true; |
| 244 |
} |
| 245 |
|
| 246 |
$agent = Helper::getAgentByUserId(); |
| 247 |
|
| 248 |
if ($ticket->agent_id == $agent->id) { |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
return !$ticket->agent_id && $permissionLevel == 'own_plus'; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* getReadablePermissionGroups method will return the permission group as array |
| 257 |
* @return array[] |
| 258 |
*/ |
| 259 |
public static function getReadablePermissionGroups() |
| 260 |
{ |
| 261 |
return [ |
| 262 |
[ |
| 263 |
'title' => __('Tickets Permissions', 'fluent-support'), |
| 264 |
'permissions' => [ |
| 265 |
'fst_view_dashboard' => __('View Dashboard', 'fluent-support'), |
| 266 |
'fst_manage_own_tickets' => __('Manage Own Tickets', 'fluent-support'), |
| 267 |
'fst_manage_unassigned_tickets' => __('Manage Unassigned Tickets', 'fluent-support'), |
| 268 |
'fst_manage_other_tickets' => __('Manage Others Tickets', 'fluent-support'), |
| 269 |
'fst_assign_agents' => __('Assign Agents', 'fluent-support'), |
| 270 |
'fst_delete_tickets' => __('Delete Tickets & Individual Responses', 'fluent-support'), |
| 271 |
'fst_merge_tickets' => __('Merge Tickets', 'fluent-support'), |
| 272 |
'fst_split_ticket' => __('Split Ticket', 'fluent-support'), |
| 273 |
'fst_draft_reply' => __('Draft Reply', 'fluent-support'), |
| 274 |
'fst_approve_draft_reply' => __('Approve Draft Reply', 'fluent-support'), |
| 275 |
] |
| 276 |
], |
| 277 |
[ |
| 278 |
'title' => __('Workflow Permissions', 'fluent-support'), |
| 279 |
'permissions' => [ |
| 280 |
'fst_manage_workflows' => __('Manage Workflows', 'fluent-support'), |
| 281 |
'fst_run_workflows' => __('Run workflows', 'fluent-support'), |
| 282 |
'fst_manage_saved_replies' => __('Manage Saved Replies', 'fluent-support') |
| 283 |
] |
| 284 |
], |
| 285 |
[ |
| 286 |
'title' => __('Settings', 'fluent-support'), |
| 287 |
'permissions' => [ |
| 288 |
'fst_manage_settings' => __('Manage Overall Settings', 'fluent-support'), |
| 289 |
'fst_sensitive_data' => __('Access Private Data (Customers, Agents)', 'fluent-support') |
| 290 |
] |
| 291 |
], |
| 292 |
[ |
| 293 |
'title' => __('Reporting', 'fluent-support'), |
| 294 |
'permissions' => [ |
| 295 |
'fst_view_all_reports' => __('View All Reports', 'fluent-support'), |
| 296 |
'fst_view_activity_logs' => __('View Activity Logs', 'fluent-support'), |
| 297 |
'fst_agent_today_performance' => __('View Agent Today Performance', 'fluent-support'), |
| 298 |
] |
| 299 |
] |
| 300 |
]; |
| 301 |
} |
| 302 |
|
| 303 |
public static function getBusinessBoxesForRestriction() |
| 304 |
{ |
| 305 |
return MailBox::select(['id', 'name'])->get(); |
| 306 |
} |
| 307 |
} |
| 308 |
|