Permalink
Browse files

Rbac component for ZF2

  • Loading branch information...
1 parent cc38f5e commit 3b35be54d91a60ee45f2b265f183ab425e798101 @spiffyjr spiffyjr committed Sep 21, 2012
@@ -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];
+ }
+}
@@ -0,0 +1,120 @@
+<?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;
+ }
+}
@@ -0,0 +1,29 @@
+<?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
+ */
+interface AssertionInterface
+{
+ /**
+ * Assertion method - must return a boolean.
+ *
+ * @param Rbac $bac
+ * @return boolean
+ */
+ public function assert(Rbac $rbac);
+}
@@ -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 Acl
+ */
+class InvalidArgumentException extends \InvalidArgumentException implements
+ ExceptionInterface
+{}
Oops, something went wrong.

0 comments on commit 3b35be5

Please sign in to comment.