Completed
Push — master ( 24fba4...970de3 )
by
unknown
04:30
created

Gatekeeper::assertAll()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 3
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Acl;
22
23
/**
24
 * Immutable access control helper.
25
 *
26
 * This class determines the user's subjects upon instantiation and stores them
27
 * for its duration.
28
 */
29
class Gatekeeper
30
{
31
    /**
32
     * @var \Caridea\Acl\Service
33
     */
34
    private $aclService;
35
    /**
36
     * @var \Caridea\Auth\Principal
37
     */
38
    private $principal;
39
    /**
40
     * @var array<\Caridea\Acl\Subject>
41
     */
42
    private $subjects;
43
44
    /**
45
     * Creates a new Gatekeeper.
46
     *
47
     * @param \Caridea\Acl\Service $aclService The ACL service
48
     * @param \Caridea\Auth\Principal $principal The authenticated principal
49
     * @param array<\Minotaur\Acl\SubjectResolver> $subjectResolvers Any additional subject resolvers
50
     */
51 2
    public function __construct(
52
        \Caridea\Acl\Service $aclService,
53
        \Caridea\Auth\Principal $principal,
54
        array $subjectResolvers
55
    ) {
56 2
        $this->aclService = $aclService;
57 2
        $this->principal = $principal;
58 2
        $subjects = [\Caridea\Acl\Subject::principal((string)$principal->getUsername())];
59 2
        foreach ($subjectResolvers as $resolver) {
60 2
          foreach($resolver->getSubjects($principal) as $subject){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
61 2
            $subjects[] = $subject;
62
          }
63
        }
64 2
        $this->subjects = $subjects;
0 ignored issues
show
Documentation Bug introduced by
It seems like $subjects of type array<integer,object<Car...<Caridea\Auth\Subject>> is incompatible with the declared type array<integer,object<Caridea\Acl\Subject>> of property $subjects.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65 2
    }
66
67
    /**
68
     * Determines if the currently authenticated user can access the resource.
69
     *
70
     * @param $verb - The verb (e.g. 'read', 'write')
71
     * @param $type - The type of object
72
     * @param $id - The object identifier
73
     * @throws \Caridea\Acl\Exception\Forbidden If the user has no access
74
     */
75 1
    public function assert(string $verb, string $type, $id): void
76
    {
77 1
        $this->aclService->assert(
78 1
            $this->subjects,
79 1
            $verb,
80 1
            new \Caridea\Acl\Target($type, $id)
81
        );
82 1
    }
83
84
    /**
85
     * Determines if the currently authenticated user can access the resources.
86
     *
87
     * @param string $verb The verb (e.g. 'read', 'write')
88
     * @param string $type The type of object
89
     * @param iterable<mixed> $ids The object identifiers
0 ignored issues
show
Documentation introduced by
The doc-type iterable<mixed> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
90
     * @throws \Caridea\Acl\Exception\Forbidden If the user has no access
91
     */
92
    public function assertAll(string $verb, string $type, iterable $ids): void
93
    {
94
        $targets = array_map(function ($a) use ($type) {
95
            return new \Caridea\Acl\Target($type, $a);
96
        }, is_array($ids) ? $ids : iterator_to_array($ids));
97
        $acls = $this->aclService->getAll($targets, $this->subjects);
98
        foreach ($acls as $acl) {
99
            if (!$acl->can($this->subjects, $verb)) {
100
                throw new \Caridea\Acl\Exception\Forbidden("Access denied to $verb " . (string)$acl->getTarget());
101
            }
102
        }
103
    }
104
105
    /**
106
     * Determines if the currently authenticated user can access the resource.
107
     *
108
     * @param string $verb The verb (e.g. 'read', 'write')
109
     * @param string $type The type of object
110
     * @param mixed $id The object identifier
111
     * @return bool Whether the user has access
112
     */
113 1
    public function can(string $verb, string $type, $id): bool
114
    {
115 1
        return $this->aclService->can(
116 1
            $this->subjects,
117 1
            $verb,
118 1
            new \Caridea\Acl\Target($type, $id)
119
        );
120
    }
121
}
122