Skip to content

Commit ca6f570

Browse files
committed
feat: support Casbin BatchAdapter interface
fix: refactor the logic
1 parent 05d2c3c commit ca6f570

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/Adapters/DatabaseAdapter.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
use EasySwoole\ORM\Exception\Exception;
1212
use EasySwoole\Permission\Model\RulesModel;
1313
use Throwable;
14+
use Casbin\Persist\BatchAdapter;
1415

15-
class DatabaseAdapter implements Adapter
16+
class DatabaseAdapter implements Adapter, BatchAdapter
1617
{
1718
use AdapterHelper;
1819

@@ -145,4 +146,53 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
145146

146147
$instance->destroy();
147148
}
149+
150+
/**
151+
* adds a policy rules to the storage.
152+
* This is part of the Auto-Save feature.
153+
*
154+
* @param string $sec
155+
* @param string $ptype
156+
* @param array $rules
157+
*/
158+
public function addPolicies(string $sec, string $ptype, array $rules): void
159+
{
160+
$cols = [];
161+
162+
foreach($rules as $rule) {
163+
$temp = [];
164+
$temp['ptype'] = $ptype;
165+
foreach ($rule as $key => $value) {
166+
$temp['v'.strval($key)] = $value;
167+
}
168+
$cols[] = $temp;
169+
}
170+
171+
RulesModel::create()->saveAll($cols);
172+
}
173+
174+
/**
175+
* removes policy rules from the storage.
176+
* This is part of the Auto-Save feature.
177+
*
178+
* @param string $sec
179+
* @param string $ptype
180+
* @param array $rules
181+
*/
182+
public function removePolicies(string $sec, string $ptype, array $rules): void
183+
{
184+
$ids = [];
185+
186+
foreach($rules as $rule) {
187+
$where = [];
188+
$where['ptype'] = $ptype;
189+
foreach ($rule as $key => $value) {
190+
$where['v'.strval($key)] = $value;
191+
}
192+
$ret = RulesModel::create()->get($where);
193+
$ret && $ids[] = $ret->id;
194+
}
195+
196+
RulesModel::create()->destroy($ids);
197+
}
148198
}

tests/DatabaseAdapterTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,39 @@ public function testSavePolicy()
108108
$adapter->savePolicy($model);
109109
$this->assertTrue($e->enforce('alice', 'data4', 'read'));
110110
}
111+
112+
public function testAddPolicies()
113+
{
114+
$policies = [
115+
['u1', 'd1', 'read'],
116+
['u2', 'd2', 'read'],
117+
['u3', 'd3', 'read'],
118+
];
119+
$e = $this->getEnforcer();
120+
$e->clearPolicy();
121+
$this->assertEquals([], $e->getPolicy());
122+
$e->addPolicies($policies);
123+
$this->assertEquals($policies, $e->getPolicy());
124+
}
125+
126+
public function testRemovePolicies()
127+
{
128+
$e = $this->getEnforcer();
129+
$this->assertEquals([
130+
['alice', 'data1', 'read'],
131+
['bob', 'data2', 'write'],
132+
['data2_admin', 'data2', 'read'],
133+
['data2_admin', 'data2', 'write'],
134+
], $e->getPolicy());
135+
136+
$e->removePolicies([
137+
['data2_admin', 'data2', 'read'],
138+
['data2_admin', 'data2', 'write'],
139+
]);
140+
141+
$this->assertEquals([
142+
['alice', 'data1', 'read'],
143+
['bob', 'data2', 'write']
144+
], $e->getPolicy());
145+
}
111146
}

0 commit comments

Comments
 (0)