Skip to content

Commit

Permalink
feat: Add BatchEnforce() API
Browse files Browse the repository at this point in the history
feat: Add BatchEnforce() API
  • Loading branch information
basakest committed Sep 27, 2021
1 parent 6034fe6 commit c8a783c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/CoreEnforcer.php
Expand Up @@ -813,6 +813,33 @@ public function buildIncrementalRoleLinks(int $op, string $ptype, array $rules):
$this->model->buildIncrementalRoleLinks($this->rmMap, $op, "g", $ptype, $rules);
}

/**
* BatchEnforce enforce in batches
*
* @param string[][] $requests
* @return bool[]
*/
public function batchEnforce(array $requests): array
{
return array_map(function (array $request) {
return $this->enforce(...$request);
}, $requests);
}

/**
* BatchEnforceWithMatcher enforce with matcher in batches
*
* @param string $matcher
* @param string[][] $requests
* @return bool[]
*/
public function batchEnforceWithMatcher(string $matcher, array $requests): array
{
return array_map(function (array $request) use ($matcher) {
return $this->enforceWithMatcher($matcher, ...$request);
}, $requests);
}

/**
* AddNamedMatchingFunc add MatchingFunc by ptype RoleManager
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/EnforcerTest.php
Expand Up @@ -381,4 +381,16 @@ public function testReloadPolicyWithFunc()
$this->assertTrue($e->enforce('alice', '/pen/1', 'GET'));
$this->assertTrue($e->enforce('alice', '/pen2/1', 'GET'));
}

public function testBatchEnforce()
{
$e = new Enforcer($this->modelAndPolicyPath . '/basic_model.conf', $this->modelAndPolicyPath . '/basic_policy.csv');

$res = $e->batchEnforce([
['alice', 'data1', 'read'],
['bob', 'data2', 'write'],
['jack', 'data3', 'read']
]);
$this->assertEquals([true, true, false], $res);
}
}

0 comments on commit c8a783c

Please sign in to comment.