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

createUser() public method

The created user and account are automatically added to their respective repositories and thus be persisted.
public createUser ( string $username, string $password, string $firstName, string $lastName, 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
$firstName string First name of the user to be created
$lastName string Last name of the user to be created
$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 created user instance
    public function createUser($username, $password, $firstName, $lastName, array $roleIdentifiers = null, $authenticationProviderName = null)
    {
        $user = new User();
        $name = new PersonName('', $firstName, '', $lastName, '', $username);
        $user->setName($name);
        return $this->addUser($username, $password, $user, $roleIdentifiers, $authenticationProviderName);
    }

Usage Example

 /**
  * Create a new user
  *
  * This command creates a new user which has access to the backend user interface.
  *
  * More specifically, this command will create a new user and a new account at the same time. The created account
  * is, by default, a Neos backend account using the the "Typo3BackendProvider" for authentication. The given username
  * will be used as an account identifier for that new account.
  *
  * If an authentication provider name is specified, the new account will be created for that provider instead.
  *
  * Roles for the new user can optionally be specified as a comma separated list. For all roles provided by
  * Neos, the role namespace "Neos.Neos:" can be omitted.
  *
  * @param string $username The username of the user to be created, used as an account identifier for the newly created account
  * @param string $password Password of the user to be created
  * @param string $firstName First name of the user to be created
  * @param string $lastName Last name of the user to be created
  * @param string $roles A comma separated list of roles to assign. Examples: "Editor, Acme.Foo:Reviewer"
  * @param string $authenticationProvider Name of the authentication provider to use for the new account. Example: "Typo3BackendProvider"
  * @return void
  */
 public function createCommand($username, $password, $firstName, $lastName, $roles = null, $authenticationProvider = null)
 {
     $user = $this->userService->getUser($username, $authenticationProvider);
     if ($user instanceof User) {
         $this->outputLine('The username "%s" is already in use', array($username));
         $this->quit(1);
     }
     try {
         if ($roles === null) {
             $user = $this->userService->createUser($username, $password, $firstName, $lastName, null, $authenticationProvider);
         } else {
             $roleIdentifiers = Arrays::trimExplode(',', $roles);
             $user = $this->userService->createUser($username, $password, $firstName, $lastName, $roleIdentifiers, $authenticationProvider);
         }
         $roleIdentifiers = array();
         foreach ($user->getAccounts() as $account) {
             /** @var Account $account */
             foreach ($account->getRoles() as $role) {
                 /** @var Role $role */
                 $roleIdentifiers[$role->getIdentifier()] = true;
             }
         }
         $roleIdentifiers = array_keys($roleIdentifiers);
         if (count($roleIdentifiers) === 0) {
             $this->outputLine('Created user "%s".', array($username));
             $this->outputLine('<b>Please note that this user currently does not have any roles assigned.</b>');
         } else {
             $this->outputLine('Created user "%s" and assigned the following role%s: %s.', array($username, count($roleIdentifiers) > 1 ? 's' : '', implode(', ', $roleIdentifiers)));
         }
     } catch (\Exception $exception) {
         $this->outputLine($exception->getMessage());
         $this->quit(1);
     }
 }
All Usage Examples Of Neos\Neos\Domain\Service\UserService::createUser