/**
  * Returns list of role assignments for specific role and content.
  *
  * @param \eZ\Publish\API\Repository\Values\User\Role $role
  * @param int $contentId
  *
  * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[]
  */
 private function getRoleAssignmentsForRoleAndContent(Role $role, $contentId)
 {
     $contentService = $this->repository->getContentService();
     $contentTypeService = $this->repository->getContentTypeService();
     $userService = $this->repository->getUserService();
     $contentType = $contentTypeService->loadContentType($contentService->loadContent($contentId)->contentTypeId);
     if ($contentType->identifier != 'user_group' && $contentType->identifier != 'user') {
         throw new \ErrorException("Implementation error, unknown contentType '{$contentType->identifier}'.");
     }
     $roleAssignments = array();
     foreach ($this->roleLimitations as $limit) {
         if ($limit['roleId'] == $role->id && $limit['contentId'] == $contentId) {
             $limitIdentifier = $limit['identifier'];
             if (!isset($roleAssignments[$limitIdentifier])) {
                 if ('user_group' === $contentType->identifier) {
                     $roleAssignments[$limitIdentifier] = new UserGroupRoleAssignmentStub(array('role' => $role, 'userGroup' => $userService->loadUserGroup($contentId), 'limitation' => $this->getOptionalRoleLimitation($role->id, $contentId, $limitIdentifier)));
                 } else {
                     if ('user' === $contentType->identifier) {
                         $roleAssignments[$limitIdentifier] = new UserRoleAssignmentStub(array('role' => $role, 'user' => $userService->loadUser($contentId), 'limitation' => $this->getOptionalRoleLimitation($role->id, $contentId, $limitIdentifier)));
                     }
                 }
             }
         }
     }
     if (empty($roleAssignments)) {
         if ('user_group' === $contentType->identifier) {
             $roleAssignments[] = new UserGroupRoleAssignmentStub(array('role' => $role, 'userGroup' => $userService->loadUserGroup($contentId), 'limitation' => null));
         } else {
             if ('user' === $contentType->identifier) {
                 $roleAssignments[] = new UserRoleAssignmentStub(array('role' => $role, 'user' => $userService->loadUser($contentId), 'limitation' => null));
             }
         }
     }
     return array_values($roleAssignments);
 }
 /**
  * Updates the fields of a draft.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version
  * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentUpdateStruct is not valid
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set to an empty value
  *
  * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
  * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields
  */
 public function updateContent(VersionInfo $versionInfo, ContentUpdateStruct $contentUpdateStruct)
 {
     if (false === $this->repository->hasAccess('content', 'edit')) {
         throw new UnauthorizedExceptionStub('What error code should be used?');
     }
     if ($versionInfo->status !== VersionInfo::STATUS_DRAFT) {
         throw new BadStateExceptionStub('What error code should be used?');
     }
     $content = $this->loadContentByVersionInfo($versionInfo);
     $contentType = $this->repository->getContentTypeService()->loadContentType($content->contentInfo->contentTypeId);
     $initialLanguageCode = $contentUpdateStruct->initialLanguageCode;
     $mainLanguageCode = $versionInfo->getContentInfo()->mainLanguageCode;
     $oldAndNewFields = array_merge($content->fields, $contentUpdateStruct->fields);
     $languageCodes = $this->getLanguageCodes($oldAndNewFields, $initialLanguageCode);
     // Automatically overwrites old with new fields
     $fields = $this->getFieldsByTypeAndLanguageCode($contentType, $oldAndNewFields, $initialLanguageCode ?: $mainLanguageCode);
     // Validate all required fields available in each language
     $this->checkRequiredFields($contentType, $fields, $languageCodes, $mainLanguageCode);
     // Complete missing fields
     $allFields = $this->createCompleteFields($contentType, $fields, $languageCodes, $mainLanguageCode);
     // Perform some fake validation to emulate validation exceptions
     $this->fakeFieldValidation($contentType, $allFields);
     $draftedContent = new ContentStub(array('id' => $content->id, 'fields' => $allFields, 'contentTypeId' => $content->contentTypeId, 'versionNo' => $versionInfo->versionNo, 'repository' => $this->repository));
     $draftedVersionInfo = new VersionInfoStub(array('id' => $versionInfo->id, 'contentId' => $content->id, 'status' => $versionInfo->status, 'versionNo' => $versionInfo->versionNo, 'creatorId' => $versionInfo->creatorId, 'creationDate' => $versionInfo->creationDate, 'modificationDate' => new \DateTime(), 'languageCodes' => $languageCodes, 'initialLanguageCode' => $mainLanguageCode, 'names' => $this->generateNames($contentType, $allFields), 'repository' => $this->repository));
     $fieldDefinitions = $contentType->getFieldDefinitions();
     foreach ($allFields as $field) {
         foreach ($fieldDefinitions as $fieldDefinition) {
             if ($fieldDefinition->identifier === $field->fieldDefIdentifier) {
                 $this->pseudoExternalStorage->handleUpdate($fieldDefinition, $field, $draftedContent);
             }
         }
     }
     if (false === ($index = array_search($content, $this->content))) {
         throw new \ErrorException("An implementation error...");
     }
     $this->versionInfo[$versionInfo->id] = $draftedVersionInfo;
     $this->content[$index] = $draftedContent;
     return $draftedContent;
 }
 /**
  * Instantiate a user group create class
  *
  * @param string $mainLanguageCode The main language for the underlying content object
  * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType 5.x the content type for the underlying content object. In 4.x it is ignored and taken from the configuration
  *
  * @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct
  */
 public function newUserGroupCreateStruct($mainLanguageCode, $contentType = null)
 {
     $contentType = $contentType ?: $this->repository->getContentTypeService()->loadContentTypeByIdentifier('user_group');
     return new UserGroupCreateStructStub(array('mainLanguageCode' => $mainLanguageCode, 'contentType' => $contentType, 'remoteId' => md5(uniqid(__METHOD__, true))));
 }