-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
Model.php
256 lines (224 loc) · 5.8 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
declare(strict_types=1);
namespace Casbin\Model;
use Casbin\Config\Config;
use Casbin\Config\ConfigContract;
use Casbin\Exceptions\CasbinException;
use Casbin\Log\Log;
use Casbin\Util\Util;
/**
* Class Model.
* Represents the whole access control model.
*
* @package Casbin\Model
* @author techlee@qq.com
*/
class Model extends Policy
{
/**
* @var array<string, string>
*/
protected $sectionNameMap = [
'r' => 'request_definition',
'p' => 'policy_definition',
'g' => 'role_definition',
'e' => 'policy_effect',
'm' => 'matchers',
];
public function __construct()
{
}
public function __clone()
{
$this->sectionNameMap = $this->sectionNameMap;
$newAstMap = [];
foreach ($this->items as $ptype => $ast) {
foreach ($ast as $i => $v) {
$newAstMap[$ptype][$i] = clone $v;
}
}
$this->items = $newAstMap;
}
/**
* @param ConfigContract $cfg
* @param string $sec
* @param string $key
*
* @return bool
* @throws CasbinException
*/
private function loadAssertion(ConfigContract $cfg, string $sec, string $key): bool
{
$value = $cfg->getString($this->sectionNameMap[$sec] . '::' . $key);
return $this->addDef($sec, $key, $value);
}
/**
* Adds an assertion to the model.
*
* @param string $sec
* @param string $key
* @param string $value
*
* @return bool
* @throws CasbinException
*/
public function addDef(string $sec, string $key, string $value): bool
{
if ('' == $value) {
return false;
}
$ast = new Assertion();
$ast->key = $key;
$ast->value = $value;
$ast->initPriorityIndex();
if ('r' == $sec || 'p' == $sec) {
$ast->tokens = explode(',', $ast->value);
foreach ($ast->tokens as $i => $token) {
$ast->tokens[$i] = $key . '_' . trim($token);
}
} else {
$ast->value = Util::removeComments(Util::escapeAssertion($ast->value));
}
$this->items[$sec][$key] = $ast;
return true;
}
/**
* @param int $i
*
* @return string
*/
private function getKeySuffix(int $i): string
{
if (1 == $i) {
return '';
}
return (string)$i;
}
/**
* @param ConfigContract $cfg
* @param string $sec
* @throws CasbinException
*/
private function loadSection(ConfigContract $cfg, string $sec): void
{
$i = 1;
for (; ;) {
if (!$this->loadAssertion($cfg, $sec, $sec . $this->getKeySuffix($i))) {
break;
} else {
++$i;
}
}
}
/**
* Creates an empty model.
*
* @return Model
*/
public static function newModel(): self
{
return new self();
}
/**
* Creates a model from a .CONF file.
*
* @param string $path
*
* @return Model
* @throws CasbinException
*/
public static function newModelFromFile(string $path): self
{
$m = self::newModel();
$m->loadModel($path);
return $m;
}
/**
* Creates a model from a string which contains model text.
*
* @param string $text
*
* @return Model
* @throws CasbinException
*/
public static function newModelFromString(string $text): self
{
$m = self::newModel();
$m->loadModelFromText($text);
return $m;
}
/**
* Loads the model from model CONF file.
*
* @param string $path
* @throws CasbinException
*/
public function loadModel(string $path): void
{
$cfg = Config::newConfig($path);
$this->loadSection($cfg, 'r');
$this->loadSection($cfg, 'p');
$this->loadSection($cfg, 'e');
$this->loadSection($cfg, 'm');
$this->loadSection($cfg, 'g');
}
/**
* Loads the model from the text.
*
* @param string $text
* @throws CasbinException
*/
public function loadModelFromText(string $text): void
{
$cfg = Config::newConfigFromText($text);
$this->loadSection($cfg, 'r');
$this->loadSection($cfg, 'p');
$this->loadSection($cfg, 'e');
$this->loadSection($cfg, 'm');
$this->loadSection($cfg, 'g');
}
/**
* Prints the model to the log.
*/
public function printModel(): void
{
Log::logPrint('Model:');
foreach ($this->items as $k => $v) {
foreach ($v as $i => $j) {
Log::logPrintf('%s.%s: %s', $k, $i, $j->value);
}
}
}
/**
* Loads an initial function map.
*
* @return FunctionMap
*/
public static function loadFunctionMap(): FunctionMap
{
return FunctionMap::loadFunctionMap();
}
public function sortPoliciesByPriority(): void
{
foreach ($this->items['p'] as $ptype => $assertion) {
$index = array_search(sprintf("%s_priority", $ptype), $assertion->tokens);
if ($index !== false) {
$assertion->priorityIndex = intval($index);
} else {
continue;
}
$policies = &$assertion->policy;
usort($policies, function ($i, $j) use ($assertion): int {
$p1 = $i[$assertion->priorityIndex];
$p2 = $j[$assertion->priorityIndex];
if ($p1 == $p2) {
return 0;
}
return ($p1 < $p2) ? -1 : 1;
});
foreach ($assertion->policy as $i => $policy) {
$assertion->policyMap[implode(',', $policy)] = $i;
}
}
}
}