PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
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 6.2.2, at app/Modules/Acl/Acl.php

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