Skip to content

Commit acf902c

Browse files
authored
Merge pull request #11 from basakest/UpdatableDatabaseAdapter
feat: support Casbin UpdatableAdapter interface
2 parents 96e2eb7 + 60848b4 commit acf902c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Diff for: src/Adapter.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Casbin\Persist\Adapters\Filter;
1111
use Casbin\Exceptions\InvalidFilterTypeException;
1212
use Casbin\Persist\BatchAdapter;
13+
use Casbin\Persist\UpdatableAdapter;
1314
use Closure;
1415
use Throwable;
1516

@@ -18,7 +19,7 @@
1819
*
1920
2021
*/
21-
class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter
22+
class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter, UpdatableAdapter
2223
{
2324
use AdapterHelper;
2425

@@ -274,4 +275,36 @@ public function loadFilteredPolicy(Model $model, $filter): void
274275
}
275276
$this->filtered = true;
276277
}
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+
}
277310
}

Diff for: tests/AdapterTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,34 @@ public function testRemoveFilteredPolicy()
208208
$this->assertFalse($e->enforce('alice', 'data2', 'write'));
209209
}
210210

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+
['alice', 'data1', 'read'],
223+
['alice', 'data1', 'write']
224+
);
225+
226+
$e->updatePolicy(
227+
['bob', 'data2', 'write'],
228+
['bob', 'data2', 'read']
229+
);
230+
231+
$this->assertEquals([
232+
['alice', 'data1', 'write'],
233+
['bob', 'data2', 'read'],
234+
['data2_admin', 'data2', 'read'],
235+
['data2_admin', 'data2', 'write'],
236+
], $e->getPolicy());
237+
}
238+
211239
protected function env($key, $default = null)
212240
{
213241
$value = getenv($key);

0 commit comments

Comments
 (0)