Neos\Neos\Domain\Service\UserService::addUser PHP Method

addUser() public method

This method basically "creates" a user like createUser() would, except that it does not create the User object itself. If you need to create the User object elsewhere, for example in your ActionController, make sure to call this method for registering the new user instead of adding it to the PartyRepository manually. This method also creates a new user workspace for the given user if no such workspace exist.
public addUser ( string $username, string $password, User $user, array $roleIdentifiers = null, string $authenticationProviderName = null ) : User
$username string The username of the user to be created.
$password string Password of the user to be created
$user Neos\Neos\Domain\Model\User The pre-built user object to start with
$roleIdentifiers array A list of role identifiers to assign
$authenticationProviderName string Name of the authentication provider to use. Example: "Typo3BackendProvider"
return Neos\Neos\Domain\Model\User The same user object
    public function addUser($username, $password, User $user, array $roleIdentifiers = null, $authenticationProviderName = null)
    {
        if ($roleIdentifiers === null) {
            $roleIdentifiers = array('Neos.Neos:Editor');
        }
        $roleIdentifiers = $this->normalizeRoleIdentifiers($roleIdentifiers);
        $account = $this->accountFactory->createAccountWithPassword($username, $password, $roleIdentifiers, $authenticationProviderName ?: $this->defaultAuthenticationProviderName);
        $this->partyService->assignAccountToParty($account, $user);
        $this->partyRepository->add($user);
        $this->accountRepository->add($account);
        $this->createPersonalWorkspace($user, $account);
        $this->emitUserCreated($user);
        return $user;
    }

Usage Example

 /**
  * Create a new user
  *
  * @param string $username The user name (ie. account identifier) of the new user
  * @param array $password Expects an array in the format array('<password>', '<password confirmation>')
  * @param User $user The user to create
  * @param array $roleIdentifiers A list of roles (role identifiers) to assign to the new user
  * @Flow\Validate(argumentName="username", type="\Neos\Flow\Validation\Validator\NotEmptyValidator")
  * @Flow\Validate(argumentName="username", type="\Neos\Neos\Validation\Validator\UserDoesNotExistValidator")
  * @Flow\Validate(argumentName="password", type="\Neos\Neos\Validation\Validator\PasswordValidator", options={ "allowEmpty"=0, "minimum"=1, "maximum"=255 })
  * @return void
  */
 public function createAction($username, array $password, User $user, array $roleIdentifiers)
 {
     $this->userService->addUser($username, $password[0], $user, $roleIdentifiers);
     $this->addFlashMessage('The user "%s" has been created.', 'User created', Message::SEVERITY_OK, array(htmlspecialchars($username)), 1416225561);
     $this->redirect('index');
 }