Skip to content

Commit

Permalink
refactor: Rewrite updateFilteredPolicies method (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest authored Nov 17, 2021
1 parent 71b7ff5 commit 03492ae
Showing 1 changed file with 53 additions and 42 deletions.
95 changes: 53 additions & 42 deletions src/adapter/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ public function __construct(Rule $model)
$this->model = $model;
}

/**
* 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 @@ -176,32 +196,51 @@ 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
* @param string $sec
* @param string $ptype
* @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
{
$count = 0;
$removedRules = [];

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

foreach ($instance->select() as $model) {
$item = $model->hidden(['id', 'ptype'])->toArray();
$item = $this->filterRule($item);
$removedRules[] = $item;
if ($model->cache('tauthz')->delete()) {
++$count;
}
}

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

/**
Expand Down Expand Up @@ -259,41 +298,13 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
*/
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, 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;
}

DB::transaction(function () use ($newP, $where, &$oldP) {
$oldRules = $this->model->where($where);
$oldP = $oldRules->select()->hidden(['id'])->toArray();

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

$oldRules->delete();
$this->model->insertAll($newP);
$oldRules = [];
DB::transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
$this->addPolicies($sec, $ptype, $newPolicies);
});

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

/**
Expand Down

0 comments on commit 03492ae

Please sign in to comment.