eZ Platform Discussions

Curent user self edit


#1

Bonjour,

I have a problem with auto editing user account.

In a service, I have this piece of code that changes the current user’s account.

    $user = $this->repository->getCurrentUser();
    $userContent = $contentService->loadContentByContentInfo($user->contentInfo);
    $draft = $contentService->createContentDraft($userContent->contentInfo);
    $struct = $contentService->newContentUpdateStruct();
    $struct->setField('my_field', $my_new_value);
    $contentService->updateContent($draft->versionInfo, $struct);
    $contentService->publishVersion($draft->versionInfo);

And in response I have the following error 500:

User does not have access to ‘edit’ ‘content’ with: contentId '11290’
500 Internal Server Error - UnauthorizedException

However, my user has the right to “user/selfedit”.

On the other hand I do not see how to give him the right of edition on the contents of type user but only himself.

By the way, I would like to skip the rights management at the service level…


#2

Got it !

        $adminUser = $this->repository->getUserService()->loadUser(14);
        $currentUser = $this->repository->getUserService()->loadUser($userContent->id);
        $this->repository->setCurrentUser($adminUser);

        $draft = $contentService->createContentDraft($userContent->contentInfo);
        $struct = $contentService->newContentUpdateStruct();
        $struct->setField('popin_accepted', $value);
        $contentService->updateContent($draft->versionInfo, $struct);
        $contentService->publishVersion($draft->versionInfo);

        $this->repository->setCurrentUser($currentUser);

#3

This is not recommended. try to use this:

$userService = $this->repository->getUserService()
$userId = $userContent->id;

$content = $this->getRepository()->sudo(function () use ($userService, $userId ) {
    $currentUser = $userService->loadUser($userContent->id);
    // do your stuff here
    return $contentService->publishVersion($draft->versionInfo);
  });

// dump($content)

btw following is deprecated:

$adminUser = $this->repository->getUserService()->loadUser(14);
$this->repository->setCurrentUser($adminUser);

The new implementation is:

$this->getRepository()->getPermissionResolver()->setCurrentUserReference( $this->getRepository()->getUserService()->loadUser( 14 ) );

if you use it in Commands for example :wink:


#4

@remy_php, I agree with @da_real, he has the good answer.


#5

Hi @Plopix, Yes @da_real has the good answer.

The only thing that bothers me is that my IDE (phpStorm) tells me that $this->getRepository()->sudo doesn’t exist.

Finally, Subsidiary Question, in this example is the transaction managed in the right place?

        $repository->beginTransaction();

        $contentCreateStruct = $data->payload;
        $locationCreateStruct = $locationService->newLocationCreateStruct( $location->id );

        /** @var Content $submitedContent */
        $submitedContent = $this->getRepository()->sudo(function () use ($contentService, $contentCreateStruct, $locationCreateStruct ) {
            $contentDraft = $contentService->createContent($contentCreateStruct, array( $locationCreateStruct ));
            return $contentService->publishVersion( $contentDraft->versionInfo );
        });

        $repository->commit();

Merci.


#6

Hello @remy_php

The only thing that bothers me is that my IDE (phpStorm) tells me that $this->getRepository()->sudo doesn’t exist.

It is because you receive the interface eZ\Publish\API\Repository\Repository when sudo is on the implementation: eZ\Publish\Core\Repository\Repository
You can make PHPStorm happy with @var (see below)

About the transactions, I don’t think you need them as createContent and publishVersion use it already.
Also, when you are using transactions, you should have a try catch and a ->rollback somewhere.

This code is enough:

    $contentCreateStruct = $data->payload;
    $locationCreateStruct = $locationService->newLocationCreateStruct( $location->id );

    /** @var \eZ\Publish\Core\Repository\Repository $repository  */
    $repository = $this->getRepository();
    
    /** @var Content $submitedContent */
    $submitedContent = $repository->sudo(function (Repository $repository) use ($contentCreateStruct, $locationCreateStruct ) {
        $contentDraft = $repository->getContentService()->createContent($contentCreateStruct, [ $locationCreateStruct ]);
        return $repository->getContentService()->publishVersion( $contentDraft->versionInfo );
    });

++