Skip to content

Commit

Permalink
fix: abac not using policy
Browse files Browse the repository at this point in the history
  • Loading branch information
basakest committed Nov 26, 2021
1 parent 88994ef commit f728d2c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
11 changes: 11 additions & 0 deletions examples/abac_not_using_policy_model.conf
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act, eft

[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))

[matchers]
m = r.sub == r.obj.owner
4 changes: 4 additions & 0 deletions examples/abac_rule_effect_policy.csv
@@ -0,0 +1,4 @@
p, alice, /data1, read, deny
p, alice, /data1, write, allow
p, bob, /data2, write, deny
p, bob, /data2, read, allow
2 changes: 1 addition & 1 deletion src/CoreEnforcer.php
Expand Up @@ -640,7 +640,7 @@ protected function enforcing(string $matcher, &$explains = [], ...$rvals): bool
$explainIndex = 0;

$policyLen = \count($this->model['p'][$pType]->policy);
if (0 != $policyLen) {
if (0 != $policyLen && (strpos($expString, $pType . '_') !== false)) {
foreach ($this->model['p'][$pType]->policy as $policyIndex => $pvals) {
$parameters = array_combine($pTokens, $pvals);
if (false == $parameters) {
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Model/ModelTest.php
Expand Up @@ -8,6 +8,7 @@
use Casbin\Rbac\DefaultRoleManager\RoleManager;
use Casbin\Util\BuiltinOperations;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
* ModelTest.
Expand All @@ -18,6 +19,14 @@ class ModelTest extends TestCase
{
private $modelAndPolicyPath = __DIR__ . '/../../../examples';

public static function newTestResource(string $name, string $owner): stdClass
{
$r = new stdClass();
$r->name = $name;
$r->owner = $owner;
return $r;
}

public function testLoadModelFromText()
{
$text = <<<'EOT'
Expand Down Expand Up @@ -53,6 +62,18 @@ public function testLoadModelFromText()
$this->assertTrue($e->enforce('bob', 'data2', 'write'));
}

public function testABACNotUsingPolicy()
{
$e = new Enforcer($this->modelAndPolicyPath . '/abac_not_using_policy_model.conf', $this->modelAndPolicyPath . '/abac_rule_effect_policy.csv');
$data1 = self::newTestResource('data1', 'alice');
$data2 = self::newTestResource('data2', 'bob');

$this->assertEquals($e->enforce('alice', $data1, 'read'), true);
$this->assertEquals($e->enforce('alice', $data1, 'write'), true);
$this->assertEquals($e->enforce('alice', $data2, 'read'), false);
$this->assertEquals($e->enforce('alice', $data2, 'write'), false);
}

public function testABACPolicy()
{
$e = new Enforcer($this->modelAndPolicyPath . '/abac_rule_model.conf', $this->modelAndPolicyPath . '/abac_rule_policy.csv');
Expand Down

0 comments on commit f728d2c

Please sign in to comment.