Skip to content

Commit

Permalink
fix: Set hit rule to first matched allow rule for allow-and-deny effe…
Browse files Browse the repository at this point in the history
…ct (#124)
  • Loading branch information
basakest committed Nov 24, 2021
1 parent c5c8728 commit 88994ef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Effector/DefaultEffector.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ public function mergeEffects(string $expr, array $effects, array $matches, int $
}

if ($eft == Effector::ALLOW) {
// set hit rule to first matched allow rule, maybe overridden by the deny part
if ($result == Effector::INDETERMINATE) {
$explainIndex = $i;
}
$result = Effector::ALLOW;
} elseif ($eft == Effector::DENY) {
$result = Effector::DENY;
// set hit rule to the (first) matched deny rule
$explainIndex = $i;
break;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/EnforcerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ public function testEnforceRbacWithDeny()
public function testEnforceExRbacWithDeny()
{
$e = new Enforcer($this->modelAndPolicyPath . '/rbac_with_deny_model.conf', $this->modelAndPolicyPath . '/rbac_with_deny_policy.csv');
$this->assertEquals($e->enforceEx('alice', 'data1', 'read'), [true, []]);
$this->assertEquals($e->enforceEx('bob', 'data2', 'write'), [true, []]);
$this->assertEquals($e->enforceEx('alice', 'data2', 'read'), [true, []]);
$this->assertEquals($e->enforceEx('alice', 'data1', 'read'), [true, ['alice', 'data1', 'read', 'allow']]);
$this->assertEquals($e->enforceEx('bob', 'data2', 'write'), [true, ['bob', 'data2', 'write', 'allow']]);
$this->assertEquals($e->enforceEx('alice', 'data2', 'read'), [true, ['data2_admin', 'data2', 'read', 'allow']]);
$this->assertEquals($e->enforceEx('alice', 'data2', 'write'), [false, ['alice', 'data2', 'write', 'deny']]);
}

Expand Down

0 comments on commit 88994ef

Please sign in to comment.