Skip to content

Commit 77f644d

Browse files
authored
Merge pull request #8 from basakest/rewrite-update-filtered-policies
refactor: Rewrite updateFilteredPolicies method
2 parents dea79cb + ddab7a0 commit 77f644d

File tree

2 files changed

+63
-49
lines changed

2 files changed

+63
-49
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ jobs:
108108
- uses: actions/checkout@v2
109109
- uses: actions/setup-node@v1
110110
with:
111-
node-version: '12'
111+
node-version: '14.17'
112112

113113
- name: Run semantic-release
114114
env:

src/Adapter.php

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public function initTable()
104104
]);
105105
}
106106

107+
/**
108+
* Filter the rule.
109+
*
110+
* @param array $rule
111+
* @return array
112+
*/
113+
public function filterRule(array $rule): array
114+
{
115+
$rule = array_values($rule);
116+
117+
$i = count($rule) - 1;
118+
for (; $i >= 0; $i--) {
119+
if ($rule[$i] != '' && !is_null($rule[$i])) {
120+
break;
121+
}
122+
}
123+
124+
return array_slice($rule, 0, $i + 1);
125+
}
126+
107127
/**
108128
* savePolicyLine function.
109129
*
@@ -238,29 +258,53 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
238258
}
239259

240260
/**
241-
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
242-
* This is part of the Auto-Save feature.
243-
*
244261
* @param string $sec
245262
* @param string $ptype
246263
* @param int $fieldIndex
247-
* @param string ...$fieldValues
248-
*
249-
* @return void
264+
* @param string|null ...$fieldValues
265+
* @return array
266+
* @throws Throwable
250267
*/
251-
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
268+
public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, ?string ...$fieldValues): array
252269
{
270+
$removedRules = [];
271+
$columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
253272
$where['ptype'] = $ptype;
254273

255274
foreach (range(0, 5) as $value) {
256275
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
257-
if ('' != $val = $fieldValues[$value - $fieldIndex]) {
258-
$where['v'.strval($value)] = $val;
276+
if ('' !== $val = $fieldValues[$value - $fieldIndex]) {
277+
$where['v' . strval($value)] = $val;
259278
}
260279
}
261280
}
262281

282+
$removedRules = [];
283+
$rows = $this->database->select($this->casbinRuleTableName, $columns, $where);
284+
foreach ($rows as $row) {
285+
unset($row['ptype']);
286+
$removedRules[] = $this->filterRule($row);
287+
}
263288
$this->database->delete($this->casbinRuleTableName, ['AND' => $where]);
289+
290+
return $removedRules;
291+
}
292+
293+
/**
294+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
295+
* This is part of the Auto-Save feature.
296+
*
297+
* @param string $sec
298+
* @param string $ptype
299+
* @param int $fieldIndex
300+
* @param string ...$fieldValues
301+
* @throws Exception|Throwable
302+
*
303+
* @return void
304+
*/
305+
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
306+
{
307+
$this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
264308
}
265309

266310
/**
@@ -307,52 +351,22 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
307351
}
308352

309353
/**
310-
* UpdateFilteredPolicies deletes old rules and adds new rules.
311-
*
312354
* @param string $sec
313355
* @param string $ptype
314-
* @param array $newPolicies
315-
* @param integer $fieldIndex
356+
* @param array $newRules
357+
* @param int $fieldIndex
316358
* @param string ...$fieldValues
317359
* @return array
360+
* @throws Throwable
318361
*/
319-
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
362+
public function updateFilteredPolicies(string $sec, string $ptype, array $newRules, int $fieldIndex, ?string ...$fieldValues): array
320363
{
321-
$where['ptype'] = $ptype;
322-
foreach ($fieldValues as $fieldValue) {
323-
$suffix = $fieldIndex++;
324-
if (!is_null($fieldValue) && $fieldValue !== '') {
325-
$where['v'. $suffix] = $fieldValue;
326-
}
327-
}
328-
329-
$newP = [];
330-
$oldP = [];
331-
foreach ($newPolicies as $newRule) {
332-
$col['ptype'] = $ptype;
333-
foreach ($newRule as $key => $value) {
334-
$col['v' . strval($key)] = $value;
335-
}
336-
$newP[] = $col;
337-
}
338-
339-
$this->database->action(function () use ($newP, $where, &$oldP) {
340-
$columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
341-
$oldP = $this->database->select($this->casbinRuleTableName, $columns, $where);
342-
343-
foreach ($oldP as &$item) {
344-
$item = array_filter($item, function ($value) {
345-
return !is_null($value) && $value !== '';
346-
});
347-
unset($item['ptype']);
348-
}
349-
350-
$this->database->delete($this->casbinRuleTableName, ['AND' => $where]);
351-
$this->database->insert($this->casbinRuleTableName, $newP);
364+
$oldRules = [];
365+
$this->database->action(function () use ($sec, $ptype, $newRules, $fieldIndex, $fieldValues, &$oldRules) {
366+
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
367+
$this->addPolicies($sec, $ptype, $newRules);
352368
});
353-
354-
// return deleted rules
355-
return $oldP;
369+
return $oldRules;
356370
}
357371

358372
/**

0 commit comments

Comments
 (0)