Skip to content

Commit 8da7014

Browse files
authored
Merge pull request #3 from basakest/batch-adapter
feat: support Casbin BatchAdapter interface
2 parents 678835d + cc0b289 commit 8da7014

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/Adapter.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CasbinAdapter\LaminasDb;
44

55
use Casbin\Persist\Adapter as AdapterContract;
6+
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
67
use Casbin\Persist\AdapterHelper;
78
use Laminas\Db\Adapter\AdapterInterface as LaminasDbAdapterInterface;
89
use Laminas\Db\Adapter\Adapter as LaminasDbAdapter;
@@ -16,7 +17,7 @@
1617
*
1718
1819
*/
19-
class Adapter implements AdapterContract
20+
class Adapter implements AdapterContract, BatchAdapterContract
2021
{
2122
use AdapterHelper;
2223

@@ -25,6 +26,11 @@ class Adapter implements AdapterContract
2526
*/
2627
protected $tableGateway;
2728

29+
/**
30+
* @var LaminasDbAdapterInterface
31+
*/
32+
protected $dbAdapter;
33+
2834
/**
2935
* default table name.
3036
*
@@ -144,6 +150,35 @@ public function addPolicy($sec, $ptype, $rule): void
144150
$this->savePolicyLine($ptype, $rule);
145151
}
146152

153+
/**
154+
* Adds a policy rules to 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 addPolicies(string $sec, string $ptype, array $rules): void
162+
{
163+
$columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
164+
$values = [];
165+
$sets = [];
166+
$columnsCount = count($columns);
167+
foreach ($rules as $rule) {
168+
array_unshift($rule, $ptype);
169+
$values = array_merge($values, array_pad($rule, $columnsCount, null));
170+
$sets[] = array_pad([], $columnsCount, '?');
171+
}
172+
$valuesStr = implode(', ', array_map(function ($set) {
173+
return '(' . implode(', ', $set) . ')';
174+
}, $sets));
175+
$sql = 'INSERT INTO ' . $this->casbinRuleTableName . ' (' . implode(', ', $columns) . ')' . ' VALUES' . $valuesStr;
176+
177+
$driver = $this->tableGateway->adapter->getDriver();
178+
$statement = $driver->createStatement($sql);
179+
$result = $statement->execute($values);
180+
}
181+
147182
/**
148183
* This is part of the Auto-Save feature.
149184
*
@@ -161,6 +196,23 @@ public function removePolicy($sec, $ptype, $rule): void
161196
$this->tableGateway->delete($where);
162197
}
163198

199+
/**
200+
* Removes policy rules from the storage.
201+
* This is part of the Auto-Save feature.
202+
*
203+
* @param string $sec
204+
* @param string $ptype
205+
* @param string[][] $rules
206+
*/
207+
public function removePolicies(string $sec, string $ptype, array $rules): void
208+
{
209+
$this->tableGateway->adapter->getDriver()->getConnection()->beginTransaction(function () use ($sec, $ptype, $rules) {
210+
foreach ($rules as $rule) {
211+
$this->removePolicy($sec, $ptype, $rule);
212+
}
213+
});
214+
}
215+
164216
/**
165217
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
166218
* 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
@@ -142,6 +142,20 @@ public function testAddPolicy()
142142
$this->assertTrue($e->enforce('eve', 'data3', 'read'));
143143
}
144144

145+
public function testAddPolicies()
146+
{
147+
$policies = [
148+
['u1', 'd1', 'read'],
149+
['u2', 'd2', 'read'],
150+
['u3', 'd3', 'read'],
151+
];
152+
$e = $this->getEnforcer();
153+
$e->clearPolicy();
154+
$this->assertEquals([], $e->getPolicy());
155+
$e->addPolicies($policies);
156+
$this->assertEquals($policies, $e->getPolicy());
157+
}
158+
145159
public function testSavePolicy()
146160
{
147161
$e = $this->getEnforcer();
@@ -164,6 +178,27 @@ public function testRemovePolicy()
164178
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
165179
}
166180

181+
public function testRemovePolicies()
182+
{
183+
$e = $this->getEnforcer();
184+
$this->assertEquals([
185+
['alice', 'data1', 'read'],
186+
['bob', 'data2', 'write'],
187+
['data2_admin', 'data2', 'read'],
188+
['data2_admin', 'data2', 'write'],
189+
], $e->getPolicy());
190+
191+
$e->removePolicies([
192+
['data2_admin', 'data2', 'read'],
193+
['data2_admin', 'data2', 'write'],
194+
]);
195+
196+
$this->assertEquals([
197+
['alice', 'data1', 'read'],
198+
['bob', 'data2', 'write']
199+
], $e->getPolicy());
200+
}
201+
167202
public function testRemoveFilteredPolicy()
168203
{
169204
$e = $this->getEnforcer();

0 commit comments

Comments
 (0)