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 #297 from belgattitude/master
Browse files Browse the repository at this point in the history
Fixed serialization in RbacCollector for ZDT
  • Loading branch information
danizord committed Sep 9, 2015
2 parents 8046ed9 + a6f2b3e commit 64b9318
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
32 changes: 17 additions & 15 deletions src/ZfcRbac/Collector/RbacCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use ZendDeveloperTools\Collector\CollectorInterface;
use ZfcRbac\Options\ModuleOptions;
use ZfcRbac\Service\RoleService;
use ZfcRbac\Exception\InvalidArgumentException;

/**
* RbacCollector
Expand All @@ -43,11 +44,6 @@ class RbacCollector implements CollectorInterface, Serializable
*/
const PRIORITY = -100;

/**
* @var array
*/
protected $collection = [];

/**
* @var array
*/
Expand Down Expand Up @@ -184,7 +180,6 @@ private function collectPermissions(RoleInterface $role)
if (method_exists($role, 'getPermissions')) {
$permissions = $role->getPermissions();
} else {

// Gather the permissions for the given role. We have to use reflection as
// the RoleInterface does not have "getPermissions" method
$reflectionProperty = new ReflectionProperty($role, 'permissions');
Expand All @@ -209,27 +204,34 @@ private function collectPermissions(RoleInterface $role)
*/
public function getCollection()
{
return $this->collection;
return [
'guards' => $this->collectedGuards,
'roles' => $this->collectedRoles,
'permissions' => $this->collectedPermissions,
'options' => $this->collectedOptions
];
}

/**
* {@inheritDoc}
*/
public function serialize()
{
return serialize([
'guards' => $this->collectedGuards,
'roles' => $this->collectedRoles,
'permissions' => $this->collectedPermissions,
'options' => $this->collectedOptions
]);
return serialize($this->getCollection());
}

/**
* {@inheritDoc}
*/
public function unserialize($serialized)
{
$this->collection = unserialize($serialized);
$collection = unserialize($serialized);
if (!is_array($collection)) {
throw new InvalidArgumentException(__METHOD__ . ": Unserialized data should be an array.");
}
$this->collectedGuards = $collection['guards'];
$this->collectedRoles = $collection['roles'];
$this->collectedPermissions = $collection['permissions'];
$this->collectedOptions = $collection['options'];

}
}
23 changes: 19 additions & 4 deletions tests/ZfcRbacTest/Collector/RbacCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testUnserialize()
$unserialized = [
'guards' => ['foo' => 'bar'],
'roles' => ['foo' => 'bar'],
'permissions' => ['foo' => 'bar'],
'options' => ['foo' => 'bar']
];
$serialized = serialize($unserialized);
Expand All @@ -77,8 +78,21 @@ public function testUnserialize()
$this->assertSame(['foo' => 'bar'], $collection['guards']);
$this->assertSame(['foo' => 'bar'], $collection['roles']);
$this->assertSame(['foo' => 'bar'], $collection['options']);
$this->assertSame(['foo' => 'bar'], $collection['permissions']);
}

public function testUnserializeThrowsInvalidArgumentException()
{
$this->setExpectedException('ZfcRbac\Exception\InvalidArgumentException');
$collector = new RbacCollector();
$unserialized = 'not_an_array';
$serialized = serialize($unserialized);

$collector->unserialize($serialized);

}


public function testCollectNothingIfNoApplicationIsSet()
{
$mvcEvent = new MvcEvent();
Expand Down Expand Up @@ -250,9 +264,11 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role)

$roleProvider = $this->getMock('ZfcRbac\Role\RoleProviderInterface');

$roleService = new RoleService($identityProvider,
$roleService = new RoleService(
$identityProvider,
$roleProvider,
new RecursiveRoleIteratorStrategy());
new RecursiveRoleIteratorStrategy()
);

$serviceManager->expects($this->at(0))
->method('get')
Expand All @@ -270,5 +286,4 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role)
$collector->unserialize($collector->serialize());
return $collector->getCollection();
}

}
}

0 comments on commit 64b9318

Please sign in to comment.