Skip to content

Commit

Permalink
Merge pull request #15 from basakest/update-policies
Browse files Browse the repository at this point in the history
feat: support updatePolicies method, fix #14
  • Loading branch information
leeqvip authored Jul 30, 2021
2 parents 5519e48 + d3fc5c6 commit 322f077
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Casbin\Persist\Adapter as AdapterContract;
use TechOne\Database\Manager;
use Casbin\Persist\AdapterHelper;
use Casbin\Persist\FilteredAdapter;
use Casbin\Persist\FilteredAdapter as FilteredAdapterContract;
use Casbin\Persist\Adapters\Filter;
use Casbin\Exceptions\InvalidFilterTypeException;
use Casbin\Persist\BatchAdapter;
use Casbin\Persist\UpdatableAdapter;
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
use Casbin\Persist\UpdatableAdapter as UpdatableAdapterContract;
use Closure;
use Throwable;

Expand All @@ -19,7 +19,7 @@
*
* @author [email protected]
*/
class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter, UpdatableAdapter
class Adapter implements AdapterContract, FilteredAdapterContract, BatchAdapterContract, UpdatableAdapterContract
{
use AdapterHelper;

Expand Down Expand Up @@ -305,4 +305,27 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $

$this->connection->execute($sql, array_merge($updateValue, $where));
}

/**
* UpdatePolicies updates some policy rules to storage, like db, redis.
*
* @param string $sec
* @param string $ptype
* @param string[][] $oldRules
* @param string[][] $newRules
* @return void
*/
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
{
$this->connection->getPdo()->beginTransaction();
try {
foreach ($oldRules as $i => $oldRule) {
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
}
$this->connection->getPdo()->commit();
} catch (Throwable $e) {
$this->connection->getPdo()->rollback();
throw $e;
}
}
}
28 changes: 28 additions & 0 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,34 @@ public function testUpdatePolicy()
], $e->getPolicy());
}

public function testUpdatePolicies()
{
$e = $this->getEnforcer();
$this->assertEquals([
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
], $e->getPolicy());

$oldPolicies = [
['alice', 'data1', 'read'],
['bob', 'data2', 'write']
];
$newPolicies = [
['alice', 'data1', 'write'],
['bob', 'data2', 'read']
];
$e->updatePolicies($oldPolicies, $newPolicies);

$this->assertEquals([
['alice', 'data1', 'write'],
['bob', 'data2', 'read'],
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
], $e->getPolicy());
}

protected function env($key, $default = null)
{
$value = getenv($key);
Expand Down

0 comments on commit 322f077

Please sign in to comment.