Skip to content

Commit bb56632

Browse files
authored
Merge pull request #12 from basakest/UpdatableDatabaseAdapter
feat: support Casbin UpdatableAdapter interface
2 parents ad06ec5 + 64d576c commit bb56632

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/Adapters/DatabaseAdapter.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Lauthz\Models\Rule;
88
use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract;
99
use Lauthz\Contracts\BatchDatabaseAdapter as BatchDatabaseAdapterContract;
10+
use Lauthz\Contracts\UpdatableDatabaseAdapter as UpdatableDatabaseAdapterContract;
1011
use Casbin\Model\Model;
1112
use Casbin\Persist\AdapterHelper;
1213
use DateTime;
@@ -15,7 +16,7 @@
1516
*
1617
1718
*/
18-
class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract
19+
class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract, UpdatableDatabaseAdapterContract
1920
{
2021
use AdapterHelper;
2122

@@ -207,4 +208,28 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
207208
}
208209
}
209210
}
211+
212+
/**
213+
* Updates a policy rule from storage.
214+
* This is part of the Auto-Save feature.
215+
*
216+
* @param string $sec
217+
* @param string $ptype
218+
* @param string[] $oldRule
219+
* @param string[] $newPolicy
220+
*/
221+
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
222+
{
223+
$instance = $this->eloquent->where('p_type', $ptype);
224+
foreach($oldRule as $k => $v) {
225+
$instance->where('v' . $k, $v);
226+
}
227+
$instance->first();
228+
$update = [];
229+
foreach($newPolicy as $k => $v) {
230+
$item = 'v' . $k;
231+
$update[$item] = $k;
232+
}
233+
$instance->update($update);
234+
}
210235
}
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\UpdatableAdapter;
6+
7+
interface UpdatableDatabaseAdapter extends UpdatableAdapter
8+
{
9+
}

tests/DatabaseAdapterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,31 @@ public function testRemoveFilteredPolicy()
101101
$this->assertFalse(Enforcer::enforce('bob', 'data2', 'write'));
102102
$this->assertFalse(Enforcer::enforce('alice', 'data2', 'write'));
103103
}
104+
105+
public function testUpdatePolicy()
106+
{
107+
$this->assertEquals([
108+
['alice', 'data1', 'read'],
109+
['bob', 'data2', 'write'],
110+
['data2_admin', 'data2', 'read'],
111+
['data2_admin', 'data2', 'write'],
112+
], Enforcer::getPolicy());
113+
114+
Enforcer::updatePolicy(
115+
['alice', 'data1', 'read'],
116+
['alice', 'data1', 'write']
117+
);
118+
119+
Enforcer::updatePolicy(
120+
['bob', 'data2', 'write'],
121+
['bob', 'data2', 'read']
122+
);
123+
124+
$this->assertEquals([
125+
['alice', 'data1', 'write'],
126+
['bob', 'data2', 'read'],
127+
['data2_admin', 'data2', 'read'],
128+
['data2_admin', 'data2', 'write'],
129+
], Enforcer::getPolicy());
130+
}
104131
}

0 commit comments

Comments
 (0)