eZ\Publish\Core\Repository\UserService::createUser PHP Method

createUser() public method

Create a new user. The created user is published by this method.
public createUser ( eZ\Publish\API\Repository\Values\User\UserCreateStruct $userCreateStruct, array $parentGroups ) : eZ\Publish\API\Repository\Values\User\User
$userCreateStruct eZ\Publish\API\Repository\Values\User\UserCreateStruct the data used for creating the user
$parentGroups array the groups which are assigned to the user after creation
return eZ\Publish\API\Repository\Values\User\User
    public function createUser(APIUserCreateStruct $userCreateStruct, array $parentGroups)
    {
        if (empty($parentGroups)) {
            throw new InvalidArgumentValue('parentGroups', $parentGroups);
        }
        if (!is_string($userCreateStruct->login) || empty($userCreateStruct->login)) {
            throw new InvalidArgumentValue('login', $userCreateStruct->login, 'UserCreateStruct');
        }
        if (!is_string($userCreateStruct->email) || empty($userCreateStruct->email)) {
            throw new InvalidArgumentValue('email', $userCreateStruct->email, 'UserCreateStruct');
        }
        if (!ezcMailTools::validateEmailAddress($userCreateStruct->email)) {
            throw new InvalidArgumentValue('email', $userCreateStruct->email, 'UserCreateStruct');
        }
        if (!is_string($userCreateStruct->password) || empty($userCreateStruct->password)) {
            throw new InvalidArgumentValue('password', $userCreateStruct->password, 'UserCreateStruct');
        }
        if (!is_bool($userCreateStruct->enabled)) {
            throw new InvalidArgumentValue('enabled', $userCreateStruct->enabled, 'UserCreateStruct');
        }
        try {
            $this->userHandler->loadByLogin($userCreateStruct->login);
            throw new InvalidArgumentException('userCreateStruct', 'User with provided login already exists');
        } catch (NotFoundException $e) {
            // Do nothing
        }
        $contentService = $this->repository->getContentService();
        $locationService = $this->repository->getLocationService();
        $contentTypeService = $this->repository->getContentTypeService();
        if ($userCreateStruct->contentType === null) {
            $userContentType = $contentTypeService->loadContentType($this->settings['userClassID']);
            $userCreateStruct->contentType = $userContentType;
        }
        $locationCreateStructs = array();
        foreach ($parentGroups as $parentGroup) {
            $parentGroup = $this->loadUserGroup($parentGroup->id);
            if ($parentGroup->getVersionInfo()->getContentInfo()->mainLocationId !== null) {
                $locationCreateStructs[] = $locationService->newLocationCreateStruct($parentGroup->getVersionInfo()->getContentInfo()->mainLocationId);
            }
        }
        // Search for the first ezuser field type in content type
        $userFieldDefinition = null;
        foreach ($userCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) {
            if ($fieldDefinition->fieldTypeIdentifier == 'ezuser') {
                $userFieldDefinition = $fieldDefinition;
                break;
            }
        }
        if ($userFieldDefinition === null) {
            throw new ContentValidationException('Provided content type does not contain ezuser field type');
        }
        $fixUserFieldType = true;
        foreach ($userCreateStruct->fields as $index => $field) {
            if ($field->fieldDefIdentifier == $userFieldDefinition->identifier) {
                if ($field->value instanceof UserValue) {
                    $userCreateStruct->fields[$index]->value->login = $userCreateStruct->login;
                } else {
                    $userCreateStruct->fields[$index]->value = new UserValue(array('login' => $userCreateStruct->login));
                }
                $fixUserFieldType = false;
            }
        }
        if ($fixUserFieldType) {
            $userCreateStruct->setField($userFieldDefinition->identifier, new UserValue(array('login' => $userCreateStruct->login)));
        }
        $this->repository->beginTransaction();
        try {
            $contentDraft = $contentService->createContent($userCreateStruct, $locationCreateStructs);
            // Create user before publishing, so that external data can be returned
            $spiUser = $this->userHandler->create(new SPIUser(array('id' => $contentDraft->id, 'login' => $userCreateStruct->login, 'email' => $userCreateStruct->email, 'passwordHash' => $this->createPasswordHash($userCreateStruct->login, $userCreateStruct->password, $this->settings['siteName'], $this->settings['hashType']), 'hashAlgorithm' => $this->settings['hashType'], 'isEnabled' => $userCreateStruct->enabled, 'maxLogin' => 0)));
            $publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
            $this->repository->commit();
        } catch (Exception $e) {
            $this->repository->rollback();
            throw $e;
        }
        return $this->buildDomainUserObject($spiUser, $publishedContent);
    }