src/Command/AddUserCommand.php
/**
     * This method is executed after interact() and initialize(). It usually
     * contains the logic to execute to complete this command task.
     */
This method is executed after interact() and initialize(). It usually
contains the logic to execute to complete this command task.
This method is executed after interact () and initialize () . It usually contains the logic to execute to complete this command task .
protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('add-user-command');
        $username = $input->getArgument('username');
        $plainPassword = $input->getArgument('password');
        $email = $input->getArgument('email');
        $displayName = $input->getArgument('display-name');
        $isAdmin = $input->getOption('admin') ? ['ROLE_ADMIN'] : [];
        $isDeveloper = $input->getOption('developer') ? ['ROLE_DEVELOPER'] : [];
        $roles = array_unique(array_merge($input->getOption('roles'), $isAdmin, $isDeveloper));
        $user = UserRepository::factory($displayName, $username, $email);
        $user->setRoles($roles);
        $user->setLocale('en');
        $user->setBackendTheme('default');
        $user->setPlainPassword($plainPassword);
        $errors = $this->validator->validate($user);
        if ($errors->count() > 0) {
            $errorMessages = [];
            foreach ($errors as $error) {
                $errorMessages[] = $error->getMessage();
            }
            throw new RuntimeException(implode(', ', $errorMessages));
        }
        $hashedPassword = $this->passwordHasher->hashPassword($user, $user->getPlainPassword());
        $user->setPassword($hashedPassword);
        $user->eraseCredentials();
        $this->entityManager->persist($user);
        $this->entityManager->flush();
        $this->io->success(sprintf('User was successfully created: %s (%s) [%s]', $user->getUsername(), $user->getEmail(), implode(',', $user->getRoles())));
        $event = $stopwatch->stop('add-user-command');
        if ($output->isVerbose()) {
            $this->io->comment(sprintf('New user database id: %d / Elapsed time: %.2f ms / Consumed memory: %.2f MB', $user->getId(), $event->getDuration(), $event->getMemory() / (1024 ** 2)));
        }
        return Command::SUCCESS;
    }
protected function execute ( InputInterface $ input , OutputInterface $ output ) : int { $ stopwatch = new Stopwatch ( ) ; $ stopwatch -> start ( 'add-user-command' ) ; $ username = $ input -> getArgument ( 'username' ) ; $ plainPassword = $ input -> getArgument ( 'password' ) ; $ email = $ input -> getArgument ( 'email' ) ; $ displayName = $ input -> getArgument ( 'display-name' ) ; $ isAdmin = $ input -> getOption ( 'admin' ) ? [ 'ROLE_ADMIN' ] : [ ] ; $ isDeveloper = $ input -> getOption ( 'developer' ) ? [ 'ROLE_DEVELOPER' ] : [ ] ; $ roles = array_unique ( array_merge ( $ input -> getOption ( 'roles' ) , $ isAdmin , $ isDeveloper ) ) ; $ user = UserRepository :: factory ( $ displayName , $ username , $ email ) ; $ user -> setRoles ( $ roles ) ; $ user -> setLocale ( 'en' ) ; $ user -> setBackendTheme ( 'default' ) ; $ user -> setPlainPassword ( $ plainPassword ) ; $ errors = $ this -> validator -> validate ( $ user ) ; if ( $ errors -> count ( ) > 0 ) { $ errorMessages = [ ] ; foreach ( $ errors as $ error ) { $ errorMessages [ ] = $ error -> getMessage ( ) ; } throw new RuntimeException ( implode ( ', ' , $ errorMessages ) ) ; } $ hashedPassword = $ this -> passwordHasher -> hashPassword ( $ user , $ user -> getPlainPassword ( ) ) ; $ user -> setPassword ( $ hashedPassword ) ; $ user -> eraseCredentials ( ) ; $ this -> entityManager -> persist ( $ user ) ; $ this -> entityManager -> flush ( ) ; $ this -> io -> success ( sprintf ( 'User was successfully created: %s (%s) [%s]' , $ user -> getUsername ( ) , $ user -> getEmail ( ) , implode ( ',' , $ user -> getRoles ( ) ) ) ) ; $ event = $ stopwatch -> stop ( 'add-user-command' ) ; if ( $ output -> isVerbose ( ) ) { $ this -> io -> comment ( sprintf ( 'New user database id: %d / Elapsed time: %.2f ms / Consumed memory: %.2f MB' , $ user -> getId ( ) , $ event -> getDuration ( ) , $ event -> getMemory ( ) / ( 1024 ** 2 ) ) ) ; } return Command :: SUCCESS ; }
This method is executed after interact() and initialize().
This method is executed after interact () and initialize () .
// Always merge the admin/developer role when the parameter has been given // create the user and hash its password /** @var ConstraintViolationInterface $error */ // See https://symfony.com/doc/current/book/security.html#security-encoding-password