public function testThrowExceptionIfIdentityIsWrongType() { $this->setExpectedException('ZfjRbac\\Exception\\RuntimeException', 'ZfjRbac expects your identity to implement ZfjRbac\\Identity\\IdentityInterface, "stdClass" given'); $identityProvider = $this->getMock('ZfjRbac\\Identity\\IdentityProviderInterface'); $identityProvider->expects($this->any())->method('getIdentity')->will($this->returnValue(new \stdClass())); $roleService = new RoleService($identityProvider, $this->getMock('ZfjRbac\\Role\\RoleProviderInterface'), $this->getMock('Rbac\\Traversal\\Strategy\\TraversalStrategyInterface')); $roleService->getIdentityRoles(); }
/** * Check if the permission is granted to the current identity * * @param string|PermissionInterface $permission * @param mixed $context * @return bool */ public function isGranted($permission, $context = null) { $roles = $this->roleService->getIdentityRoles(); if (empty($roles)) { return false; } if (!$this->rbac->isGranted($roles, $permission)) { return false; } if ($this->hasAssertion($permission)) { return $this->assert($this->assertions[(string) $permission], $context); } return true; }
/** * Collect roles and permissions * * @param RoleService $roleService * @return void */ private function collectIdentityRolesAndPermissions(RoleService $roleService) { $identityRoles = $roleService->getIdentityRoles(); foreach ($identityRoles as $role) { $roleName = $role->getName(); if (!$role instanceof HierarchicalRoleInterface) { $this->collectedRoles[] = $roleName; } else { $iteratorIterator = new RecursiveIteratorIterator(new \RecursiveArrayIterator($role->getChildren()), RecursiveIteratorIterator::SELF_FIRST); foreach ($iteratorIterator as $childRole) { $this->collectedRoles[$roleName][] = $childRole->getName(); $this->collectPermissions($childRole); } } $this->collectPermissions($role); } }