/**
  * {@inheritDoc}
  */
 public function getRoles(array $roleNames)
 {
     $roles = [];
     foreach ($roleNames as $roleName) {
         // If no config, we create a simple role with no permission
         if (!isset($this->rolesConfig[$roleName])) {
             $roles[] = new Role($roleName);
             continue;
         }
         $roleConfig = $this->rolesConfig[$roleName];
         if (isset($roleConfig['children'])) {
             $role = new HierarchicalRole($roleName);
             $childRoles = (array) $roleConfig['children'];
             foreach ($this->getRoles($childRoles) as $childRole) {
                 $role->addChild($childRole);
             }
         } else {
             $role = new Role($roleName);
         }
         $permissions = isset($roleConfig['permissions']) ? $roleConfig['permissions'] : [];
         foreach ($permissions as $permission) {
             $role->addPermission($permission);
         }
         $roles[] = $role;
     }
     return $roles;
 }
 /**
  * @covers Rbac\Traversal\RecursiveRoleIterator::getChildren
  */
 public function testGetChildrenReturnsAnRecursiveRoleIteratorOfRoleChildren()
 {
     $baz = new HierarchicalRole('Baz');
     $baz->addChild(new Role('Foo'));
     $baz->addChild(new Role('Bar'));
     $roles = [$baz];
     $iterator = new RecursiveRoleIterator($roles);
     $this->assertEquals($iterator->getChildren(), new RecursiveRoleIterator($baz->getChildren()));
 }
Example #3
0
 /**
  * @covers Rbac\Role\HierarchicalRole::getChildren
  */
 public function testCanGetChildren()
 {
     $role = new HierarchicalRole('role');
     $child1 = new HierarchicalRole('child 1');
     $child2 = new HierarchicalRole('child 2');
     $role->addChild($child1);
     $role->addChild($child2);
     $children = $role->getChildren();
     $this->assertCount(2, $children);
     $this->assertContainsOnlyInstancesOf(HierarchicalRoleInterface::class, $children);
 }
Example #4
0
 /**
  * @covers Rbac\Traversal\Strategy\GeneratorStrategy::getRolesIterator
  */
 public function testTraverseHierarchicalRoles()
 {
     $strategy = new GeneratorStrategy();
     $child1 = new Role('child 1');
     $child2 = new Role('child 2');
     $child3 = new Role('child 3');
     $parent1 = new HierarchicalRole('parent 1');
     $parent1->addChild($child1);
     $parent2 = new HierarchicalRole('parent 2');
     $parent2->addChild($child2);
     $parent3 = new HierarchicalRole('parent 3');
     $parent3->addChild($child3);
     $roles = [$parent1, $parent2, $parent3];
     $expectedResult = [$parent1, $child1, $parent2, $child2, $parent3, $child3];
     $this->assertEquals($expectedResult, iterator_to_array($strategy->getRolesIterator($roles)));
 }
 /**
  * Get role by role name
  *
  * @param $roleName
  * @return RoleInterface
  */
 protected function getRole($roleName)
 {
     if (isset($this->roles[$roleName])) {
         return $this->roles[$roleName];
     }
     // If no config, we create a simple role with no permission
     if (!isset($this->rolesConfig[$roleName])) {
         $role = new Role($roleName);
         $this->roles[$roleName] = $role;
         return $role;
     }
     $roleConfig = $this->rolesConfig[$roleName];
     if (isset($roleConfig['children'])) {
         $role = new HierarchicalRole($roleName);
         $childRoles = (array) $roleConfig['children'];
         foreach ($childRoles as $childRole) {
             $childRole = $this->getRole($childRole);
             $role->addChild($childRole);
         }
     } else {
         $role = new Role($roleName);
     }
     $permissions = isset($roleConfig['permissions']) ? $roleConfig['permissions'] : [];
     foreach ($permissions as $permission) {
         $role->addPermission($permission);
     }
     $this->roles[$roleName] = $role;
     return $role;
 }
Example #6
0
 /**
  * @covers Rbac\Rbac::isGranted
  */
 public function testReturnFalseIfNoHierarchicalRoleHasPermission()
 {
     $childRole = new Role('Bar');
     $parentRole = new HierarchicalRole('Foo');
     $parentRole->addChild($childRole);
     $rbac = new Rbac();
     $this->assertFalse($rbac->isGranted($parentRole, 'permission'));
 }