|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
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-2018 LibreWorks contributors |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Acl; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* An ACL rule. |
|
25
|
|
|
* |
|
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
27
|
|
|
* @license Apache-2.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class Rule |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Caridea\Acl\Subject The subject |
|
33
|
|
|
*/ |
|
34
|
|
|
private $subject; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array|null An array of verbs granted access, or null for all verbs |
|
37
|
|
|
*/ |
|
38
|
|
|
private $verbs; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool Whether the Subject is allowed or forbidden |
|
41
|
|
|
*/ |
|
42
|
|
|
private $allowed; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Creates a new Rule. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Caridea\Acl\Subject $subject The subject |
|
48
|
|
|
* @param bool $allowed Whether this is an allowing rule |
|
49
|
|
|
* @param array<string>|null $verbs Array of verbs, or `null` for all verbs |
|
50
|
|
|
*/ |
|
51
|
2 |
|
protected function __construct(Subject $subject, bool $allowed, array $verbs = null) |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->subject = $subject; |
|
54
|
2 |
|
$this->allowed = (bool) $allowed; |
|
55
|
2 |
|
$this->verbs = empty($verbs) ? null : $verbs; |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Gets whether this is an allowing rule. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool Whether this is an allowing rule |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function isAllowed(): bool |
|
64
|
|
|
{ |
|
65
|
2 |
|
return $this->allowed; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets whether this Rule matches the `Subject` and verb provided. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \Caridea\Acl\Subject $subject The subject to check |
|
72
|
|
|
* @param string $verb The verb to check |
|
73
|
|
|
* @return bool Whether this Rule matches the arguments provided |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function match(Subject $subject, string $verb): bool |
|
76
|
|
|
{ |
|
77
|
2 |
|
return $this->subject->getType() == $subject->getType() && |
|
78
|
2 |
|
$this->subject->getId() === $subject->getId() && |
|
79
|
2 |
|
($this->verbs === null || in_array($verb, $this->verbs, true)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates an allowing Rule. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \Caridea\Acl\Subject $subject The subject to allow access |
|
86
|
|
|
* @param array<string>|null $verbs Optional list of allowed verbs. |
|
87
|
|
|
* Empty or `null` means *all*. |
|
88
|
|
|
* @return Rule The allowing Rule |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public static function allow(Subject $subject, array $verbs = null): Rule |
|
91
|
|
|
{ |
|
92
|
1 |
|
return new self($subject, true, $verbs); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Creates an denying Rule. |
|
97
|
|
|
* |
|
98
|
|
|
* @param \Caridea\Acl\Subject $subject The subject to allow access |
|
99
|
|
|
* @param array<string>|null $verbs Optional list of denied verbs. |
|
100
|
|
|
* Empty or `null` means *all*. |
|
101
|
|
|
* @return Rule The denying Rule |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public static function deny(Subject $subject, array $verbs = null): Rule |
|
104
|
|
|
{ |
|
105
|
1 |
|
return new self($subject, false, $verbs); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|