1 | <?php |
||
29 | class Service |
||
30 | { |
||
31 | /** |
||
32 | * @var Strategy The loading strategy |
||
33 | */ |
||
34 | private $strategy; |
||
35 | |||
36 | /** |
||
37 | * Creates a new Service. |
||
38 | * |
||
39 | * @param \Caridea\Acl\Strategy $strategy The loading strategy |
||
40 | */ |
||
41 | 9 | public function __construct(Strategy $strategy) |
|
42 | { |
||
43 | 9 | $this->strategy = $strategy; |
|
44 | 9 | } |
|
45 | |||
46 | /** |
||
47 | * Asserts that one of the provided subjects can verb the Target. |
||
48 | * |
||
49 | * @param Subject[] $subjects An array of `Subject`s |
||
50 | * @param string $verb The verb (e.g. `read`, `create`, `delete`) |
||
51 | * @param \Caridea\Acl\Target $target The target |
||
52 | * @throws Exception\Forbidden if the subject cannot *verb* the Target |
||
53 | */ |
||
54 | 3 | public function assert(array $subjects, string $verb, Target $target): void |
|
55 | { |
||
56 | try { |
||
57 | 3 | if ($this->get($target, $subjects)->can($subjects, $verb)) { |
|
58 | 2 | return; |
|
59 | } |
||
60 | 1 | } catch (\Exception $ex) { |
|
61 | 1 | throw new Exception\Forbidden("Access denied to $verb the target", 0, $ex); |
|
62 | } |
||
63 | 1 | throw new Exception\Forbidden("Access denied to $verb the target"); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Whether any of the provided subjects has permission to verb the Target. |
||
68 | * |
||
69 | * @param Subject[] $subjects An array of `Subject`s |
||
70 | * @param string $verb The verb (e.g. `read`, `create`, `delete`) |
||
71 | * @param \Caridea\Acl\Target $target The target |
||
72 | * @return bool Whether one of the subjects can *verb* the provided Target |
||
73 | */ |
||
74 | 2 | public function can(array $subjects, string $verb, Target $target): bool |
|
75 | { |
||
76 | try { |
||
77 | 2 | return $this->get($target, $subjects)->can($subjects, $verb); |
|
78 | 1 | } catch (\Exception $ex) { |
|
79 | // just return false below |
||
80 | } |
||
81 | 1 | return false; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Gets an access control list for a Target. |
||
86 | * |
||
87 | * @param Target $target The Target whose ACL will be loaded |
||
88 | * @param Subject[] $subjects An array of `Subject`s |
||
89 | * @return Acl The Acl found |
||
90 | * @throws Exception\Unloadable If the target provided is invalid |
||
91 | * @throws \InvalidArgumentException If the `subjects` argument contains invalid values |
||
92 | */ |
||
93 | 6 | public function get(Target $target, array $subjects): Acl |
|
94 | { |
||
95 | 6 | return $this->strategy->load($target, $subjects, $this); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Gets access control lists for several Targets. |
||
100 | * |
||
101 | * @since 2.1.0 |
||
102 | * @param \Caridea\Acl\Target[] $targets The `Target` whose ACL will be loaded |
||
103 | * @param \Caridea\Acl\Subject[] $subjects An array of `Subject`s |
||
104 | * @return array<string,\Caridea\Acl\Acl> The loaded ACLs |
||
105 | * @throws \Caridea\Acl\Exception\Unloadable If the target provided is invalid |
||
106 | * @throws \InvalidArgumentException If the `targets` or `subjects` argument contains invalid values |
||
107 | */ |
||
108 | 3 | public function getAll(array $targets, array $subjects): array |
|
127 | } |
||
128 |