Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #325 from Wilt/patch-2
Browse files Browse the repository at this point in the history
Improvements InMemoryRoleProvider
  • Loading branch information
bakura10 committed Mar 18, 2016
2 parents 64b9318 + 101c4b5 commit b459677
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
68 changes: 47 additions & 21 deletions src/ZfcRbac/Role/InMemoryRoleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ZfcRbac\Role;

use Rbac\Role\RoleInterface;
use Rbac\Role\HierarchicalRole;
use Rbac\Role\Role;

Expand All @@ -42,6 +43,15 @@
class InMemoryRoleProvider implements RoleProviderInterface
{
/**
* Role storage
*
* @var array
*/
private $roles = [];

/**
* Roles config
*
* @var array
*/
private $rolesConfig = [];
Expand All @@ -62,34 +72,50 @@ 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];
$roles[] = $this->getRole($roleName);
}
return $roles;
}

if (isset($roleConfig['children'])) {
$role = new HierarchicalRole($roleName);
$childRoles = (array) $roleConfig['children'];
/**
* Get role by role name
*
* @param $roleName
* @return RoleInterface
*/
protected function getRole($roleName)
{
if (isset($this->roles[$roleName])) {
return $this->roles[$roleName];
}

foreach ($this->getRoles($childRoles) as $childRole) {
$role->addChild($childRole);
}
} else {
$role = new Role($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;
}

$permissions = isset($roleConfig['permissions']) ? $roleConfig['permissions'] : [];
$roleConfig = $this->rolesConfig[$roleName];

foreach ($permissions as $permission) {
$role->addPermission($permission);
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);
}

$roles[] = $role;
$permissions = isset($roleConfig['permissions']) ? $roleConfig['permissions'] : [];
foreach ($permissions as $permission) {
$role->addPermission($permission);
}

return $roles;
$this->roles[$roleName] = $role;

return $role;
}
}
3 changes: 3 additions & 0 deletions tests/ZfcRbacTest/Role/InMemoryRoleProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,8 @@ public function testInMemoryProvider()
$this->assertEquals('guest', $guestRole->getName());
$this->assertFalse($guestRole->hasPermission('write'));
$this->assertFalse($guestRole->hasPermission('delete'));

$this->assertSame($adminRole->getChildren()['member'], $memberRole);
$this->assertSame($memberRole->getChildren()['guest'], $guestRole);
}
}

0 comments on commit b459677

Please sign in to comment.