Ejemplo n.º 1
0
 /**
  * Updates a user
  *
  * 4.x: If the versionUpdateStruct is set in the user update structure, this method internally creates a content draft, updates ts with the provided data
  * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods.
  *
  * @param \eZ\Publish\API\Repository\Values\User\User $user
  * @param \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update the user
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userUpdateStruct is not valid
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty
  *
  * @return \eZ\Publish\API\Repository\Values\User\User
  */
 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);
 }