Skip to content

Commit

Permalink
feat: support Casbin BatchAdapter interface (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest authored Aug 10, 2021
1 parent 69eae24 commit b159ac4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/adapter/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Casbin\Persist\Adapter;
use Casbin\Persist\AdapterHelper;
use Casbin\Persist\UpdatableAdapter;
use Casbin\Persist\BatchAdapter;
use think\facade\Db;

/**
* DatabaseAdapter.
*
* @author techlee@qq.com
*/
class DatabaseAdapter implements Adapter, UpdatableAdapter
class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter
{
use AdapterHelper;

Expand Down Expand Up @@ -101,6 +102,30 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
$this->savePolicyLine($ptype, $rule);
}

/**
* Adds a policy rules to the storage.
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string[][] $rules
*/
public function addPolicies(string $sec, string $ptype, array $rules): void
{
$cols = [];
$i = 0;

foreach ($rules as $rule) {
$temp['ptype'] = $ptype;
foreach ($rule as $key => $value) {
$temp['v' . strval($key)] = $value;
}
$cols[$i++] = $temp;
$temp = [];
}
$this->model->cache('tauthz')->insertAll($cols);
}

/**
* This is part of the Auto-Save feature.
*
Expand All @@ -125,6 +150,23 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
}
}

/**
* Removes policy rules from the storage.
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string[][] $rules
*/
public function removePolicies(string $sec, string $ptype, array $rules): void
{
Db::transaction(function () use ($sec, $ptype, $rules) {
foreach ($rules as $rule) {
$this->removePolicy($sec, $ptype, $rule);
}
});
}

/**
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
* This is part of the Auto-Save feature.
Expand Down
38 changes: 38 additions & 0 deletions tests/DatabaseAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public function testAddPolicy()
});
}

public function testAddPolicies()
{
$this->testing(function () {
$policies = [
['u1', 'd1', 'read'],
['u2', 'd2', 'read'],
['u3', 'd3', 'read'],
];
Enforcer::clearPolicy();
$this->initTable();
$this->assertEquals([], Enforcer::getPolicy());
Enforcer::addPolicies($policies);
$this->assertEquals($policies, Enforcer::getPolicy());
});
}

public function testSavePolicy()
{
$this->testing(function () {
Expand Down Expand Up @@ -58,6 +74,28 @@ public function testRemovePolicy()
});
}

public function testRemovePolicies()
{
$this->testing(function () {
$this->assertEquals([
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
], Enforcer::getPolicy());

Enforcer::removePolicies([
['data2_admin', 'data2', 'read'],
['data2_admin', 'data2', 'write'],
]);

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

public function testRemoveFilteredPolicy()
{
$this->testing(function () {
Expand Down

0 comments on commit b159ac4

Please sign in to comment.