Skip to content

Instantly share code, notes, and snippets.

@arfaram
Created May 14, 2020 21:58
Show Gist options
  • Save arfaram/5035f755089ab0566abfaea847a020aa to your computer and use it in GitHub Desktop.
Save arfaram/5035f755089ab0566abfaea847a020aa to your computer and use it in GitHub Desktop.
CreateUserCommand for ezplatform 2.5 , help script if you delete unintentionally all users!!
<?php
//execute the script with: php bin/console ezplatform:createuser
// SEE and adapt everything marked as !!!!!!IMPORTANT!!!!!!!!
// set all required field in "ezcontentclass_attribute" to 0 , only for user attributes
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateUserCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('ezplatform:createuser');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$repository = $this->getContainer()->get('ezpublish.api.repository');
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$contentTypeService = $repository->getContentTypeService();
//!!!!!!!!!!!!!!!!!!IMPORTANT!!!!!!!!!!!!!
$contentType = $contentTypeService->loadContentTypeByIdentifier('user');
//default user contenttype identifier in ez is: user
$userService = $repository->getUserService();
$content = $repository->sudo(function ( $repository) use ($userService, $contentType ) {
//user:tom
//password:publish
$userCreateStruct = $userService->newUserCreateStruct(
'tom',
'me@ez.no',
'publish',
'eng-GB',
$contentType
);
//!!!!!!!!!!!!!!!!!!IMPORTANT!!!!!!!!!!!!!
$userGroup = $userService->loadUserGroup(12); //Adminstrator UserGroup contentId
$content = $userService->createUser($userCreateStruct, array($userGroup));
return $content;
});
$output->writeln("User created with ID:" . $content->getFieldValue('user_account')->contentId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment