Skip to content

Commit 7490b7f

Browse files
authored
Merge pull request #13 from basakest/batch-adapter
feat: support Casbin BatchAdapter interface
2 parents 5987342 + fb58023 commit 7490b7f

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/Adapter.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
use yii\permission\models\CasbinRule;
66
use Casbin\Model\Model;
77
use Casbin\Persist\Adapter as AdapterContract;
8+
use Casbin\Persist\BatchAdapter as BatchAdapterContract;
89
use Casbin\Persist\AdapterHelper;
910

1011
/**
1112
* DatabaseAdapter.
1213
*
1314
1415
*/
15-
class Adapter implements AdapterContract
16+
class Adapter implements AdapterContract, BatchAdapterContract
1617
{
1718
use AdapterHelper;
1819

@@ -85,6 +86,37 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
8586
$this->savePolicyLine($ptype, $rule);
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+
$rows = [];
100+
$columns = array_keys($rules[0]);
101+
array_walk($columns, function (&$item) {
102+
$item = 'v' . strval($item);
103+
});
104+
array_unshift($columns, 'ptype');
105+
106+
foreach ($rules as $rule) {
107+
$temp['`ptype`'] = $ptype;
108+
foreach ($rule as $key => $value) {
109+
$temp['`v'. strval($key) . '`'] = $value;
110+
}
111+
$rows[] = $temp;
112+
$temp = [];
113+
}
114+
115+
$command = $this->casbinRule->getDb()->createCommand();
116+
$tableName = $this->casbinRule->tableName();
117+
$command->batchInsert($tableName, $columns, $rows)->execute();
118+
}
119+
88120
/**
89121
* This is part of the Auto-Save feature.
90122
*
@@ -104,6 +136,27 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
104136
$this->casbinRule->deleteAll($where);
105137
}
106138

139+
/**
140+
* Removes policy rules from the storage.
141+
* This is part of the Auto-Save feature.
142+
*
143+
* @param string $sec
144+
* @param string $ptype
145+
* @param string[][] $rules
146+
*/
147+
public function removePolicies(string $sec, string $ptype, array $rules): void
148+
{
149+
$transaction = $this->casbinRule->getDb()->beginTransaction();
150+
try {
151+
foreach ($rules as $rule) {
152+
$this->removePolicy($sec, $ptype, $rule);
153+
}
154+
$transaction->commit();
155+
} catch (\Exception $e) {
156+
$transaction->rollBack();
157+
}
158+
}
159+
107160
/**
108161
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
109162
* This is part of the Auto-Save feature.

tests/AdapterTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ public function testAddPolicy()
2929
$this->assertTrue(Yii::$app->permission->enforce('eve', 'data3', 'read'));
3030
}
3131

32+
public function testAddPolicies()
33+
{
34+
$policies = [
35+
['u1', 'd1', 'read'],
36+
['u2', 'd2', 'read'],
37+
['u3', 'd3', 'read'],
38+
];
39+
Yii::$app->permission->clearPolicy();
40+
$this->assertEquals([], Yii::$app->permission->getPolicy());
41+
Yii::$app->permission->addPolicies($policies);
42+
$this->assertEquals($policies, Yii::$app->permission->getPolicy());
43+
}
44+
3245
public function testSavePolicy()
3346
{
3447
$this->assertFalse(Yii::$app->permission->enforce('alice', 'data4', 'read'));
@@ -53,6 +66,26 @@ public function testRemovePolicy()
5366
$this->assertFalse(Yii::$app->permission->enforce('alice', 'data5', 'read'));
5467
}
5568

69+
public function testRemovePolicies()
70+
{
71+
$this->assertEquals([
72+
['alice', 'data1', 'read'],
73+
['bob', 'data2', 'write'],
74+
['data2_admin', 'data2', 'read'],
75+
['data2_admin', 'data2', 'write'],
76+
], Yii::$app->permission->getPolicy());
77+
78+
Yii::$app->permission->removePolicies([
79+
['data2_admin', 'data2', 'read'],
80+
['data2_admin', 'data2', 'write'],
81+
]);
82+
83+
$this->assertEquals([
84+
['alice', 'data1', 'read'],
85+
['bob', 'data2', 'write']
86+
], Yii::$app->permission->getPolicy());
87+
}
88+
5689
public function testRemoveFilteredPolicy()
5790
{
5891
$this->assertTrue(Yii::$app->permission->enforce('alice', 'data1', 'read'));

0 commit comments

Comments
 (0)