How to create a new TYPO3 FE User in an Extbase-CommandController-Task

216 Views Asked by At

We need to synchronise users and therefore plan to pull data from a third party API with the TYPO3 scheduler that updates existing users and adds new users to the fe_users table.

Updating existing users works fine, but I don't know how to create a new user. Here is the code I currently use in TYPO3 v8. I don't get any error messages, but the new user is not added to the database.

/**
 * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
 */
protected $userRepository;

/**
 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
 */
protected $persistenceManager;

...

$user = $this->userRepository->findOneByForeignId($id);
if ($user) {
    $this->updateUserData($user);
    $this->userRepository->update($user);
} else {
    $user = new \TYPO3\CMS\Extbase\Domain\Model\FrontendUser();
    $this->updateUserData($user);
    $this->userRepository->add($user);
}
$this->persistenceManager->persistAll();

...

private function updateUserData(&$user)
{
    $user->setFirstName($this->firstName);
    $user->setLastName($this->lastName);
    ...
}

I couldn't find any example that creates a new object in a loop. They (here user models) are always injected into the controller action as a parameter.

2

There are 2 best solutions below

3
Bill.Dagou On BEST ANSWER

According to your code, I could not tell why the new user could not be created. But you can refer to the log, hopefully it will provide you some tips.

Generally, there could be 2 reasons. One is the pid. As you can update the existing users, I think it's not a problem. The other one is the validation. You can not insert a record if the SQL fails.

BTW, I have some similar extensions to do the same things, but not for fe_users table, and they all works.

1
Wolffc On

Are you shure you must synceonize all users? TYPO3 has a concept called authentication services and an authentication service. Which allow fetching an user while login.

This has multiple advantages:

  • instand access you don't need to wait for the next sync to run. Same for blocked users
  • privacy a user who never used the typo3 system has no data in it.
  • security:potential to avoid storing the users password hash in typo3

Only potential drawback login is not possible if the API is unreachable

See: https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Authentication/Index.html