Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ jobs:
needs: [ test, upload-coverage ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: '12'
node-version: 'lts/*'

- name: Run semantic-release
env:
Expand Down
47 changes: 23 additions & 24 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ public function loadPolicy(Model $model): void
$rows = $this->connection->query('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName.'');

foreach ($rows as $row) {
$line = implode(', ', array_filter($row, function ($val) {
return '' != $val && !is_null($val);
}));
$this->loadPolicyLine(trim($line), $model);
$this->loadPolicyArray($this->filterRule($row), $model);
}
}

Expand Down Expand Up @@ -183,11 +180,11 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
{
$this->connection->getPdo()->beginTransaction();
try {
foreach($rules as $rule) {
foreach ($rules as $rule) {
$this->removePolicy($sec, $ptype, $rule);
}
$this->connection->getPdo()->commit();
} catch (Throwable $e){
} catch (Throwable $e) {
$this->connection->getPdo()->rollback();
throw $e;
}
Expand Down Expand Up @@ -252,16 +249,16 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
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
*/
/**
* 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
*/
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
{
$this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
Expand All @@ -288,14 +285,14 @@ public function loadFilteredPolicy(Model $model, $filter): void
$filter = explode('=', $filter);
$sql .= "$filter[0] = :{$filter[0]}";
$bind[$filter[0]] = $filter[1];
} else if ($filter instanceof Filter) {
foreach($filter->p as $k => $v) {
} elseif ($filter instanceof Filter) {
foreach ($filter->p as $k => $v) {
$where[] = $v . ' = :' . $v;
$bind[$v] = $filter->g[$k];
}
$where = implode(' AND ', $where);
$sql .= $where;
} else if ($filter instanceof Closure) {
} elseif ($filter instanceof Closure) {
$where = '';
$filter($where);
$where = str_replace(' ', '', $where);
Expand All @@ -308,8 +305,10 @@ public function loadFilteredPolicy(Model $model, $filter): void
}

$rows = $this->connection->query($sql, $bind);
foreach($rows as $row) {
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
foreach ($rows as $row) {
$row = array_filter($row, function ($value) {
return !is_null($value) && $value !== '';
});
unset($row['id']);
$line = implode(', ', array_filter($row, function ($val) {
return '' != $val && !is_null($val);
Expand All @@ -334,21 +333,21 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
$where['ptype'] = $ptype;
$condition[] = 'ptype = :ptype';

foreach($oldRule as $key => $value) {
foreach ($oldRule as $key => $value) {
$placeholder = "w" . strval($key);
$where['w' . strval($key)] = $value;
$condition[] = 'v' . strval($key) . ' = :' . $placeholder;
}

$update = [];
foreach($newPolicy as $key => $value) {
foreach ($newPolicy as $key => $value) {
$placeholder = "s" . strval($key);
$updateValue["$placeholder"] = $value;
$update[] = 'v' . strval($key) . ' = :' . $placeholder;
}

$sql = "UPDATE {$this->casbinRuleTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition);

$this->connection->execute($sql, array_merge($updateValue, $where));
}

Expand Down