| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* PermissionManager class is responsible for getting/settings data related to permission |
| 9 |
* @package FluentSupport\App\Modules |
| 10 |
* |
| 11 |
* @version 1.0.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
class PermissionManager |
| 15 |
{ |
| 16 |
/** |
| 17 |
* pluginPermissions method will return the list of permissions support by Fluent Support Plugin |
| 18 |
* @return string[] |
| 19 |
*/ |
| 20 |
public static function pluginPermissions() |
| 21 |
{ |
| 22 |
return [ |
| 23 |
'fst_view_dashboard', |
| 24 |
'fst_manage_own_tickets', |
| 25 |
'fst_manage_unassigned_tickets', |
| 26 |
'fst_manage_other_tickets', |
| 27 |
'fst_delete_tickets', |
| 28 |
'fst_manage_settings', |
| 29 |
'fst_sensitive_data', |
| 30 |
'fst_manage_workflows', |
| 31 |
'fst_run_workflows', |
| 32 |
'fst_view_all_reports', |
| 33 |
'fst_manage_saved_replies', |
| 34 |
'fst_view_activity_logs' |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* attachPermissions method will add selected permission to the user |
| 40 |
* @param $user |
| 41 |
* @param $permissions |
| 42 |
* @return false|mixed |
| 43 |
*/ |
| 44 |
public static function attachPermissions($user, $permissions) |
| 45 |
{ |
| 46 |
if (is_numeric($user)) { |
| 47 |
$user = get_user_by('ID', $user); |
| 48 |
} |
| 49 |
|
| 50 |
if (!$user) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if (user_can($user, 'manage_options')) { |
| 55 |
return $user; |
| 56 |
} |
| 57 |
|
| 58 |
$allPermissions = self::pluginPermissions(); |
| 59 |
foreach ($allPermissions as $permission) { |
| 60 |
$user->remove_cap($permission); |
| 61 |
} |
| 62 |
|
| 63 |
$permissions = array_intersect($allPermissions, $permissions); |
| 64 |
|
| 65 |
foreach ($permissions as $permission) { |
| 66 |
$user->add_cap($permission); |
| 67 |
} |
| 68 |
|
| 69 |
return $user; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* getUserPermissions method will get all permissions for a user |
| 74 |
* @param false $user |
| 75 |
* @return array|string[] |
| 76 |
*/ |
| 77 |
public static function getUserPermissions($user = false) |
| 78 |
{ |
| 79 |
if (is_numeric($user)) { |
| 80 |
$user = get_user_by('ID', $user); |
| 81 |
} |
| 82 |
|
| 83 |
if (!$user) { |
| 84 |
return []; |
| 85 |
} |
| 86 |
|
| 87 |
$pluginPermission = self::pluginPermissions(); |
| 88 |
|
| 89 |
if ($user->has_cap('manage_options')) { |
| 90 |
$pluginPermission[] = 'administrator'; |
| 91 |
return $pluginPermission; |
| 92 |
} |
| 93 |
|
| 94 |
return array_values(array_intersect(array_keys($user->allcaps), $pluginPermission)); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* currentUserPermissions method will return the permission of logged-in user |
| 99 |
* @param bool $cached |
| 100 |
* @return array|mixed|string[] |
| 101 |
*/ |
| 102 |
public static function currentUserPermissions($cached = true) |
| 103 |
{ |
| 104 |
static $permissions; |
| 105 |
|
| 106 |
if ($permissions && $cached) { |
| 107 |
return $permissions; |
| 108 |
} |
| 109 |
|
| 110 |
$permissions = self::getUserPermissions(get_current_user_id()); |
| 111 |
|
| 112 |
return $permissions; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* currentUserCan method will return whether a user has the selected permission or not |
| 117 |
* @param $permission |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public static function currentUserCan($permission) |
| 121 |
{ |
| 122 |
if (current_user_can('manage_options')) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
return current_user_can($permission); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* currentUserTicketsPermissionLevel method will return the permission level for a user in tickets |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public static function currentUserTicketsPermissionLevel() |
| 134 |
{ |
| 135 |
$permissions = self::currentUserPermissions(); |
| 136 |
|
| 137 |
if (in_array('fst_manage_other_tickets', $permissions)) { |
| 138 |
return 'all'; |
| 139 |
} |
| 140 |
|
| 141 |
if (in_array('fst_manage_unassigned_tickets', $permissions)) { |
| 142 |
return 'own_plus'; |
| 143 |
} |
| 144 |
|
| 145 |
return 'own'; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* agentTicketPermissionLevel method will return the access level of an agent in tickets |
| 150 |
* @param false $userId |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public static function agentTicketPermissionLevel($userId = false) |
| 154 |
{ |
| 155 |
if(!$userId) { |
| 156 |
$userId = get_current_user_id(); |
| 157 |
} |
| 158 |
|
| 159 |
$permissions = self::getUserPermissions($userId); |
| 160 |
|
| 161 |
if (in_array('fst_manage_other_tickets', $permissions)) { |
| 162 |
return 'all'; |
| 163 |
} |
| 164 |
|
| 165 |
if (in_array('fst_manage_unassigned_tickets', $permissions)) { |
| 166 |
return 'own_plus'; |
| 167 |
} |
| 168 |
|
| 169 |
return 'own'; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* hasTicketPermission method will return whether the selected user has permission in selected ticket or not |
| 174 |
* @param $ticket |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public static function hasTicketPermission($ticket) |
| 178 |
{ |
| 179 |
$permissionLevel = self::currentUserTicketsPermissionLevel(); |
| 180 |
if ($permissionLevel == 'all') { |
| 181 |
return true; |
| 182 |
} |
| 183 |
$agent = Helper::getAgentByUserId(); |
| 184 |
if ($ticket->agent_id == $agent->id) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return !$ticket->agent_id && $permissionLevel == 'own_plus'; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* getReadablePermissionGroups method will return the permission group as array |
| 193 |
* @return array[] |
| 194 |
*/ |
| 195 |
public static function getReadablePermissionGroups() |
| 196 |
{ |
| 197 |
return [ |
| 198 |
[ |
| 199 |
'title' => __('Tickets Permissions', 'fluent-support'), |
| 200 |
'permissions' => [ |
| 201 |
'fst_view_dashboard' => __('View Dashboard', 'fluent-support'), |
| 202 |
'fst_manage_own_tickets' => __('Manage Own Tickets', 'fluent-support'), |
| 203 |
'fst_manage_unassigned_tickets' => __('Manage Unassigned Tickets', 'fluent-support'), |
| 204 |
'fst_manage_other_tickets' => __('Manage Others Tickets', 'fluent-support'), |
| 205 |
'fst_delete_tickets' => __('Delete Tickets', 'fluent-support'), |
| 206 |
] |
| 207 |
], |
| 208 |
[ |
| 209 |
'title' => __('Workflow Permissions', 'fluent-support'), |
| 210 |
'permissions' => [ |
| 211 |
'fst_manage_workflows' => __('Manage Workflows', 'fluent-support'), |
| 212 |
'fst_run_workflows' => __('Run workflows', 'fluent-support'), |
| 213 |
'fst_manage_saved_replies' => __('Manage Saved Replies', 'fluent-support') |
| 214 |
] |
| 215 |
], |
| 216 |
[ |
| 217 |
'title' => __('Settings', 'fluent-support'), |
| 218 |
'permissions' => [ |
| 219 |
'fst_manage_settings' => __('Manage Overall Settings', 'fluent-support'), |
| 220 |
'fst_sensitive_data' => __('Access Private Data (Customers, Agents)', 'fluent-support') |
| 221 |
] |
| 222 |
], |
| 223 |
[ |
| 224 |
'title' => __('Reporting', 'fluent-support'), |
| 225 |
'permissions' => [ |
| 226 |
'fst_view_all_reports' => __('View All Reports', 'fluent-support'), |
| 227 |
'fst_view_activity_logs' => __('View Activity Logs', 'fluent-support') |
| 228 |
] |
| 229 |
] |
| 230 |
]; |
| 231 |
} |
| 232 |
} |
| 233 |
|