Skip to content

Commit feb4f17

Browse files
committed
feat: support updatePolicies method
1 parent f0b6e04 commit feb4f17

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Adapter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,24 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
361361
$queryBuilder->execute();
362362
}
363363

364+
/**
365+
* UpdatePolicies updates some policy rules to storage, like db, redis.
366+
*
367+
* @param string $sec
368+
* @param string $ptype
369+
* @param string[][] $oldRules
370+
* @param string[][] $newRules
371+
* @return void
372+
*/
373+
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
374+
{
375+
$this->connection->transactional(function () use ($sec, $ptype, $oldRules, $newRules) {
376+
foreach ($oldRules as $i => $oldRule) {
377+
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
378+
}
379+
});
380+
}
381+
364382
/**
365383
* Returns true if the loaded policy has been filtered.
366384
*

tests/AdapterTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,34 @@ public function testUpdatePolicy()
181181
['data2_admin', 'data2', 'write'],
182182
], $e->getPolicy());
183183
}
184+
185+
public function testUpdatePolicies()
186+
{
187+
$e = $this->getEnforcer();
188+
$this->assertEquals([
189+
['alice', 'data1', 'read'],
190+
['bob', 'data2', 'write'],
191+
['data2_admin', 'data2', 'read'],
192+
['data2_admin', 'data2', 'write'],
193+
], $e->getPolicy());
194+
195+
$oldPolicies = [
196+
['alice', 'data1', 'read'],
197+
['bob', 'data2', 'write']
198+
];
199+
200+
$newPolicies = [
201+
['alice', 'data1', 'write'],
202+
['bob', 'data2', 'read']
203+
];
204+
205+
$e->updatePolicies($oldPolicies, $newPolicies);
206+
207+
$this->assertEquals([
208+
['alice', 'data1', 'write'],
209+
['bob', 'data2', 'read'],
210+
['data2_admin', 'data2', 'read'],
211+
['data2_admin', 'data2', 'write'],
212+
], $e->getPolicy());
213+
}
184214
}

0 commit comments

Comments
 (0)