Uploaded image for project: 'eZ Publish / Platform'
  1. eZ Publish / Platform
  2. EZP-21550

Changing owner, clearing cache needed for Owner(Self) to work

    XMLWordPrintable

Details

    Description

      Assume User A, for which the content object was created by the Administrator User. Now, for a policy with an Owner(Self) to be taken into account, it's necessary to perform a "chowning" operation - setting the user object for User A as its own Owner:

      $repository->setCurrentUser( $adminUser ); 
             
      $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
      $contentMetadataUpdateStruct->ownerId = $user->id;
      $contentService->updateContentMetadata( $user->getVersionInfo()>getContentInfo(), $contentMetadataUpdateStruct );
              
      $repository->setCurrentUser( $user );
      

      Now, if one wishes to have the user object (User A) edit itself, it's expected that (given that the user has the correct policies setup), it would work, but instead an error is triggered:

      User does not have access to 'edit' 'content'
      

      Clearing the cache (ezpublish/console cache:clear) and running the code once again works as expected.

      Full code:

              /** @var $repository \eZ\Publish\API\Repository\Repository */
              $repository = $this->getContainer()->get( 'ezpublish.api.repository' );
              $contentService = $repository->getContentService();
              $userService = $repository->getUserService();
              $locationService = $repository->getLocationService();
              $contentTypeService = $repository->getContentTypeService();
      
              $userId    = $input->getArgument( "contentId" );
              $adminUser = $userService->loadUser( 14);
              $user      = $userService->loadUser( $userId );
      
              // "chown" the user
              $repository->setCurrentUser( $adminUser );        
              $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
              $contentMetadataUpdateStruct->ownerId = $user->id;
              $contentService->updateContentMetadata( $user->getVersionInfo()->getContentInfo(), $contentMetadataUpdateStruct );
              
              $repository->setCurrentUser( $user );
      
              try
              {
                  $userUpdateStruct = $userService->newUserUpdateStruct();
                  $userUpdateStruct->contentUpdateStruct = $contentService->newContentUpdateStruct();
      
                  $userUpdateStruct->contentUpdateStruct->setField( 'first_name', 'test' );
                  $userUpdateStruct->contentUpdateStruct->setField( 'last_name', 'test' );
      
                  $user = $userService->updateUser( $user, $userUpdateStruct );
              }
              catch ( \Exception $e )
              {
                  $output->writeln( $e->getMessage() );
              }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            Filipe.Dobreira-obsolete@ez.no Filipe Dobreira (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 5 hours
                5h