/** * Assigns role to user * * @param $userId * * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList */ public function assignRoleToUser($userId) { $roleAssignment = $this->inputDispatcher->parse(new Message(array('Content-Type' => $this->request->headers->get('Content-Type')), $this->request->getContent())); $user = $this->userService->loadUser($userId); $role = $this->roleService->loadRole($roleAssignment->roleId); try { $this->roleService->assignRoleToUser($role, $user, $roleAssignment->limitation); } catch (LimitationValidationException $e) { throw new BadRequestException($e->getMessage()); } $roleAssignments = $this->roleService->getRoleAssignmentsForUser($user); return new Values\RoleAssignmentList($roleAssignments, $user->id); }
/** * Assigns a role to the given user. * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid * * @param \eZ\Publish\API\Repository\Values\User\Role $role * @param \eZ\Publish\API\Repository\Values\User\User $user * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) */ public function assignRoleToUser(Role $role, User $user, RoleLimitation $roleLimitation = null) { $returnValue = $this->service->assignRoleToUser($role, $user, $roleLimitation); $this->signalDispatcher->emit(new AssignRoleToUserSignal(array('roleId' => $role->id, 'userId' => $user->id, 'roleLimitation' => $roleLimitation))); return $returnValue; }
/** * Assign a role to users and groups in the assignment array. * * <pre> * $assignments = array( * array( * 'type' => 'user', * 'ids' => array(user ids), * 'limitation' => array(limitations) * ) * ) * </pre> * * @param \eZ\Publish\API\Repository\Values\User\Role $role * @param \eZ\Publish\API\Repository\RoleService $roleService * @param \eZ\Publish\API\Repository\UserService $userService * @param array $assignments */ private function assignRole(Role $role, RoleService $roleService, UserService $userService, array $assignments) { foreach ($assignments as $assign) { switch ($assign['type']) { case 'user': foreach ($assign['ids'] as $userId) { $userId = $this->referenceResolver->resolveReference($userId); $user = $userService->loadUser($userId); if (!isset($assign['limitations'])) { $roleService->assignRoleToUser($role, $user); } else { foreach ($assign['limitations'] as $limitation) { $limitationObject = $this->createLimitation($roleService, $limitation); $roleService->assignRoleToUser($role, $user, $limitationObject); } } } break; case 'group': foreach ($assign['ids'] as $groupId) { $groupId = $this->referenceResolver->resolveReference($groupId); $group = $userService->loadUserGroup($groupId); if (!isset($assign['limitations'])) { // q: why are we swallowing exceptions here ? //try { $roleService->assignRoleToUserGroup($role, $group); //} catch (InvalidArgumentException $e) {} } else { foreach ($assign['limitations'] as $limitation) { $limitationObject = $this->createLimitation($roleService, $limitation); // q: why are we swallowing exceptions here ? //try { $roleService->assignRoleToUserGroup($role, $group, $limitationObject); //} catch (InvalidArgumentException $e) {} } } } break; } } }