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 #195 from arekkas/solves-194
Browse files Browse the repository at this point in the history
Hotfix for #194
  • Loading branch information
bakura10 committed Feb 20, 2014
2 parents 9ac641f + 7730a46 commit b1010ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ZfcRbac/Role/ObjectRepositoryRoleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ObjectRepositoryRoleProvider implements RoleProviderInterface
*/
private $roleNameProperty;

/**
* @var array
*/
private $roleCache = [];

/**
* Constructor
*
Expand All @@ -51,16 +56,34 @@ public function __construct(ObjectRepository $objectRepository, $roleNamePropert
$this->roleNameProperty = $roleNameProperty;
}

/**
* Clears the role cache
*
* @return void
*/
public function clearRoleCache()
{
$this->roleCache = [];
}

/**
* {@inheritDoc}
*/
public function getRoles(array $roleNames)
{
$key = implode($roleNames);

if (isset($this->roleCache[$key])) {
return $this->roleCache[$key];
}

$roles = $this->objectRepository->findBy([$this->roleNameProperty => $roleNames]);

// We allow more roles to be loaded than asked (although this should not happen because
// role name should have a UNIQUE constraint in database... but just in case ;))
if (count($roles) >= count($roleNames)) {
$this->roleCache[$key] = $roles;

return $roles;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/ZfcRbacTest/Role/ObjectRepositoryRoleProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ public function testObjectRepositoryProviderForHierarchicalRole()
$this->assertEquals('memberguest', $childRolesString);
}

public function testRoleCacheOnConsecutiveCalls()
{
$objectRepository = $this->getMock('Doctrine\ORM\EntityRepository', ['findBy'], [], '', false);
$memberRole = new FlatRole('member');
$provider = new ObjectRepositoryRoleProvider($objectRepository, 'name');
$result = [$memberRole];

$objectRepository->expects($this->once())->method('findBy')->will($this->returnValue($result));

$this->assertEquals($result, $provider->getRoles(['member']));
$this->assertEquals($result, $provider->getRoles(['member']));
}

public function testClearRoleCache()
{
$objectRepository = $this->getMock('Doctrine\ORM\EntityRepository', ['findBy'], [], '', false);
$memberRole = new FlatRole('member');
$provider = new ObjectRepositoryRoleProvider($objectRepository, 'name');
$result = [$memberRole];

$objectRepository->expects($this->exactly(2))->method('findBy')->will($this->returnValue($result));

$this->assertEquals($result, $provider->getRoles(['member']));
$provider->clearRoleCache();
$this->assertEquals($result, $provider->getRoles(['member']));
}

public function testThrowExceptionIfAskedRoleIsNotFound()
{
$this->serviceManager = ServiceManagerFactory::getServiceManager();
Expand Down

0 comments on commit b1010ef

Please sign in to comment.