1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tomzx\PolicyEvaluator; |
4
|
|
|
|
5
|
|
|
class Evaluator |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private $policies; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Statement[] |
14
|
|
|
*/ |
15
|
|
|
private $statements; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param array $policies |
19
|
|
|
*/ |
20
|
10 |
|
public function __construct(array $policies) |
21
|
|
|
{ |
22
|
10 |
|
$this->policies = $policies; |
23
|
|
|
|
24
|
10 |
|
if ( ! isset($this->policies['Statement'])) { |
25
|
1 |
|
throw new \InvalidArgumentException('The policy must have at least one statement.'); |
26
|
|
|
} |
27
|
|
|
|
28
|
9 |
|
$this->parseStatements($this->policies['Statement']); |
29
|
7 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getStatements() |
35
|
|
|
{ |
36
|
|
|
return $this->statements; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $action |
41
|
|
|
* @param string $resource |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
6 |
|
public function canExecuteActionOnResource($action, $resource) |
45
|
|
|
{ |
46
|
|
|
// TODO([email protected]): Validate action and resource are in valid format |
47
|
6 |
|
$statements = $this->matchStatement($action, $resource); |
48
|
|
|
|
49
|
6 |
|
foreach ($statements as $statement) { |
50
|
|
|
// If we found a matching statement with an explicit deny, deny right away |
51
|
4 |
|
if ($statement->getEffect()->isDeny()) { |
52
|
2 |
|
return false; |
53
|
|
|
} |
54
|
4 |
|
} |
55
|
|
|
|
56
|
4 |
|
return ! empty($statements); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $document |
61
|
|
|
* @return \tomzx\PolicyEvaluator\Evaluator |
62
|
|
|
*/ |
63
|
|
|
public static function fromJsonString($document) |
64
|
|
|
{ |
65
|
|
|
return new self(json_decode($document, true)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $statements |
70
|
|
|
*/ |
71
|
9 |
|
private function parseStatements(array $statements) |
72
|
|
|
{ |
73
|
9 |
|
if (empty($statements)) { |
74
|
1 |
|
throw new \InvalidArgumentException('The policy must have at least one statement.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
foreach ($statements as $statement) { |
78
|
8 |
|
$statement = new Statement($statement); |
79
|
7 |
|
$this->statements[] = $statement; |
80
|
7 |
|
} |
81
|
7 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $action |
85
|
|
|
* @param string $resource |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
6 |
|
private function matchStatement($action, $resource) |
89
|
|
|
{ |
90
|
6 |
|
$statements = []; |
91
|
6 |
|
foreach ($this->statements as $statement) { |
92
|
6 |
|
if ( ! $statement->matchesAction($action)) { |
93
|
1 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
if ( ! $statement->matchesResource($resource)) { |
97
|
1 |
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
$statements[] = $statement; |
101
|
6 |
|
} |
102
|
6 |
|
return $statements; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|