From 291b3826d785122b79b36d841d0f49eeb963fd55 Mon Sep 17 00:00:00 2001 From: Jon Lee Date: Tue, 7 Dec 2021 23:39:53 +0800 Subject: [PATCH 1/2] fix: Use loadPolicyArray instead of loadPolicyLine(#19) --- src/Adapter.php | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Adapter.php b/src/Adapter.php index b88d7fd..9b1b7e7 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -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); } } @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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)); } From 723c82634bbd18780095ff0a6751db2f558fe499 Mon Sep 17 00:00:00 2001 From: Jon Lee Date: Tue, 7 Dec 2021 23:45:14 +0800 Subject: [PATCH 2/2] ci: update node version --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1a85b82..1c12be6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: