1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Casbin\Persist\Adapters; |
6
|
|
|
|
7
|
|
|
use Casbin\Exceptions\CasbinException; |
8
|
|
|
use Casbin\Exceptions\InvalidFilePathException; |
9
|
|
|
use Casbin\Exceptions\NotImplementedException; |
10
|
|
|
use Casbin\Model\Model; |
11
|
|
|
use Casbin\Persist\Adapter; |
12
|
|
|
use Casbin\Persist\AdapterHelper; |
13
|
|
|
use Casbin\Persist\BatchAdapter; |
14
|
|
|
use Casbin\Persist\UpdatableAdapter; |
15
|
|
|
use Casbin\Util\Util; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FileAdapter |
19
|
|
|
* The file adapter for Casbin. |
20
|
|
|
* It can load policy from file or save policy to file. |
21
|
|
|
* |
22
|
|
|
* @author [email protected] |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
class FileAdapter implements Adapter, BatchAdapter, UpdatableAdapter |
25
|
|
|
{ |
26
|
|
|
use AdapterHelper; |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $filePath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* FileAdapter constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $filePath |
|
|
|
|
37
|
|
|
*/ |
38
|
336 |
|
public function __construct(string $filePath) |
39
|
|
|
{ |
40
|
336 |
|
$this->filePath = $filePath; |
41
|
224 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Loads all policy rules from the storage. |
45
|
|
|
* |
46
|
|
|
* @param Model $model |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @throws CasbinException |
49
|
|
|
*/ |
|
|
|
|
50
|
333 |
|
public function loadPolicy(Model $model): void |
51
|
|
|
{ |
52
|
333 |
|
if (!file_exists($this->filePath)) { |
53
|
27 |
|
throw new InvalidFilePathException('invalid file path, file path cannot be empty'); |
54
|
|
|
} |
55
|
|
|
|
56
|
312 |
|
$this->loadPolicyFile($model); |
57
|
208 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Saves all policy rules to the storage. |
61
|
|
|
* |
62
|
|
|
* @param Model $model |
|
|
|
|
63
|
|
|
* |
64
|
|
|
* @throws CasbinException |
65
|
|
|
*/ |
|
|
|
|
66
|
6 |
|
public function savePolicy(Model $model): void |
67
|
|
|
{ |
68
|
6 |
|
if ('' == $this->filePath) { |
69
|
|
|
throw new InvalidFilePathException('invalid file path, file path cannot be empty'); |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
$writeString = ''; |
73
|
|
|
|
74
|
6 |
|
if (isset($model['p'])) { |
75
|
6 |
|
foreach ($model['p'] as $ptype => $ast) { |
76
|
6 |
|
foreach ($ast->policy as $rule) { |
77
|
6 |
|
$writeString .= $ptype . ', '; |
78
|
6 |
|
$writeString .= Util::arrayToString($rule); |
79
|
6 |
|
$writeString .= PHP_EOL; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
if (isset($model['g'])) { |
85
|
3 |
|
foreach ($model['g'] as $ptype => $ast) { |
86
|
3 |
|
foreach ($ast->policy as $rule) { |
87
|
3 |
|
$writeString .= $ptype . ', '; |
88
|
3 |
|
$writeString .= Util::arrayToString($rule); |
89
|
3 |
|
$writeString .= PHP_EOL; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
$this->savePolicyFile(rtrim($writeString, PHP_EOL)); |
95
|
4 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
|
|
|
|
98
|
|
|
* @param Model $model |
|
|
|
|
99
|
|
|
* @throws InvalidFilePathException |
|
|
|
|
100
|
|
|
*/ |
|
|
|
|
101
|
312 |
|
protected function loadPolicyFile(Model $model): void |
102
|
|
|
{ |
103
|
312 |
|
$file = fopen($this->filePath, 'rb'); |
104
|
|
|
|
105
|
312 |
|
if (false === $file) { |
106
|
|
|
throw new InvalidFilePathException(sprintf('Unable to access to the specified path "%s"', $this->filePath)); |
107
|
|
|
} |
108
|
|
|
|
109
|
312 |
|
while ($line = fgets($file)) { |
110
|
312 |
|
$this->loadPolicyLine(trim($line), $model); |
111
|
|
|
} |
112
|
312 |
|
fclose($file); |
113
|
208 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
|
|
|
|
116
|
|
|
* @param string $text |
|
|
|
|
117
|
|
|
*/ |
|
|
|
|
118
|
6 |
|
protected function savePolicyFile(string $text): void |
119
|
|
|
{ |
120
|
6 |
|
file_put_contents($this->filePath, $text, LOCK_EX); |
121
|
4 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds a policy rule to the storage. |
125
|
|
|
* |
126
|
|
|
* @param string $sec |
|
|
|
|
127
|
|
|
* @param string $ptype |
|
|
|
|
128
|
|
|
* @param string[] $rule |
|
|
|
|
129
|
|
|
* |
130
|
|
|
* @throws NotImplementedException |
131
|
|
|
*/ |
|
|
|
|
132
|
36 |
|
public function addPolicy(string $sec, string $ptype, array $rule): void |
133
|
|
|
{ |
134
|
36 |
|
throw new NotImplementedException('not implemented'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Adds a policy rule to the storage. |
139
|
|
|
* |
140
|
|
|
* @param string $sec |
|
|
|
|
141
|
|
|
* @param string $ptype |
|
|
|
|
142
|
|
|
* @param string[][] $rules |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @throws NotImplementedException |
145
|
|
|
*/ |
|
|
|
|
146
|
12 |
|
public function addPolicies(string $sec, string $ptype, array $rules): void |
147
|
|
|
{ |
148
|
12 |
|
throw new NotImplementedException('not implemented'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Removes a policy rule from the storage. |
153
|
|
|
* |
154
|
|
|
* @param string $sec |
|
|
|
|
155
|
|
|
* @param string $ptype |
|
|
|
|
156
|
|
|
* @param string[] $rule |
|
|
|
|
157
|
|
|
* |
158
|
|
|
* @throws NotImplementedException |
159
|
|
|
*/ |
|
|
|
|
160
|
33 |
|
public function removePolicy(string $sec, string $ptype, array $rule): void |
161
|
|
|
{ |
162
|
33 |
|
throw new NotImplementedException('not implemented'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Removes a policy rules from the storage. |
167
|
|
|
* |
168
|
|
|
* @param string $sec |
|
|
|
|
169
|
|
|
* @param string $ptype |
|
|
|
|
170
|
|
|
* @param string[][] $rules |
|
|
|
|
171
|
|
|
* |
172
|
|
|
* @throws NotImplementedException |
173
|
|
|
*/ |
|
|
|
|
174
|
15 |
|
public function removePolicies(string $sec, string $ptype, array $rules): void |
175
|
|
|
{ |
176
|
15 |
|
throw new NotImplementedException('not implemented'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Removes policy rules that match the filter from the storage. |
181
|
|
|
* |
182
|
|
|
* @param string $sec |
|
|
|
|
183
|
|
|
* @param string $ptype |
|
|
|
|
184
|
|
|
* @param int $fieldIndex |
|
|
|
|
185
|
|
|
* @param string ...$fieldValues |
|
|
|
|
186
|
|
|
* |
187
|
|
|
* @throws NotImplementedException |
188
|
|
|
*/ |
|
|
|
|
189
|
24 |
|
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void |
190
|
|
|
{ |
191
|
24 |
|
throw new NotImplementedException('not implemented'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Updates a policy rule from storage. |
196
|
|
|
* This is part of the Auto-Save feature. |
197
|
|
|
* |
198
|
|
|
* @param string $sec |
|
|
|
|
199
|
|
|
* @param string $ptype |
|
|
|
|
200
|
|
|
* @param string[] $oldRule |
|
|
|
|
201
|
|
|
* @param string[] $newPolicy |
|
|
|
|
202
|
|
|
*/ |
|
|
|
|
203
|
3 |
|
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void |
204
|
|
|
{ |
205
|
3 |
|
throw new NotImplementedException('not implemented'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* UpdatePolicies updates some policy rules to storage, like db, redis. |
210
|
|
|
* |
211
|
|
|
* @param string $sec |
|
|
|
|
212
|
|
|
* @param string $ptype |
|
|
|
|
213
|
|
|
* @param string[][] $oldRules |
|
|
|
|
214
|
|
|
* @param string[][] $newRules |
|
|
|
|
215
|
|
|
* @return void |
|
|
|
|
216
|
|
|
*/ |
217
|
3 |
|
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void |
218
|
|
|
{ |
219
|
3 |
|
throw new NotImplementedException('not implemented'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* UpdateFilteredPolicies deletes old rules and adds new rules. |
224
|
|
|
* |
225
|
|
|
* @param string $sec |
|
|
|
|
226
|
|
|
* @param string $ptype |
|
|
|
|
227
|
|
|
* @param array $newPolicies |
|
|
|
|
228
|
|
|
* @param integer $fieldIndex |
|
|
|
|
229
|
|
|
* @param string ...$fieldValues |
|
|
|
|
230
|
|
|
* @return array |
|
|
|
|
231
|
|
|
*/ |
232
|
|
|
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array |
233
|
|
|
{ |
234
|
|
|
throw new NotImplementedException('not implemented'); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|