Permalink
Browse files

Merge pull request #21 from GeometriaLab/feature/userRole-37093795

Feature/user role 37093795
  • Loading branch information...
2 parents a14f92d + a48ad82 commit 62b76e9bcb84e5f86a6f5031d0193f275c28f813 @shumkov shumkov committed Dec 3, 2012
@@ -1,51 +0,0 @@
-<?php
-
-namespace GeometriaLab\Permissions\Acl;
-
-use Zend\Permissions\Acl\Acl as ZendAcl,
- Zend\Permissions\Acl\Role\RoleInterface as ZendRoleInterface,
- Zend\Permissions\Acl\Resource\ResourceInterface as ZendResourceInterface,
- Zend\Permissions\Acl\Resource\GenericResource as ZendResource,
- Zend\Permissions\Acl\Assertion\AssertionInterface as ZendAssertionInterface;
-
-abstract class Resource extends ZendResource implements ZendAssertionInterface
-{
- /**
- * Define roles hear
- *
- * @param ZendAcl $acl
- */
- public function createRoles(ZendAcl $acl) {}
-
- /**
- * Define rules hear
- *
- * @abstract
- * @param ZendAcl $acl
- */
- abstract public function createRules(ZendAcl $acl);
-
- /**
- * Dynamic assertion
- *
- * @param ZendAcl $acl
- * @param ZendRoleInterface $role
- * @param ZendResourceInterface $resource
- * @param string $privilege
- * @return bool
- * @throws \InvalidArgumentException
- */
- public final function assert(ZendAcl $acl, ZendRoleInterface $role = null, ZendResourceInterface $resource = null, $privilege = null)
- {
- if (!$privilege) {
- return false;
- }
-
- $methodName = 'assert' . ucfirst($privilege);
- if (!method_exists($this, $methodName)) {
- throw new \InvalidArgumentException('Invalid dynamic assert - need declare ' . get_class($this) . '->' . $methodName);
- }
-
- return call_user_func(array($this, $methodName), $acl, $role);
- }
-}
@@ -1,150 +0,0 @@
-<?php
-
-namespace GeometriaLab\Permissions\Acl;
-
-use GeometriaLab\Api\Mvc\Router\Http\Api;
-
-use Zend\Stdlib\Glob as ZendGlob,
-
- Zend\ServiceManager\FactoryInterface as ZendFactoryInterface,
- Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface,
-
- Zend\Permissions\Acl\Acl as ZendAcl,
- Zend\Permissions\Acl\Role\GenericRole as ZendGenericRole;
-
-class ServiceFactory implements ZendFactoryInterface
-{
- /**
- * @var ZendAcl
- */
- private $acl;
- /**
- * @var array
- */
- private $config = array();
-
- /**
- * @param ZendServiceLocatorInterface $serviceLocator
- * @return ZendAcl
- */
- public function createService(ZendServiceLocatorInterface $serviceLocator)
- {
- $config = $serviceLocator->get('Configuration');
- if (isset($config['acl'])) {
- $this->setConfig($config['acl']);
- }
-
- $controllerNameSpace = $serviceLocator->get('Application')->getMvcEvent()->getRouteMatch()->getParam('__NAMESPACE__');
-
- $this->addRoles();
- $this->addResources($controllerNameSpace);
-
- return $this->getAcl();
- }
-
- /**
- * Set config
- *
- * @param array $config
- * @return ServiceFactory
- */
- public function setConfig($config)
- {
- $this->config = $config;
- return $this;
- }
-
- /**
- * Get Acl object
- *
- * @return ZendAcl
- */
- public function getAcl()
- {
- if ($this->acl === null) {
- $this->acl = new ZendAcl();
- }
- return $this->acl;
- }
-
- /**
- * Add all roles
- *
- * @return ServiceFactory
- * @throws \InvalidArgumentException
- */
- private function addRoles()
- {
- if (isset($this->config['roles']) && is_array($this->config['roles'])) {
- foreach ($this->config['roles'] as $role) {
- if (is_array($role)) {
- if (!isset($role['name'])) {
- throw new \InvalidArgumentException('Need name');
- }
-
- $roleId = $role['name'];
- $parent = isset($role['parent']) ? $role['parent'] : null;
- } else {
- $roleId = $role;
- $parent = null;
- }
-
- $this->getAcl()->addRole(new ZendGenericRole($roleId), $parent);
- }
- }
- return $this;
- }
-
- /**
- * Add all resources
- *
- * @param string $controllerNamespace
- * @return ServiceFactory
- */
- private function addResources($controllerNamespace)
- {
- $namespace = $this->getNamespace();
- $pathPattern = $this->getResourcesPath() . '*';
-
- foreach (ZendGlob::glob($pathPattern, ZendGlob::GLOB_BRACE) as $file) {
- /* @var \GeometriaLab\Permissions\Acl\Resource $resource */
- $resourceName = $namespace . '\\' . ucfirst(pathinfo($file, PATHINFO_FILENAME));
- $resourceId = $controllerNamespace . '\\' . ucfirst(pathinfo($file, PATHINFO_FILENAME));
- $resource = new $resourceName($resourceId);
-
- $this->getAcl()->addResource($resource);
-
- $resource->createRoles($this->getAcl());
- $resource->createRules($this->getAcl());
- }
- return $this;
- }
-
- /**
- * Get Acls' namespace
- *
- * @return string
- * @throws \InvalidArgumentException
- */
- private function getNamespace()
- {
- if (empty($this->config['__NAMESPACE__'])) {
- throw new \InvalidArgumentException('Need not empty "acl.__NAMESPACE__" param in config');
- }
- return $this->config['__NAMESPACE__'];
- }
-
- /**
- * Get path to the resources
- *
- * @return string
- * @throws \InvalidArgumentException
- */
- private function getResourcesPath()
- {
- if (empty($this->config['base_dir'])) {
- throw new \InvalidArgumentException('Need not empty "acl.base_dir" param in config');
- }
- return rtrim($this->config['base_dir'], '/') . '/';
- }
-}
@@ -0,0 +1,31 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion;
+
+abstract class AbstractResource implements ResourceInterface
+{
+ /**
+ * Resource unique identifier
+ *
+ * @var string
+ */
+ protected $name;
+
+ /**
+ * @param string $name
+ */
+ public function __construct($name)
+ {
+ $this->name = (string) $name;
+ }
+
+ /**
+ * Get unique identifier
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+}
@@ -0,0 +1,147 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion;
+
+class Assertion
+{
+ const DYNAMIC_ASSERT_PREFIX = 'can';
+
+ /**
+ * Resource tree
+ *
+ * @var ResourceInterface[]
+ */
+ protected $resources = array();
+
+ /**
+ * Get all resources
+ *
+ * @return ResourceInterface[]
+ */
+ public function getResources()
+ {
+ return $this->resources;
+ }
+
+ /**
+ * Adds a Resource having an identifier unique to the Assertion
+ *
+ * @param ResourceInterface $resource
+ * @throws Exception\InvalidArgumentException
+ * @return Assertion
+ */
+ public function addResource(ResourceInterface $resource)
+ {
+ $resourceName = $resource->getName();
+
+ if ($this->hasResource($resourceName)) {
+ throw new Exception\InvalidArgumentException("Resource id '$resourceName' already exists in the Assertion");
+ }
+
+ $this->resources[$resourceName] = $resource;
+
+ return $this;
+ }
+
+ /**
+ * Returns true if and only if the Resource exists in the Assertion
+ *
+ * The $resource parameter can either be a Resource or a Resource identifier.
+ *
+ * @param ResourceInterface|string $resource
+ * @return boolean
+ */
+ public function hasResource($resource)
+ {
+ $resourceName = self::getResourceName($resource);
+
+ return isset($this->resources[$resourceName]);
+ }
+
+ /**
+ * Returns the identified Resource
+ *
+ * The $resource parameter can either be a Resource or a Resource identifier.
+ *
+ * @param ResourceInterface|string $resource
+ * @throws Exception\InvalidArgumentException
+ * @return ResourceInterface
+ */
+ public function getResource($resource)
+ {
+ $resourceName = self::getResourceName($resource);
+
+ if (!$this->hasResource($resource)) {
+ throw new Exception\InvalidArgumentException("Resource '$resourceName' not found");
+ }
+
+ return $this->resources[$resourceName];
+ }
+
+ /**
+ * Removes a Resource and all of its children
+ *
+ * The $resource parameter can either be a Resource or a Resource identifier.
+ *
+ * @param ResourceInterface|string $resource
+ * @throws Exception\InvalidArgumentException
+ * @return Assertion
+ */
+ public function removeResource($resource)
+ {
+ $resourceName = self::getResourceName($resource);
+
+ if (!$this->hasResource($resource)) {
+ throw new Exception\InvalidArgumentException("Resource '$resourceName' not found");
+ }
+
+ unset($this->resources[$resourceName]);
+
+ return $this;
+ }
+
+ /**
+ * Returns false if and only if the Resource has deny to the $privilege
+ *
+ * @param ResourceInterface|string $resource
+ * @param string $privilege
+ * @param mixed $arg1 [optional]
+ * @param mixed $arg2 [optional]
+ * @param mixed $argN [optional]
+ * @return bool
+ * @throws Exception\RuntimeException
+ */
+ public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN = null)
+ {
+ $resource = $this->getResource($resource);
+
+ $methodName = self::DYNAMIC_ASSERT_PREFIX . ucfirst($privilege);
+ if (!method_exists($resource, $methodName)) {
+ throw new Exception\RuntimeException("No rules for privilege '{$privilege}'");
+ }
+
+ $funcArgs = func_get_args();
+ // Remove $resource and $privilege from array
+ unset($funcArgs[0], $funcArgs[1]);
+ // Assertion must be a first
+ array_unshift($funcArgs, $this);
+
+ return call_user_func_array(array($resource, $methodName), $funcArgs);
+ }
+
+ /**
+ * Get Resource name
+ * The $resource parameter can either be a Resource or a Resource identifier.
+ *
+ * @param ResourceInterface|string $resource
+ * @return string
+ */
+ protected static function getResourceName($resource)
+ {
+ if ($resource instanceof ResourceInterface) {
+ return $resource->getName();
+ }
+
+ return (string) $resource;
+ }
+}
@@ -0,0 +1,8 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion\Exception;
+
+interface ExceptionInterface
+{
+
+}
Oops, something went wrong.

0 comments on commit 62b76e9

Please sign in to comment.