Skip to content

Commit fc7b2d1

Browse files
authored
Merge pull request #3 from basakest/batch-adapter
feat: support Casbin BatchAdapter interface
2 parents 68ee8df + 2efb308 commit fc7b2d1

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

src/Adapter.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CasbinAdapter\Medoo;
44

55
use Casbin\Persist\Adapter as AdapterContract;
6+
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
67
use Casbin\Persist\AdapterHelper;
78
use Casbin\Model\Model;
89
use Medoo\Medoo;
@@ -12,7 +13,7 @@
1213
*
1314
1415
*/
15-
class Adapter implements AdapterContract
16+
class Adapter implements AdapterContract, BatchAdapterContract
1617
{
1718
use AdapterHelper;
1819

@@ -78,7 +79,7 @@ public function initTable()
7879
*
7980
* @param string $ptype
8081
* @param array $rule
81-
*
82+
*
8283
* @return void
8384
*/
8485
public function savePolicyLine(string $ptype, array $rule): void
@@ -95,7 +96,7 @@ public function savePolicyLine(string $ptype, array $rule): void
9596
* loads all policy rules from the storage.
9697
*
9798
* @param Model $model
98-
*
99+
*
99100
* @return void
100101
*/
101102
public function loadPolicy(Model $model): void
@@ -145,6 +146,30 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
145146
$this->savePolicyLine($ptype, $rule);
146147
}
147148

149+
/**
150+
* Adds a policy rules to the storage.
151+
* This is part of the Auto-Save feature.
152+
*
153+
* @param string $sec
154+
* @param string $ptype
155+
* @param string[][] $rules
156+
*/
157+
public function addPolicies(string $sec, string $ptype, array $rules): void
158+
{
159+
$cols = [];
160+
$i = 0;
161+
162+
foreach ($rules as $rule) {
163+
$temp['ptype'] = $ptype;
164+
foreach ($rule as $key => $value) {
165+
$temp['v'. strval($key)] = $value;
166+
}
167+
$cols[$i++] = $temp ?? [];
168+
$temp = [];
169+
}
170+
$this->database->insert($this->casbinRuleTableName, $cols);
171+
}
172+
148173
/**
149174
* This is part of the Auto-Save feature.
150175
*
@@ -165,6 +190,23 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
165190
$this->database->delete($this->casbinRuleTableName, ['AND' => $where]);
166191
}
167192

193+
/**
194+
* Removes policy rules from the storage.
195+
* This is part of the Auto-Save feature.
196+
*
197+
* @param string $sec
198+
* @param string $ptype
199+
* @param string[][] $rules
200+
*/
201+
public function removePolicies(string $sec, string $ptype, array $rules): void
202+
{
203+
$this->database->action(function () use ($sec, $ptype, $rules) {
204+
foreach ($rules as $rule) {
205+
$this->removePolicy($sec, $ptype, $rule);
206+
}
207+
});
208+
}
209+
168210
/**
169211
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
170212
* This is part of the Auto-Save feature.

tests/AdapterTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public function testAddPolicy()
8888
$this->assertTrue($e->enforce('eve', 'data3', 'read'));
8989
}
9090

91+
public function testAddPolicies()
92+
{
93+
$policies = [
94+
['u1', 'd1', 'read'],
95+
['u2', 'd2', 'read'],
96+
['u3', 'd3', 'read'],
97+
];
98+
$e = $this->getEnforcer();
99+
$e->clearPolicy();
100+
$this->assertEquals([], $e->getPolicy());
101+
$e->addPolicies($policies);
102+
$this->assertEquals($policies, $e->getPolicy());
103+
}
104+
91105
public function testSavePolicy()
92106
{
93107
$e = $this->getEnforcer();
@@ -112,6 +126,27 @@ public function testRemovePolicy()
112126
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
113127
}
114128

129+
public function testRemovePolicies()
130+
{
131+
$e = $this->getEnforcer();
132+
$this->assertEquals([
133+
['alice', 'data1', 'read'],
134+
['bob', 'data2', 'write'],
135+
['data2_admin', 'data2', 'read'],
136+
['data2_admin', 'data2', 'write'],
137+
], $e->getPolicy());
138+
139+
$e->removePolicies([
140+
['data2_admin', 'data2', 'read'],
141+
['data2_admin', 'data2', 'write'],
142+
]);
143+
144+
$this->assertEquals([
145+
['alice', 'data1', 'read'],
146+
['bob', 'data2', 'write']
147+
], $e->getPolicy());
148+
}
149+
115150
public function testRemoveFilteredPolicy()
116151
{
117152
$e = $this->getEnforcer();

0 commit comments

Comments
 (0)