PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Acl / Acl.php

Acl.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 5.2.9, at app/Modules/Acl/Acl.php

358 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Acl;
4
5 use FluentForm\App\Services\Manager\FormManagerService;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7
8 class Acl
9 {
10 public static $capability = '';
11
12 public static $role = '';
13
14 public static function getPermissionSet()
15 {
16 $data = [
17 'fluentform_dashboard_access',
18 'fluentform_forms_manager',
19 'fluentform_entries_viewer',
20 'fluentform_manage_entries',
21 'fluentform_view_payments',
22 'fluentform_manage_payments',
23 'fluentform_settings_manager',
24 'fluentform_full_access',
25 ];
26
27 $data = apply_filters_deprecated(
28 'fluentform_permission_set',
29 [
30 $data
31 ],
32 FLUENTFORM_FRAMEWORK_UPGRADE,
33 'fluentform/permission_set',
34 'Use fluentform/permission_set instead of fluentform_permission_set.'
35 );
36
37 return apply_filters('fluentform/permission_set', $data);
38 }
39
40 /**
41 * Fluentform access controll permissions assignment.
42 */
43 public static function setPermissions()
44 {
45 // Fire an event letting others know that fluentform
46 // is going to assign permission set to a role.
47 do_action_deprecated(
48 'before_fluentform_permission_set_assignment',
49 [
50
51 ],
52 FLUENTFORM_FRAMEWORK_UPGRADE,
53 'fluentform/before_permission_set_assignment',
54 'Use fluentform/before_permission_set_assignment instead of before_fluentform_permission_set_assignment.'
55 );
56
57 do_action('fluentform/before_permission_set_assignment');
58
59 // The permissions that fluentform supports altogether.
60 $permissions = self::getPermissionSet();
61
62 // The role that fluentform will use
63 // to attach the permission set.
64 $role = get_role('administrator');
65
66 if ($role) {
67 // Looping through permission set to add to the role.
68 foreach ($permissions as $permission) {
69 $role->add_cap($permission);
70 }
71 }
72
73 // Fire an event letting others know that fluentform is
74 // done with the permission assignment to the role.
75 do_action_deprecated(
76 'after_fluentform_permission_set_assignment',
77 [
78
79 ],
80 FLUENTFORM_FRAMEWORK_UPGRADE,
81 'fluentform/after_permission_set_assignment',
82 'Use fluentform/after_permission_set_assignment instead of after_fluentform_permission_set_assignment.'
83 );
84 do_action('fluentform/after_permission_set_assignment');
85 }
86
87 /**
88 * Verify if current user has a fluentform permission.
89 *
90 * @param $permission
91 * @param null $formId
92 * @param string $message
93 * @param bool $json
94 *
95 * @throws \Exception
96 */
97 public static function verify(
98 $permission,
99 $formId = null,
100 $message = 'You do not have permission to perform this action.',
101 $json = true
102 ) {
103 static::verifyNonce();
104
105 $allowed = static::hasPermission($permission, $formId);
106
107 if (!$allowed) {
108 if ($json) {
109 wp_send_json_error([
110 'message' => $message,
111 ], 422);
112 } else {
113 throw new \Exception($message);
114 }
115 }
116 }
117
118 public static function hasPermission($permissions, $formId = false)
119 {
120 if ($formId && !FormManagerService::hasFormPermission($formId)) {
121 return false;
122 }
123 $userCapability = static::getCurrentUserCapability();
124
125 if ($userCapability) {
126 return $userCapability;
127 } else {
128 if (current_user_can('fluentform_full_access')) {
129 return true;
130 }
131
132 $permissions = (array) $permissions;
133
134 foreach ($permissions as $permission) {
135 $allowed = current_user_can($permission);
136
137 if ($allowed) {
138 $allowed = apply_filters_deprecated(
139 'fluentform_verify_user_permission_' . $permission,
140 [
141 $allowed,
142 $formId
143 ],
144 FLUENTFORM_FRAMEWORK_UPGRADE,
145 'fluentform/verify_user_permission_' . $permission,
146 'Use fluentform/verify_user_permission_' . $permission . ' instead of fluentform_verify_user_permission_' . $permission
147 );
148
149 return apply_filters('fluentform/verify_user_permission_' . $permission, $allowed, $formId);
150 }
151 }
152
153 return false;
154 }
155 }
156
157 public static function hasAnyFormPermission($form_id = false)
158 {
159 $allPermissions = static::getPermissionSet();
160
161 foreach ($allPermissions as $permission) {
162 if (static::hasPermission($permission, $form_id)) {
163 return true;
164 }
165 }
166
167 return false;
168 }
169
170 public static function getCurrentUserCapability()
171 {
172 if (static::$capability) {
173 return static::$capability;
174 }
175
176 if (is_user_logged_in()) {
177 static::$capability = static::findUserCapability(wp_get_current_user());
178 } else {
179 static::$capability = false;
180 }
181
182 return apply_filters('fluentform/current_user_capability', static::$capability);
183 }
184
185 public static function findUserCapability($user)
186 {
187 if (!$user) {
188 return false;
189 }
190
191 if (static::isSuperMan($user)) {
192 return 'manage_options';
193 }
194
195 $capabilities = get_option('_fluentform_form_permission');
196
197 if (is_string($capabilities)) {
198 $capabilities = (array) $capabilities;
199 }
200
201 if (!$capabilities) {
202 return false;
203 }
204
205 foreach ($capabilities as $capability) {
206 if ($user->has_cap($capability)) {
207 return $capability;
208 }
209 }
210
211 return false;
212 }
213
214 public static function getCurrentUserRole()
215 {
216 $user = wp_get_current_user();
217
218 return static::$role = $user->roles[0];
219 }
220
221 public static function verifyNonce($key = 'fluent_forms_admin_nonce')
222 {
223 if (!wp_doing_ajax()) {
224 return;
225 }
226
227 $nonce = wpFluentForm('request')->get($key);
228
229 if (!wp_verify_nonce($nonce, $key)) {
230 $message = __('Nonce verification failed, please try again.', 'fluentform');
231 $message = apply_filters('fluentform/nonce_error', $message);
232
233 wp_send_json_error([
234 'message' => $message,
235 ], 422);
236 }
237 }
238
239 public static function getReadablePermissions()
240 {
241 return [
242 'fluentform_dashboard_access' => [
243 'title' => __('View Forms', 'fluentform'),
244 'depends' => [],
245 ],
246 'fluentform_forms_manager' => [
247 'title' => __('Manage Forms', 'fluentform'),
248 'depends' => [
249 'fluentform_dashboard_access',
250 ],
251 ],
252 'fluentform_entries_viewer' => [
253 'title' => __('View Entries', 'fluentform'),
254 'depends' => [
255 'fluentform_dashboard_access',
256 ],
257 ],
258 'fluentform_manage_entries' => [
259 'title' => __('Manage Entries', 'fluentform'),
260 'depends' => [
261 'fluentform_entries_viewer',
262 ],
263 ],
264 'fluentform_view_payments' => [
265 'title' => __('View Payments', 'fluentform'),
266 'depends' => [
267 'fluentform_dashboard_access',
268 'fluentform_entries_viewer',
269 ],
270 ],
271 'fluentform_manage_payments' => [
272 'title' => __('Manage Payments', 'fluentform'),
273 'depends' => [
274 'fluentform_view_payments',
275 ],
276 ],
277 'fluentform_settings_manager' => [
278 'title' => __('Manage Settings', 'fluentform'),
279 'depends' => [],
280 ],
281 'fluentform_full_access' => [
282 'title' => __('Full Access', 'fluentform'),
283 'depends' => [],
284 ],
285 ];
286 }
287
288 public static function getUserPermissions($user = false)
289 {
290 if (is_numeric($user)) {
291 $user = get_user_by('ID', $user);
292 }
293
294 if (!$user) {
295 return [];
296 }
297
298 $permissionSet = static::getPermissionSet();
299 $isSuperMan = static::isSuperMan($user);
300 $capability = static::findUserCapability($user);
301
302 if ($isSuperMan || $capability) {
303 if ($isSuperMan) {
304 // $permissionSet[] = 'administrator';
305 }
306
307 return $permissionSet;
308 }
309
310 $userPermissions = array_values(array_intersect(array_keys($user->allcaps), $permissionSet));
311
312 return apply_filters('fluentform/current_user_permissions', $userPermissions);
313 }
314
315 public static function isSuperMan($user = false)
316 {
317 if ($user) {
318 return $user->has_cap('manage_options');
319 } else {
320 return current_user_can('manage_options');
321 }
322 }
323
324 public static function getCurrentUserPermissions()
325 {
326 return static::getUserPermissions(wp_get_current_user());
327 }
328
329 public static function attachPermissions($user, $permissions)
330 {
331 if (is_numeric($user)) {
332 $user = get_user_by('ID', $user);
333 }
334
335 if (!$user) {
336 return false;
337 }
338
339 if (user_can($user, 'manage_options')) {
340 return $user;
341 }
342
343 $allPermissions = static::getPermissionSet();
344
345 foreach ($allPermissions as $permission) {
346 $user->remove_cap($permission);
347 }
348
349 $permissions = array_intersect($allPermissions, $permissions);
350
351 foreach ($permissions as $permission) {
352 $user->add_cap($permission);
353 }
354
355 return $user;
356 }
357 }
358