1 | <?php |
||
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( |
|
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 |
|
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 |
||
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 |
|
121 | } |
||
122 |