We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 96e2eb7 + 60848b4 commit acf902cCopy full SHA for acf902c
2 files changed
src/Adapter.php
@@ -10,6 +10,7 @@
10
use Casbin\Persist\Adapters\Filter;
11
use Casbin\Exceptions\InvalidFilterTypeException;
12
use Casbin\Persist\BatchAdapter;
13
+use Casbin\Persist\UpdatableAdapter;
14
use Closure;
15
use Throwable;
16
@@ -18,7 +19,7 @@
18
19
*
20
* @author techlee@qq.com
21
*/
-class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter
22
+class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter, UpdatableAdapter
23
{
24
use AdapterHelper;
25
@@ -274,4 +275,36 @@ public function loadFilteredPolicy(Model $model, $filter): void
274
275
}
276
$this->filtered = true;
277
278
+
279
+ /**
280
+ * Updates a policy rule from storage.
281
+ * This is part of the Auto-Save feature.
282
+ *
283
+ * @param string $sec
284
+ * @param string $ptype
285
+ * @param string[] $oldRule
286
+ * @param string[] $newPolicy
287
+ */
288
+ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
289
+ {
290
+ $where['ptype'] = $ptype;
291
+ $condition[] = 'ptype = :ptype';
292
293
+ foreach($oldRule as $key => $value) {
294
+ $placeholder = "w" . strval($key);
295
+ $where['w' . strval($key)] = $value;
296
+ $condition[] = 'v' . strval($key) . ' = :' . $placeholder;
297
+ }
298
299
+ $update = [];
300
+ foreach($newPolicy as $key => $value) {
301
+ $placeholder = "s" . strval($key);
302
+ $updateValue["$placeholder"] = $value;
303
+ $update[] = 'v' . strval($key) . ' = :' . $placeholder;
304
305
306
+ $sql = "UPDATE {$this->casbinRuleTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition);
307
308
+ $this->connection->execute($sql, array_merge($updateValue, $where));
309
310
tests/AdapterTest.php
@@ -208,6 +208,34 @@ public function testRemoveFilteredPolicy()
208
$this->assertFalse($e->enforce('alice', 'data2', 'write'));
209
210
211
+ public function testUpdatePolicy()
212
213
+ $e = $this->getEnforcer();
214
+ $this->assertEquals([
215
+ ['alice', 'data1', 'read'],
216
+ ['bob', 'data2', 'write'],
217
+ ['data2_admin', 'data2', 'read'],
218
+ ['data2_admin', 'data2', 'write'],
219
+ ], $e->getPolicy());
220
221
+ $e->updatePolicy(
222
223
+ ['alice', 'data1', 'write']
224
+ );
225
226
227
228
+ ['bob', 'data2', 'read']
229
230
231
232
+ ['alice', 'data1', 'write'],
233
+ ['bob', 'data2', 'read'],
234
235
236
237
238
239
protected function env($key, $default = null)
240
241
$value = getenv($key);
0 commit comments