Skip to content

Commit ad06ec5

Browse files
authored
Merge pull request #11 from basakest/BatchAdapter
feat: Support Casbin BatchAdapter interface
2 parents 48ff59e + b05630b commit ad06ec5

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

src/Adapters/DatabaseAdapter.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
use Lauthz\Models\Rule;
88
use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract;
9+
use Lauthz\Contracts\BatchDatabaseAdapter as BatchDatabaseAdapterContract;
910
use Casbin\Model\Model;
1011
use Casbin\Persist\AdapterHelper;
11-
12+
use DateTime;
1213
/**
1314
* DatabaseAdapter.
1415
*
1516
1617
*/
17-
class DatabaseAdapter implements DatabaseAdapterContract
18+
class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract
1819
{
1920
use AdapterHelper;
2021

@@ -101,6 +102,32 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
101102
$this->savePolicyLine($ptype, $rule);
102103
}
103104

105+
/**
106+
* Adds a policy rules to the storage.
107+
* This is part of the Auto-Save feature.
108+
*
109+
* @param string $sec
110+
* @param string $ptype
111+
* @param string[][] $rules
112+
*/
113+
public function addPolicies(string $sec, string $ptype, array $rules): void
114+
{
115+
$cols = [];
116+
$i = 0;
117+
118+
foreach($rules as $rule) {
119+
$temp['p_type'] = $ptype;
120+
$temp['created_at'] = new DateTime();
121+
$temp['updated_at'] = $temp['created_at'];
122+
foreach ($rule as $key => $value) {
123+
$temp['v'.strval($key)] = $value;
124+
}
125+
$cols[$i++] = $temp ?? [];
126+
$temp = [];
127+
}
128+
$this->eloquent->insert($cols);
129+
}
130+
104131
/**
105132
* This is part of the Auto-Save feature.
106133
*
@@ -125,6 +152,33 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
125152
}
126153
}
127154

155+
/**
156+
* Removes policy rules from the storage.
157+
* This is part of the Auto-Save feature.
158+
*
159+
* @param string $sec
160+
* @param string $ptype
161+
* @param string[][] $rules
162+
*/
163+
public function removePolicies(string $sec, string $ptype, array $rules): void
164+
{
165+
$count = 0;
166+
$instance = $this->eloquent->where('p_type', $ptype);
167+
foreach($rules as $rule)
168+
{
169+
foreach ($rule as $key => $value) {
170+
$keys[] = 'v'.strval($key);
171+
$con['v'.strval($key)][] = $value;
172+
}
173+
}
174+
$keys = array_unique($keys);
175+
foreach($keys as $key){
176+
$instance->whereIn($key, $con[$key]);
177+
}
178+
$num = $instance->delete();
179+
$count += $num;
180+
}
181+
128182
/**
129183
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
130184
* This is part of the Auto-Save feature.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Lauthz\Contracts;
4+
5+
use Casbin\Persist\BatchAdapter;
6+
7+
interface BatchDatabaseAdapter extends BatchAdapter
8+
{
9+
}

tests/DatabaseAdapterTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ public function testAddPolicy()
2727
$this->assertTrue(Enforcer::enforce('eve', 'data3', 'read'));
2828
}
2929

30+
public function testAddPolicies()
31+
{
32+
$policies = [
33+
['u1', 'd1', 'read'],
34+
['u2', 'd2', 'read'],
35+
['u3', 'd3', 'read'],
36+
];
37+
Enforcer::clearPolicy();
38+
$this->initTable();
39+
$this->assertEquals([], Enforcer::getPolicy());
40+
Enforcer::addPolicies($policies);
41+
$this->assertEquals($policies, Enforcer::getPolicy());
42+
}
43+
3044
public function testSavePolicy()
3145
{
3246
$this->assertFalse(Enforcer::enforce('alice', 'data4', 'read'));
@@ -51,6 +65,26 @@ public function testRemovePolicy()
5165
$this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
5266
}
5367

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

0 commit comments

Comments
 (0)