Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from RomainLanz/patch-1
Browse files Browse the repository at this point in the history
Allowing sending an array for allow() and deny() method
  • Loading branch information
machuga committed Feb 19, 2015
2 parents b5f111d + 099787a commit e12c9cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/Authority/Authority.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,43 +108,51 @@ public function cannot($action, $resource, $resourceValue = null)
/**
* Define privilege for a given action and resource
*
* @param string $action Action for the rule
* @param mixed $resource Resource for the rule
* @param Closure|null $condition Optional condition for the rule
* @return Rule
* @param array|string $actions Action for the rule
* @param mixed $resource Resource for the rule
* @param Closure|null $condition Optional condition for the rule
* @return array|Rule
*/
public function allow($action, $resource, $condition = null)
public function allow($actions, $resource, $condition = null)
{
return $this->addRule(true, $action, $resource, $condition);
return $this->addRule(true, $actions, $resource, $condition);
}

/**
* Define restriction for a given action and resource
*
* @param string $action Action for the rule
* @param mixed $resource Resource for the rule
* @param Closure|null $condition Optional condition for the rule
* @return Rule
* @param array|string $actions Action for the rule
* @param mixed $resource Resource for the rule
* @param Closure|null $condition Optional condition for the rule
* @return array|Rule
*/
public function deny($action, $resource, $condition = null)
public function deny($actions, $resource, $condition = null)
{
return $this->addRule(false, $action, $resource, $condition);
return $this->addRule(false, $actions, $resource, $condition);
}

/**
* Define rule for a given action and resource
*
* @param boolean $allow True if privilege, false if restriction
* @param string $action Action for the rule
* @param mixed $resource Resource for the rule
* @param Closure|null $condition Optional condition for the rule
* @return Rule
* @param boolean $allow True if privilege, false if restriction
* @param array|string $actions Action for the rule
* @param mixed $resource Resource for the rule
* @param Closure|null $condition Optional condition for the rule
* @return array|Rule
*/
public function addRule($allow, $action, $resource, $condition = null)
public function addRule($allow, $actions, $resource, $condition = null)
{
$rule = new Rule($allow, $action, $resource, $condition);
$this->rules->add($rule);
return $rule;
$rules = array();

$actions = (array) $actions;

foreach ($actions as $action) {
$rule = new Rule($allow, $action, $resource, $condition);
$this->rules->add($rule);
$rules[] = $rule;
}

return count($rules) === 1 ? $rules[0] : $rules;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/AuthorityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public function testCanStoreNewPrivilege()
$this->assertTrue($rule->getBehavior());
}

public function testCanStoreMultiplePrivileges()
{
$rules = $this->auth->allow(array('read', 'create'), 'User');
$this->assertCount(2, $this->auth->getRules());
$this->assertCount(2, $rules);

foreach ($rules as $rule) {
$this->assertContains($rule, $this->auth->getRules());
$this->assertTrue($rule->getBehavior());
}
}

public function testCanStoreNewRestriction()
{
$rule = $this->auth->deny('read', 'User');
Expand All @@ -44,6 +56,18 @@ public function testCanStoreNewRestriction()
$this->assertFalse($rule->getBehavior());
}

public function testCanStoreMultipleRestrictions()
{
$rules = $this->auth->deny(array('read', 'create'), 'User');
$this->assertCount(2, $this->auth->getRules());
$this->assertCount(2, $rules);

foreach ($rules as $rule) {
$this->assertContains($rule, $this->auth->getRules());
$this->assertFalse($rule->getBehavior());
}
}

public function testCanStoreNewAlias()
{
$alias = $this->auth->addAlias('manage', array('create', 'read', 'update', 'delete'));
Expand Down

0 comments on commit e12c9cb

Please sign in to comment.