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

Commit 64b9318

Browse files
committed
Merge pull request #297 from belgattitude/master
Fixed serialization in RbacCollector for ZDT
2 parents 8046ed9 + a6f2b3e commit 64b9318

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

src/ZfcRbac/Collector/RbacCollector.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use ZendDeveloperTools\Collector\CollectorInterface;
3030
use ZfcRbac\Options\ModuleOptions;
3131
use ZfcRbac\Service\RoleService;
32+
use ZfcRbac\Exception\InvalidArgumentException;
3233

3334
/**
3435
* RbacCollector
@@ -43,11 +44,6 @@ class RbacCollector implements CollectorInterface, Serializable
4344
*/
4445
const PRIORITY = -100;
4546

46-
/**
47-
* @var array
48-
*/
49-
protected $collection = [];
50-
5147
/**
5248
* @var array
5349
*/
@@ -184,7 +180,6 @@ private function collectPermissions(RoleInterface $role)
184180
if (method_exists($role, 'getPermissions')) {
185181
$permissions = $role->getPermissions();
186182
} else {
187-
188183
// Gather the permissions for the given role. We have to use reflection as
189184
// the RoleInterface does not have "getPermissions" method
190185
$reflectionProperty = new ReflectionProperty($role, 'permissions');
@@ -209,27 +204,34 @@ private function collectPermissions(RoleInterface $role)
209204
*/
210205
public function getCollection()
211206
{
212-
return $this->collection;
207+
return [
208+
'guards' => $this->collectedGuards,
209+
'roles' => $this->collectedRoles,
210+
'permissions' => $this->collectedPermissions,
211+
'options' => $this->collectedOptions
212+
];
213213
}
214-
215214
/**
216215
* {@inheritDoc}
217216
*/
218217
public function serialize()
219218
{
220-
return serialize([
221-
'guards' => $this->collectedGuards,
222-
'roles' => $this->collectedRoles,
223-
'permissions' => $this->collectedPermissions,
224-
'options' => $this->collectedOptions
225-
]);
219+
return serialize($this->getCollection());
226220
}
227221

228222
/**
229223
* {@inheritDoc}
230224
*/
231225
public function unserialize($serialized)
232226
{
233-
$this->collection = unserialize($serialized);
227+
$collection = unserialize($serialized);
228+
if (!is_array($collection)) {
229+
throw new InvalidArgumentException(__METHOD__ . ": Unserialized data should be an array.");
230+
}
231+
$this->collectedGuards = $collection['guards'];
232+
$this->collectedRoles = $collection['roles'];
233+
$this->collectedPermissions = $collection['permissions'];
234+
$this->collectedOptions = $collection['options'];
235+
234236
}
235237
}

tests/ZfcRbacTest/Collector/RbacCollectorTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function testUnserialize()
6565
$unserialized = [
6666
'guards' => ['foo' => 'bar'],
6767
'roles' => ['foo' => 'bar'],
68+
'permissions' => ['foo' => 'bar'],
6869
'options' => ['foo' => 'bar']
6970
];
7071
$serialized = serialize($unserialized);
@@ -77,8 +78,21 @@ public function testUnserialize()
7778
$this->assertSame(['foo' => 'bar'], $collection['guards']);
7879
$this->assertSame(['foo' => 'bar'], $collection['roles']);
7980
$this->assertSame(['foo' => 'bar'], $collection['options']);
81+
$this->assertSame(['foo' => 'bar'], $collection['permissions']);
8082
}
8183

84+
public function testUnserializeThrowsInvalidArgumentException()
85+
{
86+
$this->setExpectedException('ZfcRbac\Exception\InvalidArgumentException');
87+
$collector = new RbacCollector();
88+
$unserialized = 'not_an_array';
89+
$serialized = serialize($unserialized);
90+
91+
$collector->unserialize($serialized);
92+
93+
}
94+
95+
8296
public function testCollectNothingIfNoApplicationIsSet()
8397
{
8498
$mvcEvent = new MvcEvent();
@@ -250,9 +264,11 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role)
250264

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

253-
$roleService = new RoleService($identityProvider,
267+
$roleService = new RoleService(
268+
$identityProvider,
254269
$roleProvider,
255-
new RecursiveRoleIteratorStrategy());
270+
new RecursiveRoleIteratorStrategy()
271+
);
256272

257273
$serviceManager->expects($this->at(0))
258274
->method('get')
@@ -270,5 +286,4 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role)
270286
$collector->unserialize($collector->serialize());
271287
return $collector->getCollection();
272288
}
273-
274-
}
289+
}

0 commit comments

Comments
 (0)