/**
  * Make sure a User with name $username, $email and $password exists in parent group.
  *
  * @param string $username User name
  * @param string $email User's email
  * @param string $password User's password
  * @param string $parentGroupName (optional) name of the parent group to check
  *
  * @return \eZ\Publish\API\Repository\Values\User\User
  */
 public function ensureUserExists($username, $email, $password, $parentGroupName = null)
 {
     if ($parentGroupName) {
         $parentSearchHits = $this->searchUserGroups($parentGroupName);
         // Found matching Group(s)
         if (!empty($parentSearchHits)) {
             $firstGroupId = $parentSearchHits[0]->valueObject->contentInfo->id;
             foreach ($parentSearchHits as $userGroupHit) {
                 $groupId = $userGroupHit->valueObject->contentInfo->id;
                 // Search for user in this group
                 $user = $this->searchUserByLogin($username, $groupId);
                 if ($user) {
                     return $user;
                 }
             }
             // create user inside existing parent Group, use first group found
             $parentGroup = $this->userService->loadUserGroup($firstGroupId);
             return $this->createUser($username, $email, $password, $parentGroup);
         }
         // else
         // Parent Group does not exist yet, so create it at "root" User Group.
         $rootGroup = $this->userService->loadUserGroup(self::USERGROUP_ROOT_CONTENT_ID);
         $parentGroup = $this->createUserGroup($parentGroupName, $rootGroup);
         return $this->createUser($username, $email, $password, $parentGroup);
     }
     // else,
     $user = $this->searchUserByLogin($username);
     if (!$user) {
         $user = $this->createUser($username, $email, $password);
     }
     return $user;
 }
Beispiel #2
0
 /**
  * Assigns the user to a user group
  *
  * @param $userId
  *
  * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
  * @return \eZ\Publish\Core\REST\Server\Values\UserGroupRefList
  */
 public function assignUserToUserGroup($userId)
 {
     $user = $this->userService->loadUser($userId);
     try {
         $userGroupLocation = $this->locationService->loadLocation($this->extractLocationIdFromPath($this->request->query->get('group')));
     } catch (APINotFoundException $e) {
         throw new Exceptions\ForbiddenException($e->getMessage());
     }
     try {
         $userGroup = $this->userService->loadUserGroup($userGroupLocation->contentId);
     } catch (APINotFoundException $e) {
         throw new Exceptions\ForbiddenException($e->getMessage());
     }
     try {
         $this->userService->assignUserToUserGroup($user, $userGroup);
     } catch (InvalidArgumentException $e) {
         throw new Exceptions\ForbiddenException($e->getMessage());
     }
     $userGroups = $this->userService->loadUserGroupsOfUser($user);
     $restUserGroups = array();
     foreach ($userGroups as $userGroup) {
         $userGroupContentInfo = $userGroup->getVersionInfo()->getContentInfo();
         $userGroupLocation = $this->locationService->loadLocation($userGroupContentInfo->mainLocationId);
         $contentType = $this->contentTypeService->loadContentType($userGroupContentInfo->contentTypeId);
         $restUserGroups[] = new Values\RestUserGroup($userGroup, $contentType, $userGroupContentInfo, $userGroupLocation, $this->contentService->loadRelations($userGroup->getVersionInfo()));
     }
     return new Values\UserGroupRefList($restUserGroups, $this->router->generate('ezpublish_rest_loadUserGroupsOfUser', array('userId' => $userId)), $userId);
 }
 /**
  * @param $data
  * @return array
  */
 private function getParentGroups(&$data)
 {
     $userGroupIds = $this->objectCollection->getList('content_items', $data['groups']);
     $userGroups = [];
     foreach ($userGroupIds as $userGroupId) {
         $userGroups[] = $this->userService->loadUserGroup($userGroupId);
     }
     return $userGroups;
 }
 /**
  * @Given /^there is a User Content$/
  */
 public function thereIsAUserContent()
 {
     $login = '******' . microtime(true);
     $email = $login . '@example.com';
     $struct = $this->userService->newUserCreateStruct($login, $email, 'publish', 'eng-GB');
     $struct->setField('first_name', 'John');
     $struct->setField('last_name', 'Doe');
     $parentGroup = $this->userService->loadUserGroup(11);
     $user = $this->userService->createUser($struct, [$parentGroup]);
     $this->currentContent = $this->contentService->loadContentByContentInfo($user->contentInfo);
 }
 /**
  * @inheritdoc
  */
 public function visit(TreeNodeInterface $node, &$data)
 {
     if (!is_array($data)) {
         return null;
     }
     $struct = $this->userService->newUserGroupCreateStruct('');
     $this->fillValueObject($struct, $data, ['content_type']);
     $defaultUserGroup = $this->userService->loadUserGroup(self::DEFAULT_USER_GROUP_ID);
     // @todo: process parent user group
     $userGroup = $this->userService->createUserGroup($struct, $defaultUserGroup);
     $this->saveUserGroupDataToCollection($node, $userGroup);
     if (isset($data['roles'])) {
         foreach ($data['roles'] as $roleId) {
             $role = $this->repository->getRoleService()->loadRoleByIdentifier($roleId);
             // @doc: parameter RoleLimitation is not supported.
             // Not possible to assign role to user or group with limitation
             $this->repository->getRoleService()->assignRoleToUserGroup($role, $userGroup);
         }
     }
     return $userGroup;
 }
Beispiel #6
0
 /**
  * Returns a role assignment to the given user group
  *
  * @param $groupPath
  * @param $roleId
  *
  * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException
  * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment
  */
 public function loadRoleAssignmentForUserGroup($groupPath, $roleId)
 {
     $groupLocationParts = explode('/', $groupPath);
     $groupLocation = $this->locationService->loadLocation(array_pop($groupLocationParts));
     $userGroup = $this->userService->loadUserGroup($groupLocation->contentId);
     $roleAssignments = $this->roleService->getRoleAssignmentsForUserGroup($userGroup);
     foreach ($roleAssignments as $roleAssignment) {
         if ($roleAssignment->getRole()->id == $roleId) {
             return new Values\RestUserGroupRoleAssignment($roleAssignment, $groupPath);
         }
     }
     throw new Exceptions\NotFoundException("Role assignment not found: '{$this->request->getPathInfo()}'.");
 }
 /**
  * Loads a user group for the given id
  *
  * @param mixed $id
  *
  * @return \eZ\Publish\API\Repository\Values\User\UserGroup
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the user group with the given id was not found
  */
 public function loadUserGroup($id)
 {
     return $this->service->loadUserGroup($id);
 }
 /**
  * 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;
         }
     }
 }