Skip to content

Commit

Permalink
Merge pull request #53 from cidosx/master
Browse files Browse the repository at this point in the history
Improve DatabaseAdapter
  • Loading branch information
leeqvip committed Nov 3, 2022
2 parents 5de36c5 + 7e21d0c commit 774857a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Adapters/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
*/
public function removePolicies(string $sec, string $ptype, array $rules): void
{
DB::transaction(function () use ($sec, $rules, $ptype) {
$this->eloquent->getConnection()->transaction(function () use ($sec, $rules, $ptype) {
foreach ($rules as $rule) {
$this->removePolicy($sec, $ptype, $rule);
}
Expand Down Expand Up @@ -256,7 +256,11 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
foreach($oldRule as $k => $v) {
$instance->where('v' . $k, $v);
}
$instance->first();
$instance = $instance->first();
if (!$instance) {
return;
}

$update = [];
foreach($newPolicy as $k => $v) {
$update['v' . $k] = $v;
Expand All @@ -276,7 +280,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
*/
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
{
DB::transaction(function () use ($sec, $ptype, $oldRules, $newRules) {
$this->eloquent->getConnection()->transaction(function () use ($sec, $ptype, $oldRules, $newRules) {
foreach ($oldRules as $i => $oldRule) {
$this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
}
Expand All @@ -296,7 +300,7 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
{
$oldRules = [];
DB::transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
$this->eloquent->getConnection()->transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
$this->addPolicies($sec, $ptype, $newPolicies);
});
Expand Down

0 comments on commit 774857a

Please sign in to comment.