-
-
Save jpingo/1217f008c73ae68900ec to your computer and use it in GitHub Desktop.
Update User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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