Skip to content

Commit

Permalink
refactor: Rewrite updateFilteredPolicies method
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest committed Sep 29, 2021
1 parent dea79cb commit ddab7a0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -108,7 +108,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12'
node-version: '14.17'

- name: Run semantic-release
env:
Expand Down
110 changes: 62 additions & 48 deletions src/Adapter.php
Expand Up @@ -104,6 +104,26 @@ public function initTable()
]);
}

/**
* 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);
}

/**
* savePolicyLine function.
*
Expand Down Expand Up @@ -238,29 +258,53 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
}

/**
* 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
*
* @return void
* @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
{
$removedRules = [];
$columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
$where['ptype'] = $ptype;

foreach (range(0, 5) as $value) {
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
if ('' != $val = $fieldValues[$value - $fieldIndex]) {
$where['v'.strval($value)] = $val;
if ('' !== $val = $fieldValues[$value - $fieldIndex]) {
$where['v' . strval($value)] = $val;
}
}
}

$removedRules = [];
$rows = $this->database->select($this->casbinRuleTableName, $columns, $where);
foreach ($rows as $row) {
unset($row['ptype']);
$removedRules[] = $this->filterRule($row);
}
$this->database->delete($this->casbinRuleTableName, ['AND' => $where]);

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
* @throws Exception|Throwable
*
* @return void
*/
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
{
$this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
}

/**
Expand Down Expand Up @@ -307,52 +351,22 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
}

/**
* UpdateFilteredPolicies deletes old rules and adds new rules.
*
* @param string $sec
* @param string $ptype
* @param array $newPolicies
* @param integer $fieldIndex
* @param array $newRules
* @param int $fieldIndex
* @param string ...$fieldValues
* @return array
* @throws Throwable
*/
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
public function updateFilteredPolicies(string $sec, string $ptype, array $newRules, int $fieldIndex, ?string ...$fieldValues): array
{
$where['ptype'] = $ptype;
foreach ($fieldValues as $fieldValue) {
$suffix = $fieldIndex++;
if (!is_null($fieldValue) && $fieldValue !== '') {
$where['v'. $suffix] = $fieldValue;
}
}

$newP = [];
$oldP = [];
foreach ($newPolicies as $newRule) {
$col['ptype'] = $ptype;
foreach ($newRule as $key => $value) {
$col['v' . strval($key)] = $value;
}
$newP[] = $col;
}

$this->database->action(function () use ($newP, $where, &$oldP) {
$columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
$oldP = $this->database->select($this->casbinRuleTableName, $columns, $where);

foreach ($oldP as &$item) {
$item = array_filter($item, function ($value) {
return !is_null($value) && $value !== '';
});
unset($item['ptype']);
}

$this->database->delete($this->casbinRuleTableName, ['AND' => $where]);
$this->database->insert($this->casbinRuleTableName, $newP);
$oldRules = [];
$this->database->action(function () use ($sec, $ptype, $newRules, $fieldIndex, $fieldValues, &$oldRules) {
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
$this->addPolicies($sec, $ptype, $newRules);
});

// return deleted rules
return $oldP;
return $oldRules;
}

/**
Expand Down

0 comments on commit ddab7a0

Please sign in to comment.