Skip to content

Instantly share code, notes, and snippets.

@jpingo
Created September 17, 2013 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpingo/1217f008c73ae68900ec to your computer and use it in GitHub Desktop.
Save jpingo/1217f008c73ae68900ec to your computer and use it in GitHub Desktop.
Update User
<?php
namespace Cleverti\EZP21550Bundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateSelfCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('issue:update_self')
->setDescription('Set a user to update itself')
->addArgument('contentId', InputArgument::REQUIRED, 'The content object id for the user you want to update')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @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 );
$output->writeln( "User updated sucessfully " );
}
catch ( \Exception $e )
{
$output->writeln( $e->getMessage() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment