Example #1
0
 /**
  * Tests basic Resource inheritance
  *
  * @return void
  */
 public function testResourceInherits()
 {
     $resourceCity = new Resource\GenericResource('city');
     $resourceBuilding = new Resource\GenericResource('building');
     $resourceRoom = new Resource\GenericResource('room');
     $this->_acl->addResource($resourceCity)->addResource($resourceBuilding, $resourceCity->getResourceId())->addResource($resourceRoom, $resourceBuilding);
     $this->assertTrue($this->_acl->inheritsResource($resourceBuilding, $resourceCity, true));
     $this->assertTrue($this->_acl->inheritsResource($resourceRoom, $resourceBuilding, true));
     $this->assertTrue($this->_acl->inheritsResource($resourceRoom, $resourceCity));
     $this->assertFalse($this->_acl->inheritsResource($resourceCity, $resourceBuilding));
     $this->assertFalse($this->_acl->inheritsResource($resourceBuilding, $resourceRoom));
     $this->assertFalse($this->_acl->inheritsResource($resourceCity, $resourceRoom));
     $this->_acl->removeResource($resourceBuilding);
     $this->assertFalse($this->_acl->hasResource($resourceRoom));
 }
Example #2
0
 /**
  * @param Acl $acl
  * @param $restriction
  * @param RoleRole $role
  * @param RouteResource $resource
  */
 protected function restrictRoute(Acl $acl, $restriction, RoleRole $role = null, RouteResource $resource = null)
 {
     if (is_null($resource)) {
         return $acl->{$restriction}($role, $resource);
     }
     $func = function ($route, array $names) use(&$func, $acl, $resource, $restriction, $role) {
         if (!count($names)) {
             throw new \DomainException("Names could not be empty");
         }
         if (!$route instanceof SimpleRouteStack) {
             if ($route instanceof \Iterator) {
                 $routes = $route;
             } else {
                 return;
             }
         } else {
             $routes = $route->getRoutes();
         }
         foreach ($routes as $name => $route) {
             $routeNames = array_merge($names, array($name));
             $parentResource = new RouteResource(join('/', $names));
             $childResource = new RouteResource(join('/', $routeNames));
             if (!$acl->hasResource($childResource)) {
                 $acl->addResource($childResource, $parentResource);
             }
             if ($acl->hasResource($resource) && $acl->inheritsResource($childResource, $resource)) {
                 $acl->{$restriction ? 'allow' : 'deny'}($role, $childResource);
             }
             if ($route instanceof SimpleRouteStack) {
                 $func($route->getRoutes(), $routeNames);
             }
         }
     };
     $firstPart = $resource->getFirstPart();
     if (!$acl->hasResource($firstPart)) {
         $acl->addResource(new RouteResource($firstPart), null);
     }
     $func($this->routeStack->getRoute($firstPart), array($firstPart));
     return $acl->{$restriction ? 'allow' : 'deny'}($role, $resource);
 }