public function updateUserContext(UserContext $context)
 {
     $user = $this->repository->getCurrentUser();
     /** @var \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments */
     $roleAssignments = $this->repository->sudo(function (Repository $repository) use($user) {
         return $repository->getRoleService()->getRoleAssignmentsForUser($user, true);
     });
     $roleIds = array();
     $limitationValues = array();
     /** @var UserRoleAssignment $roleAssignment */
     foreach ($roleAssignments as $roleAssignment) {
         $roleId = $roleAssignment->getRole()->id;
         $roleIds[] = $roleId;
         $limitation = $roleAssignment->getRoleLimitation();
         // If a limitation is present, store the limitation values by roleId
         if ($limitation !== null) {
             $limitationValuesKey = sprintf('%s-%s', $roleId, $limitation->getIdentifier());
             $limitationValues[$limitationValuesKey] = array();
             foreach ($limitation->limitationValues as $value) {
                 $limitationValues[$limitationValuesKey][] = $value;
             }
         }
     }
     $context->addParameter('roleIdList', $roleIds);
     $context->addParameter('roleLimitationList', $limitationValues);
 }
 /**
  * {@inheritDoc}
  *
  * @throws InvalidConfigurationException when called without a security context being set.
  */
 public function updateUserContext(UserContext $context)
 {
     if (null === $this->context) {
         throw new InvalidConfigurationException('The context hash URL must be under a firewall.');
     }
     if (null === ($token = $this->context->getToken())) {
         return;
     }
     $roles = array_map(function (RoleInterface $role) {
         return $role->getRole();
     }, $token->getRoles());
     // Order is not important for roles and should not change hash.
     sort($roles);
     $context->addParameter('roles', $roles);
 }
 public function testSetIdentity()
 {
     $user = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\User');
     $userContext = new UserContext();
     $this->repositoryMock->expects($this->once())->method('getCurrentUser')->will($this->returnValue($user));
     $roleId1 = 123;
     $roleId2 = 456;
     $roleId3 = 789;
     $limitationForRole2 = $this->generateLimitationMock(array('limitationValues' => array('/1/2', '/1/2/43')));
     $limitationForRole3 = $this->generateLimitationMock(array('limitationValues' => array('foo', 'bar')));
     $returnedRoleAssignments = array($this->generateRoleAssignmentMock(array('role' => $this->generateRoleMock(array('id' => $roleId1)))), $this->generateRoleAssignmentMock(array('role' => $this->generateRoleMock(array('id' => $roleId2)), 'limitation' => $limitationForRole2)), $this->generateRoleAssignmentMock(array('role' => $this->generateRoleMock(array('id' => $roleId3)), 'limitation' => $limitationForRole3)));
     $this->roleServiceMock->expects($this->once())->method('getRoleAssignmentsForUser')->with($user, true)->will($this->returnValue($returnedRoleAssignments));
     $this->assertSame(array(), $userContext->getParameters());
     $contextProvider = new RoleContextProvider($this->repositoryMock);
     $contextProvider->updateUserContext($userContext);
     $userContextParams = $userContext->getParameters();
     $this->assertArrayHasKey('roleIdList', $userContextParams);
     $this->assertSame(array($roleId1, $roleId2, $roleId3), $userContextParams['roleIdList']);
     $this->assertArrayHasKey('roleLimitationList', $userContextParams);
     $limitationIdentifierForRole2 = get_class($limitationForRole2);
     $limitationIdentifierForRole3 = get_class($limitationForRole3);
     $this->assertSame(array("{$roleId2}-{$limitationIdentifierForRole2}" => array('/1/2', '/1/2/43'), "{$roleId3}-{$limitationIdentifierForRole3}" => array('foo', 'bar')), $userContextParams['roleLimitationList']);
 }
Exemple #4
0
    public function testSetParameters()
    {
        $userContext = new UserContext();

        $userContext->addParameter('authenticated', true);
        $userContext->setParameters(array(
            'roles' => array('ROLE_USER'),
            'foo' => 'bar'
        ));

        $this->assertFalse($userContext->hasParameter('authenticated'));
        $this->assertTrue($userContext->hasParameter('foo'));
        $this->assertTrue($userContext->hasParameter('roles'));

        $parameters = array();
        foreach ($userContext as $name => $value) {
            $parameters[$name] = $value;
        }
        $this->assertEquals(
            array('roles' => array('ROLE_USER'), 'foo' => 'bar'),
            $parameters
        );
    }
 public function updateUserContext(UserContext $context)
 {
     $context->addParameter('ezpublish_identity', $this->generate());
 }
Exemple #6
0
 public function updateUserContext(UserContext $context)
 {
     $context->addParameter('foo', 'bar');
 }