-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from basakest/UpdatableDatabaseAdapter
feat: support Casbin UpdatableAdapter interface
- Loading branch information
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
use Casbin\Persist\Adapters\Filter; | ||
use Casbin\Exceptions\InvalidFilterTypeException; | ||
use Casbin\Persist\BatchAdapter; | ||
use Casbin\Persist\UpdatableAdapter; | ||
use Closure; | ||
use Throwable; | ||
|
||
|
@@ -18,7 +19,7 @@ | |
* | ||
* @author [email protected] | ||
*/ | ||
class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter | ||
class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter, UpdatableAdapter | ||
{ | ||
use AdapterHelper; | ||
|
||
|
@@ -274,4 +275,36 @@ public function loadFilteredPolicy(Model $model, $filter): void | |
} | ||
$this->filtered = true; | ||
} | ||
|
||
/** | ||
* Updates a policy rule from storage. | ||
* This is part of the Auto-Save feature. | ||
* | ||
* @param string $sec | ||
* @param string $ptype | ||
* @param string[] $oldRule | ||
* @param string[] $newPolicy | ||
*/ | ||
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void | ||
{ | ||
$where['ptype'] = $ptype; | ||
$condition[] = 'ptype = :ptype'; | ||
|
||
foreach($oldRule as $key => $value) { | ||
$placeholder = "w" . strval($key); | ||
$where['w' . strval($key)] = $value; | ||
$condition[] = 'v' . strval($key) . ' = :' . $placeholder; | ||
} | ||
|
||
$update = []; | ||
foreach($newPolicy as $key => $value) { | ||
$placeholder = "s" . strval($key); | ||
$updateValue["$placeholder"] = $value; | ||
$update[] = 'v' . strval($key) . ' = :' . $placeholder; | ||
} | ||
|
||
$sql = "UPDATE {$this->casbinRuleTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition); | ||
|
||
$this->connection->execute($sql, array_merge($updateValue, $where)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters