From ad92a4c434e84f0280371a9ef983a7ca4ec46b66 Mon Sep 17 00:00:00 2001 From: matthieu Date: Fri, 3 Apr 2026 13:12:24 +0200 Subject: [PATCH] =?UTF-8?q?feat=20:=20commande=20app:create-user=20pour=20?= =?UTF-8?q?cr=C3=A9er=20des=20utilisateurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Permet de créer un user en prod sans SQL : php bin/console app:create-user --admin Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Command/CreateUserCommand.php | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 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..7931d1d --- /dev/null +++ b/src/Command/CreateUserCommand.php @@ -0,0 +1,66 @@ +addArgument('username', InputArgument::REQUIRED, 'Username') + ->addArgument('password', InputArgument::REQUIRED, 'Plain text password') + ->addOption('admin', 'a', 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'); + + $existing = $this->em->getRepository(User::class)->findOneBy(['username' => $username]); + + if ($existing) { + $io->error(sprintf('User "%s" already exists.', $username)); + + return Command::FAILURE; + } + + $user = new User(); + $user->setUsername($username); + $user->setPassword($this->passwordHasher->hashPassword($user, $plainPassword)); + $user->setRoles($input->getOption('admin') ? ['ROLE_ADMIN'] : []); + + $this->em->persist($user); + $this->em->flush(); + + $io->success(sprintf('User "%s" created%s.', $username, $input->getOption('admin') ? ' (admin)' : '')); + + return Command::SUCCESS; + } +}