PluginProbe
The Innovative Form Builder – IvyForms / 1.4.1
The Innovative Form Builder – IvyForms v1.4.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Permissions / FormScopedGrantChecker.php

FormScopedGrantChecker.php in The Innovative Form Builder – IvyForms 1.4.1, at backend/src/Services/Permissions/FormScopedGrantChecker.php

98 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 */
6
7 namespace IvyForms\Services\Permissions;
8
9 // phpcs:disable PSR1.Files.SideEffects
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 /**
15 * Checks whether a user has permission keys from any form-scoped access rule.
16 */
17 final class FormScopedGrantChecker
18 {
19 /**
20 * @param list<array<string,mixed>> $rules
21 * @param \WP_User $user
22 * @param list<string> $permissionKeys
23 */
24 public static function userHasAnyGrant(array $rules, \WP_User $user, array $permissionKeys): bool
25 {
26 foreach ($rules as $rule) {
27 if (!is_array($rule)) {
28 continue;
29 }
30 if (self::ruleGrantsAny($rule, $user, $permissionKeys)) {
31 return true;
32 }
33 }
34
35 return false;
36 }
37
38 /**
39 * @param array<string,mixed> $rule
40 * @param \WP_User $user
41 * @param list<string> $permissionKeys
42 */
43 private static function ruleGrantsAny(array $rule, \WP_User $user, array $permissionKeys): bool
44 {
45 if (!self::isFormScopedRule($rule) || !self::ruleMatchesPrincipal($rule, $user)) {
46 return false;
47 }
48
49 return self::ruleGrantsAnyKey($rule, $permissionKeys);
50 }
51
52 /**
53 * @param array<string,mixed> $rule
54 */
55 private static function isFormScopedRule(array $rule): bool
56 {
57 $formIds = $rule['form_ids'] ?? null;
58
59 return is_array($formIds) && $formIds !== [];
60 }
61
62 /**
63 * @param array<string,mixed> $rule
64 * @param \WP_User $user
65 */
66 private static function ruleMatchesPrincipal(array $rule, \WP_User $user): bool
67 {
68 $ruleType = (string) ($rule['type'] ?? '');
69 if ($ruleType === 'user') {
70 return AccessRuleMatcher::matchesUser($rule, $user);
71 }
72 if ($ruleType === 'role') {
73 return AccessRuleMatcher::matchesRole($rule, $user);
74 }
75
76 return false;
77 }
78
79 /**
80 * @param array<string,mixed> $rule
81 * @param list<string> $permissionKeys
82 */
83 private static function ruleGrantsAnyKey(array $rule, array $permissionKeys): bool
84 {
85 $permissions = isset($rule['permissions']) && is_array($rule['permissions'])
86 ? PermissionCatalog::sanitize($rule['permissions'])
87 : [];
88
89 foreach ($permissionKeys as $permissionKey) {
90 if (in_array($permissionKey, $permissions, true)) {
91 return true;
92 }
93 }
94
95 return false;
96 }
97 }
98