Skip to content

Commit a1fbd4a

Browse files
committed
feat: support Casbin BatchAdapter interface
feat: support Casbin BatchAdapter interface feat: support Casbin BatchAdapter interface feat: support Casbin BatchAdapter interface
1 parent c93ebda commit a1fbd4a

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

src/Adapter.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Casbin\Exceptions\CasbinException;
66
use Casbin\Persist\Adapter as AdapterContract;
7+
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
78
use Casbin\Persist\AdapterHelper;
89
use Cake\ORM\TableRegistry;
910
use CasbinAdapter\Cake\Model\Table\CasbinRuleTable;
@@ -13,7 +14,7 @@
1314
*
1415
1516
*/
16-
class Adapter implements AdapterContract
17+
class Adapter implements AdapterContract, BatchAdapterContract
1718
{
1819
use AdapterHelper;
1920

@@ -85,6 +86,54 @@ public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
8586
throw new CasbinException('not implemented');
8687
}
8788

89+
/**
90+
* Adds a policy rules to the storage.
91+
* This is part of the Auto-Save feature.
92+
*
93+
* @param string $sec
94+
* @param string $ptype
95+
* @param string[][] $rules
96+
*/
97+
public function addPolicies(string $sec, string $ptype, array $rules): void
98+
{
99+
$cols = [];
100+
$i = 0;
101+
102+
foreach ($rules as $rule) {
103+
$temp['`ptype`'] = $ptype;
104+
foreach ($rule as $key => $value) {
105+
$temp['`v'. strval($key) . '`'] = $value;
106+
}
107+
$cols[$i++] = $temp ?? [];
108+
$temp = [];
109+
}
110+
$entities = $this->table->newEntity($cols);
111+
$this->table->saveMany($entities);
112+
}
113+
114+
/**
115+
* Removes policy rules from the storage.
116+
* This is part of the Auto-Save feature.
117+
*
118+
* @param string $sec
119+
* @param string $ptype
120+
* @param string[][] $rules
121+
*/
122+
public function removePolicies(string $sec, string $ptype, array $rules): void
123+
{
124+
$this->table->getConnection()->transactional(function () use ($ptype, $rules) {
125+
foreach ($rules as $rule) {
126+
$entity = $this->table->find();
127+
128+
foreach ($rule as $key => $value) {
129+
$entity->where(['v' . strval($key) => $value]);
130+
}
131+
$entity = $entity->first();
132+
$this->table->delete($entity, ['atomic' => false]);
133+
}
134+
});
135+
}
136+
88137
protected function getTable()
89138
{
90139
return TableRegistry::getTableLocator()->get('CasbinRule', [

src/Casbin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct($config = [])
2727
$config = Configure::consume('Casbin');
2828

2929
$this->config = $this->mergeConfig(
30-
require_once dirname(__DIR__).'/config/casbin.php',
30+
require dirname(__DIR__).'/config/casbin.php',
3131
$config
3232
);
3333
if (is_string($config['adapter'])) {

tests/DatabaseAdapterTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,51 @@ public function testAddPolicy()
8686
$e->addPermissionForUser('eve', 'data3', 'read');
8787
$this->assertTrue($e->enforce('eve', 'data3', 'read'));
8888
}
89+
90+
public function testAddPolicies()
91+
{
92+
$policies = [
93+
['u1', 'd1', 'read'],
94+
['u2', 'd2', 'read'],
95+
['u3', 'd3', 'read'],
96+
];
97+
$e = $this->getEnforcer();
98+
$e->clearPolicy();
99+
$this->assertEquals([], $e->getPolicy());
100+
$e->addPolicies($policies);
101+
$this->assertEquals($policies, $e->getPolicy());
102+
}
103+
104+
public function testRemovePolicy()
105+
{
106+
$e = $this->getEnforcer();
107+
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
108+
109+
$e->addPermissionForUser('alice', 'data5', 'read');
110+
$this->assertTrue($e->enforce('alice', 'data5', 'read'));
111+
112+
$e->deletePermissionForUser('alice', 'data5', 'read');
113+
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
114+
}
115+
116+
public function testRemovePolicies()
117+
{
118+
$e = $this->getEnforcer();
119+
$this->assertEquals([
120+
['alice', 'data1', 'read'],
121+
['bob', 'data2', 'write'],
122+
['data2_admin', 'data2', 'read'],
123+
['data2_admin', 'data2', 'write'],
124+
], $e->getPolicy());
125+
126+
$e->removePolicies([
127+
['data2_admin', 'data2', 'read'],
128+
['data2_admin', 'data2', 'write'],
129+
]);
130+
131+
$this->assertEquals([
132+
['alice', 'data1', 'read'],
133+
['bob', 'data2', 'write']
134+
], $e->getPolicy());
135+
}
89136
}

0 commit comments

Comments
 (0)