Skip to content

Commit 6342697

Browse files
committed
feat: support updatePolicies method
1 parent 3bee8c4 commit 6342697

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Adapter.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,18 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
274274
*/
275275
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
276276
{
277-
$where = ['ptype' => $ptype,];
277+
$where = ['ptype' => $ptype];
278278

279279
foreach ($oldRule as $k => $v) {
280280
$where['v' . strval($k)] = $v;
281281
}
282282

283283
$columns = [];
284284
foreach ($newPolicy as $k => $v) {
285-
$columns['v' . strval($k)][$where['v' . strval($k)]] = $v;
285+
$columns['v' . strval($k)] = $v;
286286
}
287287

288-
$this->database->replace($this->casbinRuleTableName, $columns, $where);
288+
$this->database->update($this->casbinRuleTableName, $columns, $where);
289289
}
290290

291291
/**
@@ -299,7 +299,11 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
299299
*/
300300
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
301301
{
302-
throw new CasbinException('not implemented');
302+
$this->database->action(function () use ($sec, $ptype, $oldRules, $newRules) {
303+
foreach ($oldRules as $i => $oldRule) {
304+
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
305+
}
306+
});
303307
}
304308

305309
/**

tests/AdapterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,34 @@ public function testUpdatePolicy()
200200
], $e->getPolicy());
201201
}
202202

203+
public function testUpdatePolicies()
204+
{
205+
$e = $this->getEnforcer();
206+
$this->assertEquals([
207+
['alice', 'data1', 'read'],
208+
['bob', 'data2', 'write'],
209+
['data2_admin', 'data2', 'read'],
210+
['data2_admin', 'data2', 'write'],
211+
], $e->getPolicy());
212+
213+
$oldRules = [
214+
['alice', 'data1', 'read'],
215+
['bob', 'data2', 'write']
216+
];
217+
$newRules = [
218+
['alice', 'data1', 'write'],
219+
['bob', 'data2', 'read']
220+
];
221+
$e->updatePolicies($oldRules, $newRules);
222+
223+
$this->assertEquals([
224+
['alice', 'data1', 'write'],
225+
['bob', 'data2', 'read'],
226+
['data2_admin', 'data2', 'read'],
227+
['data2_admin', 'data2', 'write'],
228+
], $e->getPolicy());
229+
}
230+
203231
public function testLoadFilteredPolicy()
204232
{
205233
$e = $this->getEnforcer();

0 commit comments

Comments
 (0)