Skip to content

Commit

Permalink
wip match any rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearCarbon committed Sep 11, 2013
1 parent 1cbc7d2 commit 1e4a312
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
46 changes: 37 additions & 9 deletions library/SimpleAcl/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,33 @@ protected function getNames($object)
* @param string|RoleAggregateInterface $roleAggregate
* @param string|ResourceAggregateInterface $resourceAggregate
*/
protected function isRuleAllow($roleName, $resourceName, $ruleName, RuleResultCollection $ruleResultCollection, $roleAggregate, $resourceAggregate)
protected function isRuleAllow($roleName, $resourceName, $ruleName,
RuleResultCollection $ruleResultCollection, $roleAggregate, $resourceAggregate)
{
foreach ($this->rules as $rule) {
$rule->resetAggregate($roleAggregate, $resourceAggregate);

$result = $rule->isAllowed($ruleName, $roleName, $resourceName);
$ruleResultCollection->add($result);
}

}

protected function addAnyRuleResultToResultSet($roleName, $resourceName,
$ruleResultCollection)
{
$any_resultSet = $this->matchAnyRuleResult($roleName, $resourceName);
$ruleResultCollection->merge($any_resultSet);

return $ruleResultCollection;
}

protected function matchAnyRuleResult($roleName, $resourceName)
{
$result_set = $this->isAllowedReturnResult($roleName,
$resourceName, "*");

return $result_set;
}

/**
Expand Down Expand Up @@ -222,18 +241,27 @@ public function isAllowed($roleName, $resourceName, $ruleName)
*/
public function isAllowedReturnResult($roleAggregate, $resourceAggregate, $ruleName)
{
$ruleResultCollection = $this->getRuleResultCollection($roleAggregate);
$ruleResultCollection = $this->getRuleResultCollection($roleAggregate);

$roles = $this->getNames($roleAggregate);
$resources = $this->getNames($resourceAggregate);
$roles = $this->getNames($roleAggregate);
$resources = $this->getNames($resourceAggregate);

var_dump($roles);
var_dump($resources);

foreach ($roles as $roleName) {
foreach ($resources as $resourceName) {
$this->isRuleAllow($roleName, $resourceName, $ruleName, $ruleResultCollection, $roleAggregate, $resourceAggregate);

if($ruleName != "*") {
$ruleResultCollection = $this->addAnyRuleResultToResultSet(
$roleName, $resourceName, $ruleResultCollection);
}

foreach ($roles as $roleName) {
foreach ($resources as $resourceName) {
$this->isRuleAllow($roleName, $resourceName, $ruleName, $ruleResultCollection, $roleAggregate, $resourceAggregate);
}
}
}

return $ruleResultCollection;
return $ruleResultCollection;
}

private function getRuleResultCollection($roleAggregate)
Expand Down
2 changes: 1 addition & 1 deletion library/SimpleAcl/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,4 @@ public function getResource()
{
return $this->resource;
}
}
}
7 changes: 7 additions & 0 deletions library/SimpleAcl/RuleResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public function add(RuleResult $result = null)
$this->collection->insert($result, $result->getPriority());
}

public function merge($resultCollection)
{
foreach($resultCollection as $result) {
$this->add($result);
}
}

/**
* @return bool
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/SimpleAcl/Rule/MatchAnyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace SimpleAclTest;

use PHPUnit_Framework_TestCase;

use SimpleAcl\Acl;
use SimpleAcl\Role;
use SimpleAcl\Role\RoleAggregate;
use SimpleAcl\Resource;
use SimpleAcl\Strategy\AggregateStrategyDenyWins;
use SimpleAcl\Object;
use SimpleAcl\Object\RecursiveIterator;
use RecursiveIteratorIterator;

class MatchAnyTest extends PHPUnit_Framework_TestCase
{
public function testAny()
{
$acl = new Acl();
$user = new Role('User');
$siteFrontend = new Resource('SiteFrontend');
$acl->addRule($user, $siteFrontend, '*', true);
$this->assertTrue($acl->isAllowed($user, 'SiteFrontend', 'Edit'));
}
}
2 changes: 1 addition & 1 deletion tests/SimpleAcl/Strategy/AggregateStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use SimpleAcl\Object\RecursiveIterator;
use RecursiveIteratorIterator;

class AggregateStrategyDenyWinsTest extends PHPUnit_Framework_TestCase
class AggregateStrategyTest extends PHPUnit_Framework_TestCase
{
public function testDenyWins()
{
Expand Down

0 comments on commit 1e4a312

Please sign in to comment.