1 | <?php |
||
5 | class Evaluator |
||
6 | { |
||
7 | /** |
||
8 | * @var array |
||
9 | */ |
||
10 | private $policies; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $variables; |
||
16 | |||
17 | /** |
||
18 | * @var Statement[] |
||
19 | */ |
||
20 | private $statements; |
||
21 | |||
22 | /** |
||
23 | * @param array $policies |
||
24 | */ |
||
25 | 12 | public function __construct(array $policies, array $variables = []) |
|
36 | |||
37 | /** |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getStatements() |
||
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | public function getVariables() |
||
52 | |||
53 | /** |
||
54 | * @param string $action |
||
55 | * @param string $resource |
||
56 | * @param array $variables |
||
57 | * @return bool |
||
58 | */ |
||
59 | 8 | public function canExecuteActionOnResource($action, $resource, array $variables = []) |
|
60 | { |
||
61 | // TODO([email protected]): Validate action and resource are in valid format |
||
62 | 8 | $statements = $this->matchStatement($action, $resource, $variables); |
|
63 | |||
64 | 8 | foreach ($statements as $statement) { |
|
65 | // If we found a matching statement with an explicit deny, deny right away |
||
66 | 6 | if ($statement->getEffect()->isDeny()) { |
|
67 | 6 | return false; |
|
68 | } |
||
69 | } |
||
70 | |||
71 | 6 | return ! empty($statements); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $document |
||
76 | * @return \tomzx\PolicyEvaluator\Evaluator |
||
77 | */ |
||
78 | public static function fromJsonString($document) |
||
82 | |||
83 | /** |
||
84 | * @param array $statements |
||
85 | */ |
||
86 | 11 | private function parseStatements(array $statements) |
|
97 | |||
98 | /** |
||
99 | * @param array $statement |
||
100 | * @return \tomzx\PolicyEvaluator\Statement |
||
101 | */ |
||
102 | 10 | protected function makeStatement(array $statement) |
|
106 | |||
107 | /** |
||
108 | * @param string $action |
||
109 | * @param string $resource |
||
110 | * @param array $variables |
||
111 | * @return array |
||
112 | */ |
||
113 | 8 | public function matchStatement($action, $resource, array $variables = []) |
|
130 | } |
||
131 |