Completed
Push — cached_permision_criterion ( 018fb5 )
by André
13:02
created

getPermissionsCriterion()   C

Complexity

Conditions 17
Paths 35

Size

Total Lines 80
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
c 0
b 0
f 0
cc 17
eloc 40
nc 35
nop 2
rs 5.033

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Repository\Permission;
8
9
use eZ\Publish\API\Repository\PermissionCriterionResolver as APIPermissionCriterionResolver;
10
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd;
11
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOr;
12
use eZ\Publish\API\Repository\Values\User\Limitation;
13
use eZ\Publish\API\Repository\PermissionResolver as PermissionResolverInterface;
14
use eZ\Publish\Core\Repository\Helper\LimitationService;
15
use RuntimeException;
16
17
/**
18
 * Implementation of Permissions Criterion Resolver.
19
 *
20
 * @since 6.12
21
 */
22
class PermissionsCriterionResolver implements APIPermissionCriterionResolver
23
{
24
    /**
25
     * @var \eZ\Publish\API\Repository\PermissionResolver
26
     */
27
    private $permissionResolver;
28
29
    /**
30
     * @var \eZ\Publish\Core\Repository\Helper\LimitationService
31
     */
32
    private $limitationService;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
38
     * @param \eZ\Publish\Core\Repository\Helper\LimitationService $limitationService
39
     */
40
    public function __construct(
41
        PermissionResolverInterface $permissionResolver,
42
        LimitationService $limitationService
43
    ) {
44
        $this->permissionResolver = $permissionResolver;
45
        $this->limitationService = $limitationService;
46
    }
47
48
    /**
49
     * Get content-read Permission criteria if needed and return false if no access at all.
50
     *
51
     * @uses \eZ\Publish\API\Repository\PermissionResolver::hasAccess()
52
     *
53
     * @throws \RuntimeException If empty array of limitations are provided from hasAccess()
54
     *
55
     * @param string $module
56
     * @param string $function
57
     *
58
     * @return bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion
59
     */
60
    public function getPermissionsCriterion($module = 'content', $function = 'read')
61
    {
62
        $permissionSets = $this->permissionResolver->hasAccess($module, $function);
63
        if ($permissionSets === false || $permissionSets === true) {
64
            return $permissionSets;
65
        }
66
67
        if (empty($permissionSets)) {
68
            throw new RuntimeException("Got an empty array of limitations from hasAccess( '{$module}', '{$function}' )");
69
        }
70
71
        /*
72
         * RoleAssignment is a OR condition, so is policy, while limitations is a AND condition
73
         *
74
         * If RoleAssignment has limitation then policy OR conditions are wrapped in a AND condition with the
75
         * role limitation, otherwise it will be merged into RoleAssignment's OR condition.
76
         */
77
        $currentUserRef = $this->permissionResolver->getCurrentUserReference();
78
        $roleAssignmentOrCriteria = array();
79
        foreach ($permissionSets as $permissionSet) {
0 ignored issues
show
Bug introduced by
The expression $permissionSets of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
            // $permissionSet is a RoleAssignment, but in the form of role limitation & role policies hash
81
            $policyOrCriteria = array();
82
            /**
83
             * @var \eZ\Publish\API\Repository\Values\User\Policy
84
             */
85
            foreach ($permissionSet['policies'] as $policy) {
86
                $limitations = $policy->getLimitations();
87
                if ($limitations === '*' || empty($limitations)) {
88
                    // Given policy gives full access, optimize away all role policies (but not role limitation if any)
89
                    // This should be optimized on create/update of Roles, however we keep this here for bc with older data
90
                    $policyOrCriteria = [];
91
                    break;
92
                }
93
94
                $limitationsAndCriteria = array();
95
                foreach ($limitations as $limitation) {
96
                    $type = $this->limitationService->getLimitationType($limitation->getIdentifier());
97
                    $limitationsAndCriteria[] = $type->getCriterion($limitation, $currentUserRef);
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
                $type = $this->limitationService->getLimitationType($permissionSet['limitation']->getIdentifier());
113
                if (!empty($policyOrCriteria)) {
114
                    $roleAssignmentOrCriteria[] = new LogicalAnd(
115
                        array(
116
                            $type->getCriterion($permissionSet['limitation'], $currentUserRef),
117
                            isset($policyOrCriteria[1]) ? new LogicalOr($policyOrCriteria) : $policyOrCriteria[0],
118
                        )
119
                    );
120
                } else {
121
                    $roleAssignmentOrCriteria[] = $type->getCriterion($permissionSet['limitation'], $currentUserRef);
122
                }
123
            } elseif (!empty($policyOrCriteria)) {
124
                // Otherwise merge $policyOrCriteria into $roleAssignmentOrCriteria
125
                // There is no role limitation, so any of the policies can globally match in the returned OR criteria
126
                $roleAssignmentOrCriteria = empty($roleAssignmentOrCriteria) ?
127
                    $policyOrCriteria :
128
                    array_merge($roleAssignmentOrCriteria, $policyOrCriteria);
129
            }
130
        }
131
132
        if (empty($roleAssignmentOrCriteria)) {
133
            return false;
134
        }
135
136
        return isset($roleAssignmentOrCriteria[1]) ?
137
            new LogicalOr($roleAssignmentOrCriteria) :
138
            $roleAssignmentOrCriteria[0];
139
    }
140
}
141