PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Services / Permission / PermissionManager.php

PermissionManager.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.27, at app/Services/Permission/PermissionManager.php

304 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\Permission;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Helpers\Helper;
7 use FluentCart\Framework\Support\Arr;
8 use FluentCart\App\Models\User;
9
10 class PermissionManager
11 {
12
13 static $metaKey = '_fluent_cart_admin_role';
14 public const ADMIN_CAP = 'fluent_cart_admin';
15
16 public static function getAllRoles(): array
17 {
18
19 $allRoles = [
20 'super_admin' => [
21 'title' => __('Super Admin', 'fluent-cart'),
22 'descriptions' => __('With All Permissions', 'fluent-cart'),
23 'permissions' => array_keys(self::getAllPermissions())
24 ],
25 'manager' => [
26 'title' => __('Manager', 'fluent-cart'),
27 'descriptions' => __('With All Permissions Except Sensitive Settings', 'fluent-cart'),
28 'permissions' => [
29 'store/settings',
30 'products/view',
31 'products/create',
32 'products/edit',
33 'products/delete',
34 'customers/view',
35 'customers/manage',
36 'customers/delete',
37 'orders/view',
38 'orders/manage_statuses',
39 'orders/can_refund',
40 'orders/manage',
41 'orders/export',
42 'orders/delete',
43 'subscriptions/view',
44 'subscriptions/manage',
45 'subscriptions/delete',
46 'licenses/view',
47 'licenses/manage',
48 'licenses/delete',
49 'coupons/view',
50 'coupons/manage',
51 'coupons/delete',
52 'reports/view',
53 'reports/export',
54 'integrations/view',
55 'integrations/manage',
56 'integrations/delete'
57 ]
58 ],
59 'worker' => [
60 'title' => __('Worker', 'fluent-cart'),
61 'descriptions' => __('View Access for products, customers, coupons, integretions. Manage Access for Order Statuses', 'fluent-cart'),
62 'permissions' => [
63 'products/view',
64 'customers/view',
65 'orders/view',
66 'orders/manage_statuses',
67 'subscriptions/view',
68 'licenses/view',
69 'coupons/view',
70 'coupons/manage',
71 'integrations/view'
72 ]
73 ],
74 'accountant' => [
75 'title' => __('Accountant', 'fluent-cart'),
76 'descriptions' => __('View Access for products, customers, orders, subscriptions, licenses, coupons, reports and integrations', 'fluent-cart'),
77 'permissions' => [
78 'orders/view',
79 'orders/export',
80 'reports/view',
81 'reports/export',
82 'products/view',
83 'customers/view',
84 'subscriptions/view',
85 'licenses/view',
86 'coupons/view',
87 'integrations/view'
88 ]
89 ]
90 ];
91
92 return apply_filters('fluent_cart/permission/all_roles', $allRoles, []);
93 }
94
95 public static function getUserPermissions($userId = false)
96 {
97 if ($userId === false) {
98 $userId = get_current_user_id();
99 }
100
101 $user = get_user_by('ID', $userId);
102
103 if (user_can($user, 'manage_options')) {
104 $allPermissions = array_keys(self::getAllPermissions());
105 $allPermissions[] = 'is_super_admin';
106
107 return $allPermissions;
108 }
109
110 if (!App::isProActive()) {
111 return [];
112 }
113
114 $currentRole = get_user_meta($userId, static::$metaKey, true);
115
116
117 if (empty($currentRole)) {
118 return [];
119 }
120
121 $allRoles = self::getAllRoles();
122
123 $permissions = Arr::get($allRoles, $currentRole . '.permissions', []);
124
125 return $permissions;
126 }
127
128 public static function hasPermission($permissions, $userId = false): bool
129 {
130 if ($userId === false) {
131 $userId = get_current_user_id();
132 }
133
134
135 if (!$userId) {
136 return false;
137 }
138
139 $userPermissions = self::getUserPermissions($userId);
140 $permissions = (array)$permissions;
141
142 return array_intersect($userPermissions, $permissions) || in_array('super_admin', $userPermissions);
143 }
144
145 public static function hasAnyPermission(array $permissions, $userId = false): bool
146 {
147 if ($userId === false) {
148 $userId = get_current_user_id();
149 }
150
151
152 if (!$userId) {
153 return false;
154 }
155
156 $userPermissions = self::getUserPermissions($userId);
157
158 return !empty(array_intersect($userPermissions, $permissions)) || in_array('is_super_admin', $userPermissions);
159 }
160
161
162 public static function getShopRole($userId)
163 {
164 $user = get_user_by('ID', $userId);
165
166 if (user_can($user, 'manage_options')) {
167 return 'super_admin';
168 }
169
170 $currentRole = get_user_meta($userId, static::$metaKey, true);
171
172 if (empty($currentRole)) {
173 return false;
174 }
175
176 return $currentRole;
177 }
178
179 public static function getAllPermissions(): array
180 {
181 return [
182 'store/settings' => __('Store Settings', 'fluent-cart'),
183 'store/sensitive' => __('Sensitive Settings', 'fluent-cart'),
184 'products/view' => __('View Products', 'fluent-cart'),
185 'products/create' => __('Create Products', 'fluent-cart'),
186 'products/edit' => __('Edit Products', 'fluent-cart'),
187 'products/delete' => __('Delete Products', 'fluent-cart'),
188 'customers/view' => __('View Customers', 'fluent-cart'),
189 'customers/manage' => __('Manage Customers', 'fluent-cart'),
190 'customers/delete' => __('Delete Customers', 'fluent-cart'),
191 'orders/view' => __('View Orders', 'fluent-cart'),
192 'orders/create' => __('Create Orders', 'fluent-cart'),
193 'orders/manage_statuses' => __('Manage Order Statuses', 'fluent-cart'),
194 'orders/manage' => __('Manage Orders', 'fluent-cart'),
195 'orders/can_refund' => __('Can Refund Orders', 'fluent-cart'),
196 'orders/export' => __('Export Orders', 'fluent-cart'),
197 'orders/delete' => __('Delete Orders', 'fluent-cart'),
198 'subscriptions/view' => __('View Subscriptions', 'fluent-cart'),
199 'subscriptions/manage' => __('Manage Subscriptions', 'fluent-cart'),
200 'subscriptions/delete' => __('Delete Subscriptions', 'fluent-cart'),
201 'licenses/view' => __('View Licenses', 'fluent-cart'),
202 'licenses/manage' => __('Manage Licenses', 'fluent-cart'),
203 'licenses/delete' => __('Delete Licenses', 'fluent-cart'),
204 'coupons/view' => __('View Coupons', 'fluent-cart'),
205 'coupons/manage' => __('Manage Coupons', 'fluent-cart'),
206 'coupons/delete' => __('Delete Coupons', 'fluent-cart'),
207 'reports/view' => __('View Reports', 'fluent-cart'),
208 'reports/export' => __('Export Reports', 'fluent-cart'),
209 'integrations/view' => __('View Integrations', 'fluent-cart'),
210 'integrations/manage' => __('Manage Integrations', 'fluent-cart'),
211 'integrations/delete' => __('Delete Integrations', 'fluent-cart'),
212 'labels/view' => __('View Labels', 'fluent-cart'),
213 'labels/manage' => __('Manage Labels', 'fluent-cart'),
214 'labels/delete' => __('Delete Labels', 'fluent-cart'),
215 'dashboard_stats/view' => __('View Dashboard Stats', 'fluent-cart')
216 ];
217 }
218
219 /**
220 * Get all users that have a FluentCart admin role assigned
221 *
222 * @return array
223 */
224 public static function getUsersWithShopRole(): array
225 {
226 $users = User::query()
227 ->select(['users.*', 'usermeta.meta_value as shop_role'])
228 ->join('usermeta', function ($join) {
229 $join->on('users.ID', '=', 'usermeta.user_id')
230 ->where('usermeta.meta_key', '=', static::$metaKey)
231 ->whereNotNull('usermeta.meta_value')//->where('usermeta.meta_value', '!=', '')
232 ;
233 })
234 ->get();
235
236 // get all roles
237 $allRoles = self::getAllRoles();
238
239 if ($users->isEmpty()) {
240 return [];
241 }
242
243 return $users->map(function ($user) use ($allRoles) {
244 // Check if the shop_role exists in allRoles
245 $role = $allRoles[$user->shop_role] ?? [];
246
247 return [
248 'id' => (int)$user->ID,
249 'email' => $user->user_email,
250 'display_name' => $user->display_name,
251 'username' => $user->user_login,
252 'shop_role' => $user->shop_role,
253 'description' => $role['descriptions'] ?? '',
254 'registered_at' => $user->user_registered,
255 'role_permissions' => self::getUserPermissions($user->ID)
256 ];
257 })->toArray();
258 }
259
260 public static function attachRole($userId, $role)
261 {
262 $wpUser = get_user_by('ID', $userId);
263
264 if (!$wpUser) {
265 return new \WP_Error('user_not_found', __('User not found', 'fluent-cart'));
266 }
267 if (user_can($wpUser, 'manage_options')) {
268 return new \WP_Error('super_admin', __('The user already has all the accesses as part of Administrator Role', 'fluent-cart'));
269 }
270 // Assign the capability directly to the user
271 if (!$wpUser->has_cap(static::ADMIN_CAP)) {
272 $wpUser->add_cap(static::ADMIN_CAP);
273 }
274
275 return update_user_meta($userId, static::$metaKey, $role);
276 }
277
278 public static function detachRole($userId, $role)
279 {
280 $wpUser = get_user_by('ID', $userId);
281
282 if (!$wpUser) {
283 return new \WP_Error('user_not_found', __('User not found', 'fluent-cart'));
284 }
285 if (user_can($wpUser, 'manage_options')) {
286 return new \WP_Error('super_admin', __('The user already has all the accesses as part of Administrator Role', 'fluent-cart'));
287 }
288
289 if (!$wpUser->has_cap(static::ADMIN_CAP)) {
290 $wpUser->remove_cap(static::ADMIN_CAP);
291 }
292
293 return delete_user_meta($userId, static::$metaKey);
294 }
295
296 public static function userCan($permission): bool
297 {
298 $currentUser = Helper::getCurrentUser();
299 return $currentUser && $currentUser->userCan($permission);
300 }
301 }
302
303
304