Creation of a user in Ibexa

In our development process, there are occasions where we need to create a user or perform an update.

I’m sharing an example here of creating and modifying an Ibexa user.

The code is easy to understand. In summary:

  1. Load the user group using a method available in the Creation of a user in Ibexa.
  2. Once the group is found, you can create what we call a structure, always using methods from the Creation of a user in Ibexa.
  3. Fill in the data.
  4. Create the user
    public function createUser(array $userInfo)
    {
        try {
            $userService = $this->repository->getUserService();
            $contentTypeService = $this->repository->getContentTypeService();

            $userGroup   = $this->repository->sudo(
                function () use ($userService) {
                    return $userService->loadUserGroupByRemoteId('REMOTE');
                }
            );

            if ($userGroup) {
                $createStruct = $userService->newUserCreateStruct(
                    $userInfo['login'],
                    $userInfo['email'],
                    md5($userInfo['login']),
                    self::MAIN_LANGUAGE,
                    $contentTypeService->loadContentTypeByIdentifier('user')
            )
                );
                // Create Field User
                $createStruct->setField('first_name', trim($userInfo['first_name']));
                $createStruct->setField('second_name', trim($userInfo['second_name']));
                // Create  User
                return $this->repository->sudo(
                    function () use ($userService, $createStruct, $userGroup) {
                        return $userService->createUser($createStruct, [$userGroup]);
                    }
                );
            }
        } catch (\Exception $exception) {
            $this->logger->error('Error when create user in Ibexa '.$exception->getTraceAsString());

            return false;
        }
    }

Another example of an update

public function updateUser($user, $userInfo)
    {
        try {
            $userService = $this->repository->getUserService();

            $updateStruct                      = $userService->newUserUpdateStruct();
            $updateStruct->contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();

            // Update Field user
            $updateStruct->email = $userInfo['email'];
            $updateStruct->contentUpdateStruct->setField('first_name', trim($userInfo['first_name']));
            $updateStruct->contentUpdateStruct->setField('second_name', trim($userInfo['second_name']));

            // Update Field User
            return $this->repository->sudo(
                function () use ($userService, $updateStruct, $user) {
                    return $userService->updateUser($user, $updateStruct);
                }
            );
        } catch (\Exception $exception) {
            $this->logger->error('Error when update user in Ibexa '.$exception->getTraceAsString());

            return false;
        }
    }

We have seen two examples that consume Ibexa’s Public API.

The given example is for Ibexa 3.3.

More examples and code will be shared under the Ibexa category.