Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Feature/user role 37093795 #21

Merged
merged 17 commits into from

2 participants

library/GeometriaLab/Permissions/Assertion/Assertion.php
((20 lines not shown))
+ */
+ 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)
+ {
+ $resourceId = $resource->getId();
@Shumkov Owner
Shumkov added a note

getName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Assertion.php
((97 lines not shown))
+ $resourceId = $this->getResource($resource)->getName();
+
+ unset($this->resources[$resourceId]);
+
+ return $this;
+ }
+
+ /**
+ * Returns false if and only if the Resource has deny to the $privilege
+ *
+ * @param ResourceInterface|string $resource
+ * @param string $privilege
+ * @param AbstractModel $params
+ * @return bool
+ */
+ public function assert($resource, $privilege, AbstractModel $params = null)
@Shumkov Owner
Shumkov added a note

$resource, $privilege, $arg1 = null, $arg2 = null, $argN = null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Assertion.php
((99 lines not shown))
+ unset($this->resources[$resourceId]);
+
+ return $this;
+ }
+
+ /**
+ * Returns false if and only if the Resource has deny to the $privilege
+ *
+ * @param ResourceInterface|string $resource
+ * @param string $privilege
+ * @param AbstractModel $params
+ * @return bool
+ */
+ public function assert($resource, $privilege, AbstractModel $params = null)
+ {
+ if (!$this->hasResource($resource)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Assertion.php
((41 lines not shown))
+ $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)
+ {
+ $resourceId = (string) $resource;
@Shumkov Owner
Shumkov added a note

если инстанс интерфеса то getName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Resource.php
((90 lines not shown))
+
+ return $this;
+ }
+
+ /**
+ * Returns true if and only if the Privilege exists in the Resource
+ *
+ * @param string $privilege
+ * @return bool
+ */
+ public function hasPrivilege($privilege)
+ {
+ return isset($this->privileges[$privilege]);
+ }
+
+ /**
@Shumkov Owner
Shumkov added a note

remove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Resource.php
((119 lines not shown))
+ */
+ public function getServiceManager()
+ {
+ return $this->serviceManager;
+ }
+
+ /**
+ * Dynamic assertion
+ *
+ * @param Assertion $assertion
+ * @param string $privilege
+ * @param Model $model
+ * @return bool
+ * @throws \InvalidArgumentException
+ */
+ public final function assert(Assertion $assertion, $privilege, Model $model = null)
@Shumkov Owner
Shumkov added a note

эту логику в assertion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Resource.php
((61 lines not shown))
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Get all privileges defined for this resource
+ *
+ * @return array
+ */
+ public function getPrivileges()
+ {
+ return $this->privileges;
+ }
+
+ /**
@Shumkov Owner
Shumkov added a note

это не надо

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...metriaLab/Permissions/Assertion/ResourceInterface.php
((1 lines not shown))
+<?php
+
+namespace GeometriaLab\Permissions\Assertion;
+
+use GeometriaLab\Model\Schemaless\Model;
+
+interface ResourceInterface
+{
+ /**
+ * Get unique identifier
+ *
+ * @return string
+ */
+ public function getName();
+
+ /**
@Shumkov Owner
Shumkov added a note

убрать

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...GeometriaLab/Permissions/Assertion/ServiceFactory.php
((10 lines not shown))
Zend\ServiceManager\FactoryInterface as ZendFactoryInterface,
- Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface,
-
- Zend\Permissions\Acl\Acl as ZendAcl,
- Zend\Permissions\Acl\Role\GenericRole as ZendGenericRole;
+ Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface;
class ServiceFactory implements ZendFactoryInterface
@Shumkov Owner
Shumkov added a note

помойму это не фабрика

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ometriaLab/Permissions/Assertion/AbstractResource.php
((28 lines not shown))
+ /**
+ * Get unique identifier
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Get array of privileges which always allowed for all
+ *
+ * @return array
+ */
+ public function getAllowedPrivileges()
@Shumkov Owner
Shumkov added a note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Assertion.php
((107 lines not shown))
+ /**
+ * 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);
+
+ if (in_array($privilege, $resource->getAllowedPrivileges())) {
@Shumkov Owner
Shumkov added a note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Permissions/Assertion/Service.php
@@ -0,0 +1,141 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion;
+
+use GeometriaLab\Api\Mvc\Router\Http\Api;
@Shumkov Owner
Shumkov added a note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Shumkov Shumkov merged commit 62b76e9 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 12, 2012
  1. @Bukarinov

    Rename parent to parents.

    Bukarinov authored
Commits on Nov 13, 2012
  1. @Bukarinov

    Fix misprint

    Bukarinov authored
  2. @Bukarinov

    Fix misprint

    Bukarinov authored
  3. @Bukarinov

    Fix misprint

    Bukarinov authored
  4. @Bukarinov
Commits on Nov 29, 2012
  1. @Bukarinov

    Remove ZF ACL.

    Bukarinov authored
    Create Assertion draft.
  2. @Bukarinov

    Remove test for Acl.

    Bukarinov authored
Commits on Nov 30, 2012
  1. @Bukarinov
  2. @Bukarinov

    Merge master

    Bukarinov authored
  3. @Bukarinov
  4. @Bukarinov
  5. @Bukarinov
  6. @Bukarinov

    Rename ServiceFactory to Service.

    Bukarinov authored
    Simple AbstractResource and ResourceInterface.
    Pass all parameters from assertion to resource.
Commits on Dec 3, 2012
  1. @Bukarinov

    Move Guest, Permission, Permissions, User and UserInterface to Model/…

    Bukarinov authored
    …User.
    
    Rename grant to permissions.
  2. @Bukarinov
  3. @Bukarinov

    Tests for Assertion

    Bukarinov authored
  4. @Bukarinov
This page is out of date. Refresh to see the latest.
View
51 library/GeometriaLab/Permissions/Acl/Resource.php
@@ -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);
- }
-}
View
150 library/GeometriaLab/Permissions/Acl/ServiceFactory.php
@@ -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'], '/') . '/';
- }
-}
View
31 library/GeometriaLab/Permissions/Assertion/AbstractResource.php
@@ -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;
+ }
+}
View
147 library/GeometriaLab/Permissions/Assertion/Assertion.php
@@ -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;
+ }
+}
View
8 library/GeometriaLab/Permissions/Assertion/Exception/ExceptionInterface.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion\Exception;
+
+interface ExceptionInterface
+{
+
+}
View
8 library/GeometriaLab/Permissions/Assertion/Exception/InvalidArgumentException.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion\Exception;
+
+class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
+{
+
+}
View
8 library/GeometriaLab/Permissions/Assertion/Exception/RuntimeException.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion\Exception;
+
+class RuntimeException extends \RuntimeException implements ExceptionInterface
+{
+
+}
View
13 library/GeometriaLab/Permissions/Assertion/ResourceInterface.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion;
+
+interface ResourceInterface
+{
+ /**
+ * Get unique identifier
+ *
+ * @return string
+ */
+ public function getName();
+}
View
139 library/GeometriaLab/Permissions/Assertion/Service.php
@@ -0,0 +1,139 @@
+<?php
+
+namespace GeometriaLab\Permissions\Assertion;
+
+use Zend\Stdlib\Glob as ZendGlob,
+ Zend\ServiceManager\FactoryInterface as ZendFactoryInterface,
+ Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface;
+
+class Service implements ZendFactoryInterface
+{
+ /**
+ * @var Assertion
+ */
+ private $assertion;
+ /**
+ * @var array
+ */
+ private $config = array();
+ /**
+ * @var ZendServiceLocatorInterface
+ */
+ private $serviceLocator;
+
+ /**
+ * Create service
+ *
+ * @param ZendServiceLocatorInterface $serviceLocator
+ * @return Assertion
+ */
+ public function createService(ZendServiceLocatorInterface $serviceLocator)
+ {
+ $this->setServiceLocator($serviceLocator);
+
+ $config = $serviceLocator->get('Configuration');
+ if (isset($config['assertion'])) {
+ $this->setConfig($config['assertion']);
+ }
+
+ $this->addResources();
+
+ return $this->getAssertion();
+ }
+
+ /**
+ * Set Service Locator
+ *
+ * @param ZendServiceLocatorInterface $serviceLocator
+ */
+ public function setServiceLocator(ZendServiceLocatorInterface $serviceLocator)
+ {
+ $this->serviceLocator = $serviceLocator;
+ }
+
+ /**
+ * Get Service Locator
+ *
+ * @return ZendServiceLocatorInterface
+ */
+ public function getServiceLocator()
+ {
+ return $this->serviceLocator;
+ }
+
+ /**
+ * Set config
+ *
+ * @param array $config
+ * @return Service
+ */
+ public function setConfig($config)
+ {
+ $this->config = $config;
+ return $this;
+ }
+
+ /**
+ * Get Assertion object
+ *
+ * @return Assertion
+ */
+ public function getAssertion()
+ {
+ if ($this->assertion === null) {
+ $this->assertion = new Assertion();
+ }
+ return $this->assertion;
+ }
+
+ /**
+ * Add all resources
+ *
+ * @return Service
+ */
+ private function addResources()
+ {
+ $namespace = $this->getNamespace();
+ $pathPattern = $this->getResourcesPath() . '*';
+
+ foreach (ZendGlob::glob($pathPattern, ZendGlob::GLOB_BRACE) as $file) {
+ /* @var \GeometriaLab\Permissions\Assertion\ResourceInterface $resource */
+ $resourceName = ucfirst(pathinfo($file, PATHINFO_FILENAME));
+ $resourceClassName = $namespace . '\\' . $resourceName;
+
+ $this->getAssertion()->addResource(
+ new $resourceClassName($resourceName)
+ );
+ }
+
+ return $this;
+ }
+
+ /**
+ * Get Permissions' namespace
+ *
+ * @return string
+ * @throws \InvalidArgumentException
+ */
+ private function getNamespace()
+ {
+ if (empty($this->config['__NAMESPACE__'])) {
+ throw new \InvalidArgumentException('Need not empty "assertion.__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 "assertion.base_dir" param in config');
+ }
+ return rtrim($this->config['base_dir'], '/') . '/';
+ }
+}
View
21 tests/GeometriaLabTest/Permissions/Acl/Sample/Cities.php
@@ -1,21 +0,0 @@
-<?php
-
-namespace GeometriaLabTest\Permissions\Acl\Sample;
-
-use GeometriaLab\Permissions\Acl\Resource;
-
-use Zend\Permissions\Acl\Acl as ZendAcl,
- Zend\Permissions\Acl\Role\GenericRole as ZendGenericRole;
-
-class Cities extends Resource
-{
- public function createRoles(ZendAcl $acl)
- {
- $acl->addRole(new ZendGenericRole('cityManager'));
- }
-
- public function createRules(ZendAcl $acl)
- {
- $acl->allow('cityManager', $this, 'assert');
- }
-}
View
29 tests/GeometriaLabTest/Permissions/Acl/Sample/Users.php
@@ -1,29 +0,0 @@
-<?php
-
-namespace GeometriaLabTest\Permissions\Acl\Sample;
-
-use GeometriaLab\Permissions\Acl\Resource;
-
-use Zend\Permissions\Acl\Acl as ZendAcl,
- Zend\Permissions\Acl\Role\GenericRole as ZendGenericRole,
- Zend\Permissions\Acl\Role\RoleInterface as ZendRoleInterface;
-
-class Users extends Resource
-{
- public function createRoles(ZendAcl $acl)
- {
- $acl->addRole(new ZendGenericRole('moderator'), 'user');
- }
-
- public function createRules(ZendAcl $acl)
- {
- $acl->allow('moderator', $this, 'assert');
- $acl->allow('moderator', $this, 'dynamic', $this);
- $acl->allow('moderator', $this, 'notDynamic', $this);
- }
-
- protected function assertDynamic(ZendAcl $acl, ZendRoleInterface $role)
- {
- return true;
- }
-}
View
145 tests/GeometriaLabTest/Permissions/Assertion/AssertionTest.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion;
+
+use GeometriaLab\Permissions\Assertion\Assertion;
+
+class AssertionTest extends \PHPUnit_Framework_TestCase
+{
+ public function testAddResource()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertInstanceOf('\\GeometriaLab\\Permissions\\Assertion\\ResourceInterface', $assertion->getResource('Foo'));
+ $this->assertEquals($fooResource, $assertion->getResource('Foo'));
+ }
+
+ public function testAddResources()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $barResource = new Sample\Bar('Bar');
+ $assertion->addResource($barResource);
+
+ $expected = array(
+ 'Foo' => $fooResource,
+ 'Bar' => $barResource,
+ );
+
+ $this->assertEquals($expected, $assertion->getResources());
+ }
+
+ public function testAddExistingResource()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+ $assertion->addResource($fooResource);
+ }
+
+ public function testHasResourceByName()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertTrue($assertion->hasResource('Foo'));
+ }
+
+ public function testHasResourceByObject()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertTrue($assertion->hasResource($fooResource));
+ }
+
+ public function testHasNotExistingResource()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertFalse($assertion->hasResource('Bar'));
+ }
+
+ public function testGetResourceByName()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertEquals($fooResource, $assertion->getResource('Foo'));
+ }
+
+ public function testGetResourceByObject()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertEquals($fooResource, $assertion->getResource($fooResource));
+ }
+
+ public function testGetNotExistingResource()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
+
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $assertion->getResource('Bar');
+ }
+
+ public function testRemoveResourceByName()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertEquals($fooResource, $assertion->getResource('Foo'));
+
+ $assertion->removeResource('Foo');
+
+ $this->assertFalse($assertion->hasResource('Foo'));
+ $this->assertEmpty($assertion->getResources());
+ }
+
+ public function testAssertWithoutResource()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
+
+ $assertion = new Assertion();
+ $assertion->assert('Bar', 'privilege');
+ }
+
+ public function testAssertWithoutPrivilege()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\RuntimeException');
+
+ $assertion = new Assertion();
+ $barResource = new Sample\Bar('Bar');
+ $assertion->addResource($barResource);
+
+ $assertion->assert('Bar', 'privilege');
+ }
+
+ public function testWithDynamicAssert()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $obj = new \stdClass();
+ $obj->bar = 'bar';
+
+ $this->assertTrue($assertion->assert('Foo', 'dynamicAssert', $obj, array('bar')));
+ }
+}
View
15 tests/GeometriaLabTest/Permissions/Assertion/ResourceTest.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion;
+
+use GeometriaLab\Permissions\Assertion\Assertion;
+
+class ResourceTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCreate()
+ {
+ $resource = new Sample\Foo('Foo');
+
+ $this->assertEquals('Foo', $resource->getName());
+ }
+}
View
10 tests/GeometriaLabTest/Permissions/Assertion/Sample/Bar.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion\Sample;
+
+use GeometriaLab\Permissions\Assertion\AbstractResource;
+
+class Bar extends AbstractResource
+{
+
+}
View
14 tests/GeometriaLabTest/Permissions/Assertion/Sample/Foo.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion\Sample;
+
+use GeometriaLab\Permissions\Assertion\Assertion,
+ GeometriaLab\Permissions\Assertion\AbstractResource;
+
+class Foo extends AbstractResource
+{
+ public function canDynamicAssert(Assertion $assertion, \stdClass $obj, array $array)
+ {
+ return !empty($obj) && !empty($array);
+ }
+}
View
71 .../GeometriaLabTest/Permissions/Acl/ServiceTest.php → ...triaLabTest/Permissions/Assertion/ServiceTest.php
@@ -1,8 +1,9 @@
<?php
-namespace GeometriaLabTest\Permissions\Acl;
+namespace GeometriaLabTest\Permissions\Assertion;
-use GeometriaLab\Permissions\Acl\ServiceFactory as AclServiceFactory;
+use GeometriaLab\Permissions\Assertion\Assertion,
+ GeometriaLab\Permissions\Assertion\Service;
use Zend\ServiceManager\ServiceManager as ZendServiceManager,
Zend\Mvc\Service\ServiceManagerConfig as ZendServiceManagerConfig,
@@ -16,9 +17,9 @@ class ServiceTest extends \PHPUnit_Framework_TestCase
static private $serviceManager;
/**
- * @var \Zend\Permissions\Acl\Acl
+ * @var Assertion
*/
- static private $acl;
+ static private $assertion;
static public function setUpBeforeClass()
{
@@ -39,16 +40,9 @@ static public function setUpBeforeClass()
'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
'Config' => function($e){
return array(
- 'acl' => array(
- 'roles' => array(
- 'guest',
- array(
- 'name' => 'user',
- 'parent' => 'guest',
- ),
- ),
+ 'assertion' => array(
'base_dir' => __DIR__ . '/Sample',
- '__NAMESPACE__' => 'GeometriaLabTest\Permissions\Acl\Sample',
+ '__NAMESPACE__' => 'GeometriaLabTest\Permissions\Assertion\Sample',
),
);
},
@@ -65,53 +59,22 @@ static public function setUpBeforeClass()
'__NAMESPACE__' => 'Sample',
)));
- $serviceFactory = new AclServiceFactory();
- static::$acl = $serviceFactory->createService(static::$serviceManager);
+ $serviceFactory = new Service();
+ static::$assertion = $serviceFactory->createService(static::$serviceManager);
}
- public function testStaticRoles()
+ public function testCreateService()
{
- $this->assertTrue(static::$acl->hasRole('guest'));
- $this->assertTrue(static::$acl->hasRole('user'));
+ $this->assertInstanceOf('\\GeometriaLab\\Permissions\\Assertion\\Assertion', static::$assertion);
}
- public function testInheritStaticRole()
+ public function testAddResources()
{
- $this->assertTrue(static::$acl->inheritsRole('user', 'guest'));
- }
-
- public function testDynamicRoles()
- {
- $this->assertTrue(static::$acl->hasRole('moderator'));
- $this->assertTrue(static::$acl->hasRole('cityManager'));
- }
-
- public function testResources()
- {
- $this->assertTrue(static::$acl->hasResource('Sample\Users'));
- $this->assertTrue(static::$acl->hasResource('Sample\Cities'));
- }
-
- public function testStaticAssert()
- {
- $this->assertTrue(static::$acl->isAllowed('moderator', 'Sample\Users', 'assert'));
- $this->assertFalse(static::$acl->isAllowed('moderator', 'Sample\Users', 'foo'));
- }
-
- public function testDynamicAssert()
- {
- $this->assertTrue(static::$acl->isAllowed('moderator', 'Sample\Users', 'dynamic'));
- }
-
- public function testDynamicAssertWithoutPrivilege()
- {
- $this->assertFalse(static::$acl->isAllowed('moderator', 'Sample\Users'));
- }
-
- public function testBadDynamicAssert()
- {
- $this->setExpectedException('\InvalidArgumentException');
+ $expected = array(
+ 'Foo' => new Sample\Foo('Foo'),
+ 'Bar' => new Sample\Bar('Bar'),
+ );
- static::$acl->isAllowed('moderator', 'Sample\Users', 'notDynamic');
+ $this->assertEquals($expected, static::$assertion->getResources());
}
}
Something went wrong with that request. Please try again.