|
4 | 4 |
|
5 | 5 | use Casbin\Persist\Adapter as AdapterContract; |
6 | 6 | use Casbin\Persist\BatchAdapter as BatchAdapterContract; |
| 7 | +use Casbin\Persist\UpdatableAdapter as UpdatableAdapterContract; |
7 | 8 | use Casbin\Persist\AdapterHelper; |
8 | 9 | use Casbin\Model\Model; |
9 | 10 | use Medoo\Medoo; |
|
13 | 14 | * |
14 | 15 | |
15 | 16 | */ |
16 | | -class Adapter implements AdapterContract, BatchAdapterContract |
| 17 | +class Adapter implements AdapterContract, BatchAdapterContract, UpdatableAdapterContract |
17 | 18 | { |
18 | 19 | use AdapterHelper; |
19 | 20 |
|
@@ -199,7 +200,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void |
199 | 200 | * @param string[][] $rules |
200 | 201 | */ |
201 | 202 | public function removePolicies(string $sec, string $ptype, array $rules): void |
202 | | - { |
| 203 | + { |
203 | 204 | $this->database->action(function () use ($sec, $ptype, $rules) { |
204 | 205 | foreach ($rules as $rule) { |
205 | 206 | $this->removePolicy($sec, $ptype, $rule); |
@@ -233,6 +234,31 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex |
233 | 234 | $this->database->delete($this->casbinRuleTableName, ['AND' => $where]); |
234 | 235 | } |
235 | 236 |
|
| 237 | + /** |
| 238 | + * Updates a policy rule from storage. |
| 239 | + * This is part of the Auto-Save feature. |
| 240 | + * |
| 241 | + * @param string $sec |
| 242 | + * @param string $ptype |
| 243 | + * @param string[] $oldRule |
| 244 | + * @param string[] $newPolicy |
| 245 | + */ |
| 246 | + public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void |
| 247 | + { |
| 248 | + $where = ['ptype' => $ptype,]; |
| 249 | + |
| 250 | + foreach ($oldRule as $k => $v) { |
| 251 | + $where['v' . strval($k)] = $v; |
| 252 | + } |
| 253 | + |
| 254 | + $columns = []; |
| 255 | + foreach ($newPolicy as $k => $v) { |
| 256 | + $columns['v' . strval($k)][$where['v' . strval($k)]] = $v; |
| 257 | + } |
| 258 | + |
| 259 | + $this->database->replace($this->casbinRuleTableName, $columns, $where); |
| 260 | + } |
| 261 | + |
236 | 262 | /** |
237 | 263 | * Gets database. |
238 | 264 | * |
|
0 commit comments