我有TYPO3版本7.6.18。我尝试按代码创建用户。如何添加用户组?我看到它需要创建用户组对象。
$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);
我认为用户没有添加到数据库,因为我没有设置用户组。请帮帮我)
我已更正了我的代码,但没有将用户添加到数据库:
$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);
答案 0 :(得分:3)
你差不多完成了。您需要UserGroupRepository
来检索所需的用户组并执行$userGroup->attach($yourUserGroup)
。
答案 1 :(得分:2)
尝试下面的内容。
$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);