Conditions | 16 |
Paths | 35 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
61 | public function getPermissionsCriterion($module = 'content', $function = 'read', ?array $targets = null) |
||
62 | { |
||
63 | $permissionSets = $this->permissionResolver->hasAccess($module, $function); |
||
64 | if (is_bool($permissionSets)) { |
||
65 | return $permissionSets; |
||
66 | } |
||
67 | |||
68 | if (empty($permissionSets)) { |
||
69 | throw new RuntimeException("Got an empty array of limitations from hasAccess( '{$module}', '{$function}' )"); |
||
70 | } |
||
71 | |||
72 | /* |
||
73 | * RoleAssignment is a OR condition, so is policy, while limitations is a AND condition |
||
74 | * |
||
75 | * If RoleAssignment has limitation then policy OR conditions are wrapped in a AND condition with the |
||
76 | * role limitation, otherwise it will be merged into RoleAssignment's OR condition. |
||
77 | */ |
||
78 | $currentUserRef = $this->permissionResolver->getCurrentUserReference(); |
||
79 | $roleAssignmentOrCriteria = []; |
||
80 | foreach ($permissionSets as $permissionSet) { |
||
81 | // $permissionSet is a RoleAssignment, but in the form of role limitation & role policies hash |
||
82 | $policyOrCriteria = []; |
||
83 | /** |
||
84 | * @var \eZ\Publish\API\Repository\Values\User\Policy |
||
85 | */ |
||
86 | foreach ($permissionSet['policies'] as $policy) { |
||
87 | $limitations = $policy->getLimitations(); |
||
88 | if ($limitations === '*' || empty($limitations)) { |
||
89 | // Given policy gives full access, optimize away all role policies (but not role limitation if any) |
||
90 | // This should be optimized on create/update of Roles, however we keep this here for bc with older data |
||
91 | $policyOrCriteria = []; |
||
92 | break; |
||
93 | } |
||
94 | |||
95 | $limitationsAndCriteria = []; |
||
96 | foreach ($limitations as $limitation) { |
||
97 | $limitationsAndCriteria[] = $this->getCriterionForLimitation($limitation, $currentUserRef, $targets); |
||
98 | } |
||
99 | |||
100 | $policyOrCriteria[] = isset($limitationsAndCriteria[1]) ? |
||
101 | new LogicalAnd($limitationsAndCriteria) : |
||
102 | $limitationsAndCriteria[0]; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Apply role limitations if there is one. |
||
107 | * |
||
108 | * @var \eZ\Publish\API\Repository\Values\User\Limitation[] |
||
109 | */ |
||
110 | if ($permissionSet['limitation'] instanceof Limitation) { |
||
111 | // We need to match both the limitation AND *one* of the policies, aka; roleLimit AND policies(OR) |
||
112 | if (!empty($policyOrCriteria)) { |
||
113 | $criterion = $this->getCriterionForLimitation($permissionSet['limitation'], $currentUserRef, $targets); |
||
114 | $roleAssignmentOrCriteria[] = new LogicalAnd( |
||
115 | [ |
||
116 | $criterion, |
||
117 | isset($policyOrCriteria[1]) ? new LogicalOr($policyOrCriteria) : $policyOrCriteria[0], |
||
118 | ] |
||
119 | ); |
||
120 | } else { |
||
121 | $roleAssignmentOrCriteria[] = $this->getCriterionForLimitation( |
||
122 | $permissionSet['limitation'], $currentUserRef, $targets |
||
123 | ); |
||
124 | } |
||
125 | } elseif (!empty($policyOrCriteria)) { |
||
126 | // Otherwise merge $policyOrCriteria into $roleAssignmentOrCriteria |
||
127 | // There is no role limitation, so any of the policies can globally match in the returned OR criteria |
||
128 | $roleAssignmentOrCriteria = empty($roleAssignmentOrCriteria) ? |
||
129 | $policyOrCriteria : |
||
130 | array_merge($roleAssignmentOrCriteria, $policyOrCriteria); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | if (empty($roleAssignmentOrCriteria)) { |
||
135 | return false; |
||
136 | } |
||
137 | |||
138 | return isset($roleAssignmentOrCriteria[1]) ? |
||
139 | new LogicalOr($roleAssignmentOrCriteria) : |
||
140 | $roleAssignmentOrCriteria[0]; |
||
141 | } |
||
142 | |||
160 |