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

Initial ZF2 RBAC Component #2616

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 102 additions & 0 deletions library/Zend/Permissions/Rbac/AbstractIterator.php
@@ -0,0 +1,102 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Permissions
*/

namespace Zend\Permissions\Rbac;

use RecursiveIterator;

/**
* @category Zend
* @package Zend_Permissions
* @subpackage Rbac
*/
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 boolean 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()
{
return count($this->children) > 0;
}

/**
* (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];
}
}
124 changes: 124 additions & 0 deletions library/Zend/Permissions/Rbac/AbstractRole.php
@@ -0,0 +1,124 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Permissions
*/

namespace Zend\Permissions\Rbac;

use RecursiveIteratorIterator;

/**
* @category Zend
* @package Zend_Permissions
* @subpackage Rbac
*/
abstract class AbstractRole extends AbstractIterator
{
/**
* @var null|AbstractRole
*/
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 AbstractRole
*/
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 AbstractRole $leaf */
if ($leaf->hasPermission($name)) {
return true;
}
}

return false;
}

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

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

return $this;
}

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

return $this;
}

/**
* @return null|AbstractRole
*/
public function getParent()
{
return $this->parent;
}
}
27 changes: 27 additions & 0 deletions library/Zend/Permissions/Rbac/AssertionInterface.php
@@ -0,0 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Permissions
*/

namespace Zend\Permissions\Rbac;

/**
* @category Zend
* @package Zend_Permissions
* @subpackage Rbac
*/
interface AssertionInterface
{
/**
* Assertion method - must return a boolean.
*
* @param Rbac $bac
* @return boolean
*/
public function assert(Rbac $rbac);
}
19 changes: 19 additions & 0 deletions library/Zend/Permissions/Rbac/Exception/ExceptionInterface.php
@@ -0,0 +1,19 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Permissions
*/

namespace Zend\Permissions\Rbac\Exception;

/**
* @category Zend
* @package Zend_Permissions
* @subpackage Rbac
*/
interface ExceptionInterface
{}
@@ -0,0 +1,20 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Permissions
*/

namespace Zend\Permissions\Rbac\Exception;

/**
* @category Zend
* @package Zend_Permissions
* @subpackage Rbac
*/
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{}