Skip to content

Commit

Permalink
feat: support updatePolicies method
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest committed Aug 2, 2021
1 parent f0b6e04 commit feb4f17
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
$queryBuilder->execute();
}

/**
* 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->transactional(function () use ($sec, $ptype, $oldRules, $newRules) {
foreach ($oldRules as $i => $oldRule) {
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
}
});
}

/**
* Returns true if the loaded policy has been filtered.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,34 @@ public function testUpdatePolicy()
['data2_admin', 'data2', 'write'],
], $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());
}
}

0 comments on commit feb4f17

Please sign in to comment.