Skip to content

Commit

Permalink
Merge pull request #3 from basakest/update-policies
Browse files Browse the repository at this point in the history
feat: support updatePolicies method
  • Loading branch information
leeqvip authored Aug 2, 2021
2 parents f0b6e04 + feb4f17 commit 5f101c0
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 5f101c0

Please sign in to comment.