Skip to content

Commit

Permalink
add match any rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Sep 11, 2013
1 parent 1e4a312 commit 9808577
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 5 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,32 @@ $this->assertTrue($acl->isAllowed($all, 'SiteFrontend', 'View'));
$this->assertFalse($acl->isAllowed($all, 'SiteBackend', 'View'));
```

##### Using Match Any Rules
You can add a match any rule named '*'.

```php
$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');

$strategy = new AggregateStrategyDenyWins();

$all = new RoleAggregate();
$all->setStrategy($strategy);
$all->addRole($user);
$all->addRole($admin);

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, '*', true);
$acl->addRule($admin, $siteFrontend, '*', true);

$acl->addRule($admin, $siteBackend, '*', true);
$acl->addRule($user, $siteBackend, '*', false);

$this->assertTrue($acl->isAllowed($all, 'SiteFrontend', 'View'));
$this->assertFalse($acl->isAllowed($all, 'SiteBackend', 'View'));
```
__For more help check out wiki pages.__
6 changes: 2 additions & 4 deletions library/SimpleAcl/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,10 @@ public function isAllowedReturnResult($roleAggregate, $resourceAggregate, $ruleN
$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);
$this->isRuleAllow($roleName, $resourceName, $ruleName,
$ruleResultCollection, $roleAggregate, $resourceAggregate);

if($ruleName != "*") {
$ruleResultCollection = $this->addAnyRuleResultToResultSet(
Expand Down
89 changes: 88 additions & 1 deletion tests/SimpleAcl/Rule/MatchAnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,93 @@ public function testAny()
$user = new Role('User');
$siteFrontend = new Resource('SiteFrontend');
$acl->addRule($user, $siteFrontend, '*', true);
$this->assertTrue($acl->isAllowed($user, 'SiteFrontend', 'Edit'));
$this->assertTrue($acl->isAllowed('User', 'SiteFrontend', 'Edit'));
}

public function testDifferentResource()
{
$acl = new Acl();
$user = new Role('User');
$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');
$acl->addRule($user, $siteFrontend, '*', true);
$this->assertFalse($acl->isAllowed('User', 'SiteBackend', 'Edit'));
}

public function testAggregate()
{
$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');

$all = new RoleAggregate();
$all->addRole($user);
$all->addRole($admin);

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, '*', true);
$acl->addRule($admin, $siteFrontend, '*', true);

$acl->addRule($admin, $siteBackend, '*', true);
$acl->addRule($user, $siteBackend, '*', false);

$this->assertTrue($acl->isAllowed($all, 'SiteFrontend', 'View'));
$this->assertTrue($acl->isAllowed($all, 'SiteBackend', 'View'));
}

public function testAggregateDenyWins()
{
$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');

$strategy = new AggregateStrategyDenyWins();

$all = new RoleAggregate();
$all->setStrategy($strategy);
$all->addRole($user);
$all->addRole($admin);

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, '*', true);
$acl->addRule($admin, $siteFrontend, '*', true);

$acl->addRule($admin, $siteBackend, '*', true);
$acl->addRule($user, $siteBackend, '*', false);

$this->assertTrue($acl->isAllowed($all, 'SiteFrontend', 'View'));
$this->assertFalse($acl->isAllowed($all, 'SiteBackend', 'View'));
}

public function testRuleNotDefinedAggregate()
{
$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');

$strategy = new AggregateStrategyDenyWins();

$all = new RoleAggregate();
$all->setStrategy($strategy);
$all->addRole($user);
$all->addRole($admin);

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, '*', true);

$acl->addRule($admin, $siteBackend, '*', true);
$acl->addRule($user, $siteBackend, '*', false);

$this->assertTrue($acl->isAllowed($all, 'SiteFrontend', 'View'));
$this->assertFalse($acl->isAllowed($all, 'SiteBackend', 'View'));
}
}

0 comments on commit 9808577

Please sign in to comment.