Neos\Neos\Command\UserCommandController::createCommand PHP Method

createCommand() public method

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.
public createCommand ( string $username, string $password, string $firstName, string $lastName, string $roles = null, string $authenticationProvider = null ) : void
$username string The username of the user to be created, used as an account identifier for the newly created account
$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
$roles string A comma separated list of roles to assign. Examples: "Editor, Acme.Foo:Reviewer"
$authenticationProvider string 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);
        }
    }