Skip to content

Commit b159ac4

Browse files
authored
feat: support Casbin BatchAdapter interface (#26)
1 parent 69eae24 commit b159ac4

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/adapter/DatabaseAdapter.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Casbin\Persist\Adapter;
88
use Casbin\Persist\AdapterHelper;
99
use Casbin\Persist\UpdatableAdapter;
10+
use Casbin\Persist\BatchAdapter;
1011
use think\facade\Db;
1112

1213
/**
1314
* DatabaseAdapter.
1415
*
1516
1617
*/
17-
class DatabaseAdapter implements Adapter, UpdatableAdapter
18+
class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter
1819
{
1920
use AdapterHelper;
2021

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

105+
/**
106+
* Adds a policy rules to the storage.
107+
* This is part of the Auto-Save feature.
108+
*
109+
* @param string $sec
110+
* @param string $ptype
111+
* @param string[][] $rules
112+
*/
113+
public function addPolicies(string $sec, string $ptype, array $rules): void
114+
{
115+
$cols = [];
116+
$i = 0;
117+
118+
foreach ($rules as $rule) {
119+
$temp['ptype'] = $ptype;
120+
foreach ($rule as $key => $value) {
121+
$temp['v' . strval($key)] = $value;
122+
}
123+
$cols[$i++] = $temp;
124+
$temp = [];
125+
}
126+
$this->model->cache('tauthz')->insertAll($cols);
127+
}
128+
104129
/**
105130
* This is part of the Auto-Save feature.
106131
*
@@ -125,6 +150,23 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
125150
}
126151
}
127152

153+
/**
154+
* Removes policy rules from the storage.
155+
* This is part of the Auto-Save feature.
156+
*
157+
* @param string $sec
158+
* @param string $ptype
159+
* @param string[][] $rules
160+
*/
161+
public function removePolicies(string $sec, string $ptype, array $rules): void
162+
{
163+
Db::transaction(function () use ($sec, $ptype, $rules) {
164+
foreach ($rules as $rule) {
165+
$this->removePolicy($sec, $ptype, $rule);
166+
}
167+
});
168+
}
169+
128170
/**
129171
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
130172
* This is part of the Auto-Save feature.

tests/DatabaseAdapterTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ public function testAddPolicy()
3030
});
3131
}
3232

33+
public function testAddPolicies()
34+
{
35+
$this->testing(function () {
36+
$policies = [
37+
['u1', 'd1', 'read'],
38+
['u2', 'd2', 'read'],
39+
['u3', 'd3', 'read'],
40+
];
41+
Enforcer::clearPolicy();
42+
$this->initTable();
43+
$this->assertEquals([], Enforcer::getPolicy());
44+
Enforcer::addPolicies($policies);
45+
$this->assertEquals($policies, Enforcer::getPolicy());
46+
});
47+
}
48+
3349
public function testSavePolicy()
3450
{
3551
$this->testing(function () {
@@ -58,6 +74,28 @@ public function testRemovePolicy()
5874
});
5975
}
6076

77+
public function testRemovePolicies()
78+
{
79+
$this->testing(function () {
80+
$this->assertEquals([
81+
['alice', 'data1', 'read'],
82+
['bob', 'data2', 'write'],
83+
['data2_admin', 'data2', 'read'],
84+
['data2_admin', 'data2', 'write'],
85+
], Enforcer::getPolicy());
86+
87+
Enforcer::removePolicies([
88+
['data2_admin', 'data2', 'read'],
89+
['data2_admin', 'data2', 'write'],
90+
]);
91+
92+
$this->assertEquals([
93+
['alice', 'data1', 'read'],
94+
['bob', 'data2', 'write']
95+
], Enforcer::getPolicy());
96+
});
97+
}
98+
6199
public function testRemoveFilteredPolicy()
62100
{
63101
$this->testing(function () {

0 commit comments

Comments
 (0)