Skip to content

Commit 3a91ed9

Browse files
authored
fix: Use loadPolicyArray instead of loadPolicyLine(#19) (#20)
1 parent e08aadd commit 3a91ed9

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/Adapter.php

+23-24
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ public function loadPolicy(Model $model): void
119119
$rows = $this->connection->query('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName.'');
120120

121121
foreach ($rows as $row) {
122-
$line = implode(', ', array_filter($row, function ($val) {
123-
return '' != $val && !is_null($val);
124-
}));
125-
$this->loadPolicyLine(trim($line), $model);
122+
$this->loadPolicyArray($this->filterRule($row), $model);
126123
}
127124
}
128125

@@ -183,11 +180,11 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
183180
{
184181
$this->connection->getPdo()->beginTransaction();
185182
try {
186-
foreach($rules as $rule) {
183+
foreach ($rules as $rule) {
187184
$this->removePolicy($sec, $ptype, $rule);
188185
}
189186
$this->connection->getPdo()->commit();
190-
} catch (Throwable $e){
187+
} catch (Throwable $e) {
191188
$this->connection->getPdo()->rollback();
192189
throw $e;
193190
}
@@ -252,16 +249,16 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
252249
return $removedRules;
253250
}
254251

255-
/**
256-
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
257-
* This is part of the Auto-Save feature.
258-
*
259-
* @param string $sec
260-
* @param string $ptype
261-
* @param int $fieldIndex
262-
* @param string ...$fieldValues
263-
* @throws Exception|Throwable
264-
*/
252+
/**
253+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
254+
* This is part of the Auto-Save feature.
255+
*
256+
* @param string $sec
257+
* @param string $ptype
258+
* @param int $fieldIndex
259+
* @param string ...$fieldValues
260+
* @throws Exception|Throwable
261+
*/
265262
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
266263
{
267264
$this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
@@ -288,14 +285,14 @@ public function loadFilteredPolicy(Model $model, $filter): void
288285
$filter = explode('=', $filter);
289286
$sql .= "$filter[0] = :{$filter[0]}";
290287
$bind[$filter[0]] = $filter[1];
291-
} else if ($filter instanceof Filter) {
292-
foreach($filter->p as $k => $v) {
288+
} elseif ($filter instanceof Filter) {
289+
foreach ($filter->p as $k => $v) {
293290
$where[] = $v . ' = :' . $v;
294291
$bind[$v] = $filter->g[$k];
295292
}
296293
$where = implode(' AND ', $where);
297294
$sql .= $where;
298-
} else if ($filter instanceof Closure) {
295+
} elseif ($filter instanceof Closure) {
299296
$where = '';
300297
$filter($where);
301298
$where = str_replace(' ', '', $where);
@@ -308,8 +305,10 @@ public function loadFilteredPolicy(Model $model, $filter): void
308305
}
309306

310307
$rows = $this->connection->query($sql, $bind);
311-
foreach($rows as $row) {
312-
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
308+
foreach ($rows as $row) {
309+
$row = array_filter($row, function ($value) {
310+
return !is_null($value) && $value !== '';
311+
});
313312
unset($row['id']);
314313
$line = implode(', ', array_filter($row, function ($val) {
315314
return '' != $val && !is_null($val);
@@ -334,21 +333,21 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
334333
$where['ptype'] = $ptype;
335334
$condition[] = 'ptype = :ptype';
336335

337-
foreach($oldRule as $key => $value) {
336+
foreach ($oldRule as $key => $value) {
338337
$placeholder = "w" . strval($key);
339338
$where['w' . strval($key)] = $value;
340339
$condition[] = 'v' . strval($key) . ' = :' . $placeholder;
341340
}
342341

343342
$update = [];
344-
foreach($newPolicy as $key => $value) {
343+
foreach ($newPolicy as $key => $value) {
345344
$placeholder = "s" . strval($key);
346345
$updateValue["$placeholder"] = $value;
347346
$update[] = 'v' . strval($key) . ' = :' . $placeholder;
348347
}
349348

350349
$sql = "UPDATE {$this->casbinRuleTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition);
351-
350+
352351
$this->connection->execute($sql, array_merge($updateValue, $where));
353352
}
354353

0 commit comments

Comments
 (0)