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