PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.1
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.1
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Modules / PermissionManager.php

PermissionManager.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.4.1, at app/Modules/PermissionManager.php

164 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 hasTicketPermission($ticket)
113 {
114 $permissionLevel = self::currentUserTicketsPermissionLevel();
115 if ($permissionLevel == 'all') {
116 return true;
117 }
118 $agent = Helper::getAgentByUserId();
119 if ($ticket->agent_id == $agent->id) {
120 return true;
121 }
122
123 return !$ticket->agent_id && $permissionLevel == 'own_plus';
124 }
125
126 public static function getReadablePermissionGroups()
127 {
128 return [
129 [
130 'title' => 'Tickets Permissions',
131 'permissions' => [
132 'fst_view_dashboard' => 'View Dashboard',
133 'fst_manage_own_tickets' => 'Manage Own Tickets',
134 'fst_manage_unassigned_tickets' => 'Manage Unassigned Tickets',
135 'fst_manage_other_tickets' => 'Manage Others Tickets',
136 'fst_delete_tickets' => 'Delete Tickets',
137 ]
138 ],
139 [
140 'title' => 'Workflow Permissions',
141 'permissions' => [
142 'fst_manage_workflows' => 'Manage Workflows',
143 'fst_run_workflows' => 'Run workflows',
144 'fst_manage_saved_replies' => 'Manage Saved Replies'
145 ]
146 ],
147 [
148 'title' => 'Settings',
149 'permissions' => [
150 'fst_manage_settings' => 'Manage Overall Settings',
151 'fst_sensitive_data' => 'Access Private Data (Customers, Agents)'
152 ]
153 ],
154 [
155 'title' => 'Reporting',
156 'permissions' => [
157 'fst_view_all_reports' => 'View All Reports',
158 'fst_view_activity_logs' => 'View Activity Logs'
159 ]
160 ]
161 ];
162 }
163 }
164