Skip to content

Commit 2242090

Browse files
authored
Merge pull request #6 from basakest/BatchAdapter
try to implement BatchAdapter
2 parents 63da5ec + 8852cf8 commit 2242090

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

Diff for: src/Adapter.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
use Casbin\Persist\FilteredAdapter;
1010
use Casbin\Persist\Adapters\Filter;
1111
use Casbin\Exceptions\InvalidFilterTypeException;
12+
use Casbin\Persist\BatchAdapter;
1213
use Closure;
14+
use Throwable;
1315

1416
/**
1517
* DatabaseAdapter.
1618
*
1719
1820
*/
19-
class Adapter implements AdapterContract, FilteredAdapter
21+
class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter
2022
{
2123
use AdapterHelper;
2224

@@ -136,6 +138,38 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
136138
$this->savePolicyLine($ptype, $rule);
137139
}
138140

141+
public function addPolicies(string $sec, string $ptype, array $rules): void
142+
{
143+
$table = $this->casbinRuleTableName;
144+
$columns = ['p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
145+
$values = [];
146+
$sets = [];
147+
$columnsCount = count($columns);
148+
foreach ($rules as $rule) {
149+
$values = array_merge($values, array_pad($rule, $columnsCount, null));
150+
$sets[] = array_pad([], $columnsCount, '?');
151+
}
152+
$valuesStr = implode(', ', array_map(function ($set) {
153+
return '(' . implode(', ', $set) . ')';
154+
}, $sets));
155+
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $columns) . ')' .
156+
' VALUES' . $valuesStr;
157+
}
158+
159+
public function removePolicies(string $sec, string $ptype, array $rules): void
160+
{
161+
$this->connection->getPdo()->beginTransaction();
162+
try {
163+
foreach($rules as $rule) {
164+
$this->removePolicy($sec, $ptype, $rule);
165+
}
166+
$this->connection->getPdo()->commit();
167+
} catch (Throwable $e){
168+
$this->connection->getPdo()->rollback();
169+
throw $e;
170+
}
171+
}
172+
139173
/**
140174
* This is part of the Auto-Save feature.
141175
*

Diff for: tests/AdapterTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ public function testAddPolicy()
125125
$this->assertTrue($e->enforce('eve', 'data3', 'read'));
126126
}
127127

128+
public function testAddPolicies()
129+
{
130+
$policies = [
131+
['u1', 'd1', 'read'],
132+
['u2', 'd2', 'read'],
133+
['u3', 'd3', 'read'],
134+
];
135+
$e = $this->getEnforcer();
136+
$e->clearPolicy();
137+
$this->initDb(DatabaseAdapter::newAdapter($this->config));
138+
$this->assertEquals([], $e->getPolicy());
139+
$e->addPolicies($policies);
140+
$this->assertEquals($policies, $e->getPolicy());
141+
}
142+
128143
public function testSavePolicy()
129144
{
130145
$e = $this->getEnforcer();
@@ -149,6 +164,27 @@ public function testRemovePolicy()
149164
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
150165
}
151166

167+
public function testRemovePolicies()
168+
{
169+
$e = $this->getEnforcer();
170+
$this->assertEquals([
171+
['alice', 'data1', 'read'],
172+
['bob', 'data2', 'write'],
173+
['data2_admin', 'data2', 'read'],
174+
['data2_admin', 'data2', 'write'],
175+
], $e->getPolicy());
176+
177+
$e->removePolicies([
178+
['data2_admin', 'data2', 'read'],
179+
['data2_admin', 'data2', 'write'],
180+
]);
181+
182+
$this->assertEquals([
183+
['alice', 'data1', 'read'],
184+
['bob', 'data2', 'write']
185+
], $e->getPolicy());
186+
}
187+
152188
public function testRemoveFilteredPolicy()
153189
{
154190
$e = $this->getEnforcer();

0 commit comments

Comments
 (0)