From 4485bc03488a05f40aacfdc9935f25aa22f55479 Mon Sep 17 00:00:00 2001 From: AlexzPurewoko Date: Thu, 28 Dec 2023 09:41:56 +0700 Subject: [PATCH] passing test when have inverted rule with whole action --- .../Core/Comparator/AbilityCheckerImpl.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Abilities/Core/Comparator/AbilityCheckerImpl.php b/src/Abilities/Core/Comparator/AbilityCheckerImpl.php index a251403..34532ef 100644 --- a/src/Abilities/Core/Comparator/AbilityCheckerImpl.php +++ b/src/Abilities/Core/Comparator/AbilityCheckerImpl.php @@ -18,22 +18,22 @@ public function __construct( */ public function can(string $action, string $resource, string $scope, mixed $field = null): bool { - $specificActionRules = $this->compiledRules->queryRule($scope, $resource, $action); - $specificNormalRules = []; + $unspecifiedActionRules = $this->compiledRules->queryRule($scope, $resource, ''); - foreach ($specificActionRules as $specificActionRule) { - if ($specificActionRule->isInverted()) { - /** 1. Checking on specific inverted rules */ - if ($specificActionRule->getResource()->matchField($field)) { - return false; // as the correspondent user is prohibited access resource - } - } else { - $specificNormalRules[] = $specificActionRule; + $specificNormalRules = []; + $starActionRules = []; + foreach ($unspecifiedActionRules as $unspecifiedActionRule) { + /** 1. Checking on specific inverted rules */ + if ($unspecifiedActionRule->isInverted() && $unspecifiedActionRule->getResource()->matchField($field)) { + return false; // as the correspondent user is prohibited access resource + } elseif ($unspecifiedActionRule->getAction()->wholeAction()) { + $starActionRules[] = $unspecifiedActionRule; + } elseif ($unspecifiedActionRule->getAction()->get() === $action) { + $specificNormalRules[] = $unspecifiedActionRule; } } /** 2. Star- rules */ - $starActionRules = $this->compiledRules->queryRule($scope, $resource, '*'); foreach ($starActionRules as $starActionRule) { if ($starActionRule->getResource()->matchField($field)) { return !$starActionRule->isInverted();