-
Notifications
You must be signed in to change notification settings - Fork 9
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 #15 from basakest/updatable-adapter
feat: Support Casbin UpdatableAdapter interface
- Loading branch information
Showing
3 changed files
with
311 additions
and
8 deletions.
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
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Casbin\Persist\Adapter as AdapterContract; | ||
use Casbin\Persist\BatchAdapter as BatchAdapterContract; | ||
use Casbin\Persist\FilteredAdapter as FilteredAdapterContract; | ||
use Casbin\Persist\UpdatableAdapter as UpdatableAdapterContract; | ||
use Casbin\Persist\AdapterHelper; | ||
use Casbin\Persist\Adapters\Filter; | ||
use Casbin\Exceptions\InvalidFilterTypeException; | ||
|
@@ -16,7 +17,7 @@ | |
* | ||
* @author [email protected] | ||
*/ | ||
class Adapter implements AdapterContract, BatchAdapterContract, FilteredAdapterContract | ||
class Adapter implements AdapterContract, BatchAdapterContract, FilteredAdapterContract, UpdatableAdapterContract | ||
{ | ||
use AdapterHelper; | ||
|
||
|
@@ -162,19 +163,19 @@ public function removePolicies(string $sec, string $ptype, array $rules): void | |
$transaction->commit(); | ||
} catch (\Exception $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* RemoveFilteredPolicy removes policy rules that match the filter from the storage. | ||
* This is part of the Auto-Save feature. | ||
* | ||
* @param string $sec | ||
* @param string $ptype | ||
* @param int $fieldIndex | ||
* @param string ...$fieldValues | ||
* @param int $fieldIndex | ||
* @param string|null ...$fieldValues | ||
* @return array | ||
* @throws Throwable | ||
*/ | ||
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void | ||
public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, ?string ...$fieldValues): array | ||
{ | ||
$where = []; | ||
$where['ptype'] = $ptype; | ||
|
@@ -187,7 +188,31 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex | |
} | ||
} | ||
|
||
$removedRules = $this->casbinRule->find()->where($where)->all(); | ||
$this->casbinRule->deleteAll($where); | ||
|
||
array_walk($removedRules, function (&$removedRule) { | ||
unset($removedRule->id); | ||
unset($removedRule->ptype); | ||
$removedRule = $removedRule->toArray(); | ||
$removedRule = $this->filterRule($removedRule); | ||
}); | ||
|
||
return $removedRules; | ||
} | ||
|
||
/** | ||
* RemoveFilteredPolicy removes policy rules that match the filter from the storage. | ||
* This is part of the Auto-Save feature. | ||
* | ||
* @param string $sec | ||
* @param string $ptype | ||
* @param int $fieldIndex | ||
* @param string ...$fieldValues | ||
*/ | ||
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void | ||
{ | ||
$this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues); | ||
} | ||
|
||
/** | ||
|
@@ -226,6 +251,100 @@ public function loadFilteredPolicy(Model $model, $filter): void | |
$this->setFiltered(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 | ||
{ | ||
$entity = clone $this->casbinRule; | ||
|
||
$condition['ptype'] = $ptype; | ||
foreach ($oldRule as $k => $v) { | ||
$condition['v' . $k] = $v; | ||
} | ||
$item = $entity->findOne($condition); | ||
foreach ($newPolicy as $k => $v) { | ||
$key = 'v' . $k; | ||
$item->$key = $v; | ||
} | ||
$item->update(); | ||
} | ||
|
||
/** | ||
* UpdatePolicies updates some policy rules to storage, like db, redis. | ||
* | ||
* @param string $sec | ||
* @param string $ptype | ||
* @param string[][] $oldRules | ||
* @param string[][] $newRules | ||
* @return void | ||
*/ | ||
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void | ||
{ | ||
$transaction = $this->casbinRule->getDb()->beginTransaction(); | ||
try { | ||
foreach ($oldRules as $i => $oldRule) { | ||
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]); | ||
} | ||
$transaction->commit(); | ||
} catch (\Exception $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* UpdateFilteredPolicies deletes old rules and adds new rules. | ||
* | ||
* @param string $sec | ||
* @param string $ptype | ||
* @param array $newPolicies | ||
* @param integer $fieldIndex | ||
* @param string ...$fieldValues | ||
* @return array | ||
*/ | ||
public function updateFilteredPolicies(string $sec, string $ptype, array $newRules, int $fieldIndex, ?string ...$fieldValues): array | ||
{ | ||
$oldRules = []; | ||
$transaction = $this->casbinRule->getDb()->beginTransaction(); | ||
try { | ||
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues); | ||
$this->addPolicies($sec, $ptype, $newRules); | ||
$transaction->commit(); | ||
} catch (\Exception $e) { | ||
$transaction->rollBack(); | ||
throw $e; | ||
} | ||
|
||
return $oldRules; | ||
} | ||
|
||
/** | ||
* Filter the rule. | ||
* | ||
* @param array $rule | ||
* @return array | ||
*/ | ||
public function filterRule(array $rule): array | ||
{ | ||
$rule = array_values($rule); | ||
|
||
$i = count($rule) - 1; | ||
for (; $i >= 0; $i--) { | ||
if ($rule[$i] != "" && !is_null($rule[$i])) { | ||
break; | ||
} | ||
} | ||
|
||
return array_slice($rule, 0, $i + 1); | ||
} | ||
|
||
/** | ||
* Returns true if the loaded policy has been filtered. | ||
* | ||
|
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