public function updateUser(APIUser $user, UserUpdateStruct $userUpdateStruct)
{
$loadedUser = $this->loadUser($user->id);
// We need to determine if we have anything to update.
// UserUpdateStruct is specific as some of the new content is in
// content update struct and some of it is in additional fields like
// email, password and so on
$doUpdate = false;
foreach ($userUpdateStruct as $propertyValue) {
if ($propertyValue !== null) {
$doUpdate = true;
break;
}
}
if (!$doUpdate) {
// Nothing to update, so we just quit
return $user;
}
if ($userUpdateStruct->email !== null) {
if (!is_string($userUpdateStruct->email) || empty($userUpdateStruct->email)) {
throw new InvalidArgumentValue('email', $userUpdateStruct->email, 'UserUpdateStruct');
}
if (!ezcMailTools::validateEmailAddress($userUpdateStruct->email)) {
throw new InvalidArgumentValue('email', $userUpdateStruct->email, 'UserUpdateStruct');
}
}
if ($userUpdateStruct->password !== null && (!is_string($userUpdateStruct->password) || empty($userUpdateStruct->password))) {
throw new InvalidArgumentValue('password', $userUpdateStruct->password, 'UserUpdateStruct');
}
if ($userUpdateStruct->enabled !== null && !is_bool($userUpdateStruct->enabled)) {
throw new InvalidArgumentValue('enabled', $userUpdateStruct->enabled, 'UserUpdateStruct');
}
if ($userUpdateStruct->maxLogin !== null && !is_int($userUpdateStruct->maxLogin)) {
throw new InvalidArgumentValue('maxLogin', $userUpdateStruct->maxLogin, 'UserUpdateStruct');
}
$contentService = $this->repository->getContentService();
if (!$this->repository->canUser('content', 'edit', $loadedUser)) {
throw new UnauthorizedException('content', 'edit');
}
$this->repository->beginTransaction();
try {
$publishedContent = $loadedUser;
if ($userUpdateStruct->contentUpdateStruct !== null) {
$contentDraft = $contentService->createContentDraft($loadedUser->getVersionInfo()->getContentInfo());
$contentDraft = $contentService->updateContent($contentDraft->getVersionInfo(), $userUpdateStruct->contentUpdateStruct);
$publishedContent = $contentService->publishVersion($contentDraft->getVersionInfo());
}
if ($userUpdateStruct->contentMetadataUpdateStruct !== null) {
$contentService->updateContentMetadata($publishedContent->getVersionInfo()->getContentInfo(), $userUpdateStruct->contentMetadataUpdateStruct);
}
$this->userHandler->update(new SPIUser(array('id' => $loadedUser->id, 'login' => $loadedUser->login, 'email' => $userUpdateStruct->email ?: $loadedUser->email, 'passwordHash' => $userUpdateStruct->password ? $this->createPasswordHash($loadedUser->login, $userUpdateStruct->password, $this->settings['siteName'], $this->settings['hashType']) : $loadedUser->passwordHash, 'hashAlgorithm' => $this->settings['hashType'], 'isEnabled' => $userUpdateStruct->enabled !== null ? $userUpdateStruct->enabled : $loadedUser->enabled, 'maxLogin' => $userUpdateStruct->maxLogin !== null ? (int) $userUpdateStruct->maxLogin : $loadedUser->maxLogin)));
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return $this->loadUser($loadedUser->id);
}