From ef1c14f8da252885a57216b438838c0d4dba38b8 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Tue, 7 Apr 2026 15:09:21 +0200 Subject: [PATCH] feat : add app:create-user console command Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Command/CreateUserCommand.php | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/Command/CreateUserCommand.php diff --git a/src/Command/CreateUserCommand.php b/src/Command/CreateUserCommand.php new file mode 100644 index 0000000..ec5f407 --- /dev/null +++ b/src/Command/CreateUserCommand.php @@ -0,0 +1,62 @@ +addArgument('username', InputArgument::REQUIRED, 'Username') + ->addArgument('password', InputArgument::REQUIRED, 'Plain password') + ->addOption('admin', null, InputOption::VALUE_NONE, 'Grant ROLE_ADMIN') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $username = $input->getArgument('username'); + $plainPassword = $input->getArgument('password'); + + $user = new User(); + $user->setUsername($username); + $user->setPassword($this->passwordHasher->hashPassword($user, $plainPassword)); + + if ($input->getOption('admin')) { + $user->setRoles(['ROLE_ADMIN']); + } + + $this->em->persist($user); + $this->em->flush(); + + $io->success(sprintf('User "%s" created%s.', $username, $input->getOption('admin') ? ' with ROLE_ADMIN' : '')); + + return Command::SUCCESS; + } +}