Skip to content

Commit ad88626

Browse files
committed
LaravelRuleModel $model
1 parent ace01f7 commit ad88626

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

src/Adapter/DatabaseAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
230230
$item = $model->hidden(['id', 'ptype'])->toArray();
231231
$item = $this->filterRule($item);
232232
$removedRules[] = $item;
233-
if ($model->cache('tauthz')->delete()) {
233+
if ($model->delete()) {
234234
++$count;
235235
}
236236
}

src/Adapter/LaravelDatabaseAdapter.php

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
use Casbin\Persist\Adapters\Filter;
2020
use Casbin\Exceptions\InvalidFilterTypeException;
2121
use Casbin\WebmanPermission\Model\LaravelRuleModel;
22-
use Casbin\WebmanPermission\Model\RuleModel;
2322
use Illuminate\Support\Facades\DB;
23+
use Throwable;
2424

2525
/**
2626
* DatabaseAdapter.
@@ -41,18 +41,37 @@ class LaravelDatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter,
4141
*
4242
* @var LaravelRuleModel
4343
*/
44-
protected $model;
44+
protected LaravelRuleModel $model;
4545

4646
/**
47-
* the DatabaseAdapter constructor.
48-
*
49-
* @param RuleModel $model
47+
* LaravelDatabaseAdapter constructor.
48+
* @param LaravelRuleModel $model
5049
*/
51-
public function __construct(RuleModel $model)
50+
public function __construct(LaravelRuleModel $model)
5251
{
5352
$this->model = $model;
5453
}
5554

55+
/**
56+
* Filter the rule.
57+
*
58+
* @param array $rule
59+
* @return array
60+
*/
61+
public function filterRule(array $rule): array
62+
{
63+
$rule = array_values($rule);
64+
65+
$i = count($rule) - 1;
66+
for (; $i >= 0; $i--) {
67+
if ($rule[$i] != '' && !is_null($rule[$i])) {
68+
break;
69+
}
70+
}
71+
72+
return array_slice($rule, 0, $i + 1);
73+
}
74+
5675
/**
5776
* savePolicyLine function.
5877
*
@@ -163,6 +182,40 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
163182
LaravelRuleModel::fireModelEvent('deleted');
164183
}
165184

185+
/**
186+
* @param string $sec
187+
* @param string $ptype
188+
* @param int $fieldIndex
189+
* @param string|null ...$fieldValues
190+
* @return array
191+
* @throws Throwable
192+
*/
193+
public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, ?string ...$fieldValues): array
194+
{
195+
$count = 0;
196+
$removedRules = [];
197+
198+
$instance = $this->model->where('ptype', $ptype);
199+
foreach (range(0, 5) as $value) {
200+
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
201+
if ('' != $fieldValues[$value - $fieldIndex]) {
202+
$instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
203+
}
204+
}
205+
}
206+
207+
foreach ($instance->select() as $model) {
208+
$item = $model->hidden(['id', 'ptype'])->toArray();
209+
$item = $this->filterRule($item);
210+
$removedRules[] = $item;
211+
if ($model->cache('tauthz')->delete()) {
212+
++$count;
213+
}
214+
}
215+
216+
return $removedRules;
217+
}
218+
166219
/**
167220
* Removes policy rules from the storage.
168221
* This is part of the Auto-Save feature.
@@ -315,12 +368,14 @@ public function loadFilteredPolicy(Model $model, $filter): void
315368
throw new InvalidFilterTypeException('invalid filter type');
316369
}
317370
$rows = $instance->get()->makeHidden(['created_at','updated_at', 'id'])->toArray();
318-
foreach ($rows as $row) {
319-
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
320-
$line = implode(', ', array_filter($row, function ($val) {
321-
return '' != $val && !is_null($val);
322-
}));
323-
$this->loadPolicyLine(trim($line), $model);
371+
if ($rows) {
372+
foreach ($rows as $row) {
373+
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
374+
$line = implode(', ', array_filter($row, function ($val) {
375+
return '' != $val && !is_null($val);
376+
}));
377+
$this->loadPolicyLine(trim($line), $model);
378+
}
324379
}
325380
$this->setFiltered(true);
326381
}

0 commit comments

Comments
 (0)