2
votes

I have TYPO3 version 7.6.18. I trying create user by code. How to add user group? I see it need create user group object.

$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
            $newUser = $this->objectManager->get('Fhk\\Feusersplus\\Domain\\Repository\\UserRepository');

$userModel = $this->objectManager>get('Fhk\\Feusersplus\\Domain\\Model\\User');

$userGroup = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();

$userModel->setUsername("sdfdsfdsfs");
$userModel->setEmail($user_data['email']);
$gender = 0;
$password = rand(99999999,9999999999999);
$userModel->setPassword($password);

$userModel->setUsergroup($userGroup);
$newUser->add($userModel);

I think the user did't add to database because I did't set user group. Help me please)

I have corrected my code but it does't add user to database:

 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
            $newUser = $this->objectManager->get('Fhk\\Feusersplus\\Domain\\Repository\\UserRepository');

                $userModel = $this->objectManager->get('Fhk\\Feusersplus\\Domain\\Model\\User');
                $userGroup = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserGroupRepository');

                $userGroup = $userGroup->findByUid(1);
                $newUserGroup = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
                $newUserGroup->attach($userGroup);

            $userModel->setUsername("sdfdsfdsfs");
                $userModel->setEmail($user_data['email']);
                $gender = 0;
                $password = rand(99999999,9999999999999);
                $userModel->setPassword($password);
                $userModel->setPid(3);
                $userModel->setUsergroup($newUserGroup);

                $newUser->add($userModel);
2

2 Answers

4
votes

You are almost done. You need a UserGroupRepository to retrieve the user group you need and do $userGroup->attach($yourUserGroup).

2
votes

try something like below.

$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$newUser = $this->objectManager->get('Fhk\\Feusersplus\\Domain\\Repository\\UserRepository');

$userModel = $this->objectManager->get('Fhk\\Feusersplus\\Domain\\Model\\User');
$userGroup = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserGroupRepository');

$userGroup = $this->FrontendUserGroupRepository->findByUid(1);

$userModel = new \Typo3\CMS\Extbase\Domain\Model\FrontendUser();

$userModel->setUsername("sdfdsfdsfs");
$userModel->setEmail($user_data['email']);
$gender = 0;
$password = rand(99999999,9999999999999);
$userModel->setPassword($password);
$userModel->setPid(3);
$userModel->addUsergroup($userGroup);

$this->FrontendUserRepository->add($userModel);