Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 13 changed files with 785 additions and 10 deletions.
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "zendframework/zend-permissions-rbac",
"description": "Zend\\Permissions\\Rbac component",
"description": "provides a role-based access control management",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
"permissions-rbac"
"Rbac"
],
"homepage": "https://github.com/zendframework/zend-permissions-rbac",
"autoload": {
Expand All @@ -13,22 +13,22 @@
}
},
"require": {
"php": ">=5.3.23"
"php": ">=5.3.3"
},
"extra": {
"branch-alias": {
"dev-master": "2.4-dev",
"dev-develop": "2.5-dev"
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
},
"autoload-dev": {
"psr-4": {
"ZendTest\\Permissions\\Rbac\\": "test/"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
}
}
100 changes: 100 additions & 0 deletions src/AbstractIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Permissions\Rbac;

use RecursiveIterator;

abstract class AbstractIterator implements RecursiveIterator
{
protected $index = 0;
protected $children = array();

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
return $this->children[$this->index];
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next()
{
$this->index++;
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return scalar scalar on success, or null on failure.
*/
public function key()
{
return $this->index;
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return bool The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
return isset($this->children[$this->index]);
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
$this->index = 0;
}

/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Returns if an iterator can be created fot the current entry.
* @link http://php.net/manual/en/recursiveiterator.haschildren.php
* @return bool true if the current entry can be iterated over, otherwise returns false.
*/
public function hasChildren()
{
if ($this->valid() && ($this->current() instanceof RecursiveIterator)) {
return true;
}

return false;
}

/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Returns an iterator for the current entry.
* @link http://php.net/manual/en/recursiveiterator.getRoles.php
* @return RecursiveIterator An iterator for the current entry.
*/
public function getChildren()
{
return $this->children[$this->index];
}
}
118 changes: 118 additions & 0 deletions src/AbstractRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Permissions\Rbac;

use RecursiveIteratorIterator;

abstract class AbstractRole extends AbstractIterator implements RoleInterface
{
/**
* @var null|RoleInterface
*/
protected $parent;

/**
* @var string
*/
protected $name;

/**
* @var array
*/
protected $permissions = array();

/**
* Get the name of the role.
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Add permission to the role.
*
* @param $name
* @return RoleInterface
*/
public function addPermission($name)
{
$this->permissions[$name] = true;

return $this;
}

/**
* Checks if a permission exists for this role or any child roles.
*
* @param string $name
* @return bool
*/
public function hasPermission($name)
{
if (isset($this->permissions[$name])) {
return true;
}

$it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($it as $leaf) {
/** @var RoleInterface $leaf */
if ($leaf->hasPermission($name)) {
return true;
}
}

return false;
}

/**
* Add a child.
*
* @param RoleInterface|string $child
* @return Role
*/
public function addChild($child)
{
if (is_string($child)) {
$child = new Role($child);
}
if (!$child instanceof RoleInterface) {
throw new Exception\InvalidArgumentException(
'Child must be a string or implement Zend\Permissions\Rbac\RoleInterface'
);
}

$child->setParent($this);
$this->children[] = $child;

return $this;
}

/**
* @param RoleInterface $parent
* @return RoleInterface
*/
public function setParent($parent)
{
$this->parent = $parent;

return $this;
}

/**
* @return null|RoleInterface
*/
public function getParent()
{
return $this->parent;
}
}
21 changes: 21 additions & 0 deletions src/AssertionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Permissions\Rbac;

interface AssertionInterface
{
/**
* Assertion method - must return a boolean.
*
* @param Rbac $rbac
* @return bool
*/
public function assert(Rbac $rbac);
}
13 changes: 13 additions & 0 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Permissions\Rbac\Exception;

interface ExceptionInterface
{}
14 changes: 14 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Permissions\Rbac\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{}
Loading

0 comments on commit 24ccb84

Please sign in to comment.