Skip to content

Commit c40bdba

Browse files
authored
Merge pull request #4 from basakest/updatable-adapter
feat: support Casbin UpdatableAdapter interface
2 parents fc7b2d1 + 26ffcaa commit c40bdba

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/Adapter.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Casbin\Persist\Adapter as AdapterContract;
66
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
7+
use Casbin\Persist\UpdatableAdapter as UpdatableAdapterContract;
78
use Casbin\Persist\AdapterHelper;
89
use Casbin\Model\Model;
910
use Medoo\Medoo;
@@ -13,7 +14,7 @@
1314
*
1415
1516
*/
16-
class Adapter implements AdapterContract, BatchAdapterContract
17+
class Adapter implements AdapterContract, BatchAdapterContract, UpdatableAdapterContract
1718
{
1819
use AdapterHelper;
1920

@@ -199,7 +200,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
199200
* @param string[][] $rules
200201
*/
201202
public function removePolicies(string $sec, string $ptype, array $rules): void
202-
{
203+
{
203204
$this->database->action(function () use ($sec, $ptype, $rules) {
204205
foreach ($rules as $rule) {
205206
$this->removePolicy($sec, $ptype, $rule);
@@ -233,6 +234,31 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
233234
$this->database->delete($this->casbinRuleTableName, ['AND' => $where]);
234235
}
235236

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+
236262
/**
237263
* Gets database.
238264
*

tests/AdapterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,34 @@ public function testRemoveFilteredPolicy()
170170
$this->assertFalse($e->enforce('alice', 'data2', 'write'));
171171
}
172172

173+
public function testUpdatePolicy()
174+
{
175+
$e = $this->getEnforcer();
176+
$this->assertEquals([
177+
['alice', 'data1', 'read'],
178+
['bob', 'data2', 'write'],
179+
['data2_admin', 'data2', 'read'],
180+
['data2_admin', 'data2', 'write'],
181+
], $e->getPolicy());
182+
183+
$e->updatePolicy(
184+
['alice', 'data1', 'read'],
185+
['alice', 'data1', 'write']
186+
);
187+
188+
$e->updatePolicy(
189+
['bob', 'data2', 'write'],
190+
['bob', 'data2', 'read']
191+
);
192+
193+
$this->assertEquals([
194+
['alice', 'data1', 'write'],
195+
['bob', 'data2', 'read'],
196+
['data2_admin', 'data2', 'read'],
197+
['data2_admin', 'data2', 'write'],
198+
], $e->getPolicy());
199+
}
200+
173201
protected function env($key, $default = null)
174202
{
175203
$value = getenv($key);

0 commit comments

Comments
 (0)