From 5a1b3044a40ebd3da6208b9735be1c5cf57e494e Mon Sep 17 00:00:00 2001 From: Benoit VIGNAL Date: Mon, 5 Aug 2024 16:28:45 +0200 Subject: [PATCH] Add cli account creation --- src/Command/InstallCommand.php | 57 ++++-------- src/Command/MemberCreateCommand.php | 135 ++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 38 deletions(-) create mode 100644 src/Command/MemberCreateCommand.php diff --git a/src/Command/InstallCommand.php b/src/Command/InstallCommand.php index d65a995..9f35c96 100644 --- a/src/Command/InstallCommand.php +++ b/src/Command/InstallCommand.php @@ -19,7 +19,7 @@ use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -#[AsCommand(name: 'install', description: 'Create the bare default environment',)] +#[AsCommand(name: 'install', description: 'Create the bare default environment')] class InstallCommand extends Command { private SymfonyStyle $io; @@ -104,35 +104,14 @@ private function generateJwtKeys(): void { private function createAdminAccount(): void { $this->io->section("Création d'un compte administrateur"); - $adminEmail = $this->io->askQuestion(new Question("Adresse mail administrateur", "admin@admin.com")); - - $dbAmin = $this->memberRepository->findOneByEmail($adminEmail); - - if ($dbAmin) { - $this->io->info("Email déjà existant, création du compte administrateur ignoré"); - return; - } - - $pwdQuestion = new Question("Mot de passe"); - $pwdQuestion->setHidden(true); - $pwdQuestion->setValidator(function (?string $value): string { - if (empty($value)) { - throw new \Exception("Mot de passe invalide"); - } - return $value; - }); - - $pwd = $this->io->askQuestion($pwdQuestion); - $adminMember = new Member(); - $adminMember->setFirstname("admin") - ->setLastname("admin") - ->setEmail($adminEmail) - ->setPlainPassword($pwd) - ->setRole(MemberRole::admin) - ->setAccountActivated(true); - - $this->em->persist($adminMember); - $this->em->flush(); + $command = new ArrayInput([ + 'command' => 'member:create', + '--licence' => 'null', + '--role' => MemberRole::admin->value, + '--firstname' => 'Admin', + '--lastname' => 'ADMIN', + ]); + $this->getApplication()->doRun($command, $this->io); } private function createBadgerAccount(): void { @@ -144,14 +123,16 @@ private function createBadgerAccount(): void { return; } - $badgerMember = new Member(); - $badgerMember->setFirstname("badger") - ->setLastname("badger") - ->setEmail("badger") - ->setPlainPassword("badger") - ->setRole(MemberRole::badger); - $this->em->persist($badgerMember); - $this->em->flush(); + $command = new ArrayInput([ + 'command' => 'member:create', + '--licence' => 'null', + '--role' => MemberRole::badger->value, + '--email' => 'badger', + '--password' => 'badger', + '--firstname' => 'Badger', + '--lastname' => 'badger', + ]); + $this->getApplication()->doRun($command, $this->io); } private function generateBadgerLoginToken(): void { diff --git a/src/Command/MemberCreateCommand.php b/src/Command/MemberCreateCommand.php new file mode 100644 index 0000000..2b758b1 --- /dev/null +++ b/src/Command/MemberCreateCommand.php @@ -0,0 +1,135 @@ +addOption('email', null,InputOption::VALUE_OPTIONAL, 'Email'); + $this->addOption('password', null,InputOption::VALUE_OPTIONAL, 'Mot de passe'); + $this->addOption('firstname', null,InputOption::VALUE_OPTIONAL, 'Prénom'); + $this->addOption('lastname', null,InputOption::VALUE_OPTIONAL, 'Nom'); + $this->addOption('role', null,InputOption::VALUE_OPTIONAL, 'Rôle. Valeurs possibles : ' . implode(', ', array_column(MemberRole::cases(), 'value'))); + $this->addOption('licence', null,InputOption::VALUE_OPTIONAL, 'Licence. Écrire `null` pour ne pas en définir'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $this->io = new SymfonyStyle($input, $output); + + // Field are not defined + $email = $input->getOption('email'); + if (!$email) { + $email = $this->io->askQuestion(new Question("Adresse mail", "admin@admin.com")); + } + + $dbMember = $this->memberRepository->findOneByEmail($email); + if ($dbMember) { + $this->io->info("Email déjà existant, création du compte ignoré."); + return Command::INVALID; + } + + $password = $input->getOption('password'); + if (!$password) { + $pwdQuestion = new Question("Mot de passe"); + $pwdQuestion->setHidden(true); + $pwdQuestion->setValidator(function (?string $value): string { + if (empty($value)) { + throw new \Exception("Mot de passe invalide"); + } + return $value; + }); + + $password = $this->io->askQuestion($pwdQuestion); + } + + $firstname = $input->getOption('firstname'); + if (!$firstname) { + $question = new Question("Prénom"); + $question->setValidator(function (?string $value): string { + if (empty($value)) { + throw new \Exception("Champ requis"); + } + return $value; + }); + $firstname = $this->io->askQuestion($question); + } + + $lastname = $input->getOption('lastname'); + if (!$lastname) { + $question = new Question("Nom"); + $question->setValidator(function (?string $value): string { + if (empty($value)) { + throw new \Exception("Champ requis"); + } + return $value; + }); + $lastname = $this->io->askQuestion($question); + } + + $role = $input->getOption('role'); + if (!$role) { + $role = $this->io->askQuestion(new Question("Rôle. Valeurs possibles : " . implode(', ', array_column(MemberRole::cases(), 'value')), MemberRole::admin->value)); + } + $role = MemberRole::tryFrom($role) ?? MemberRole::admin; + + $licence = $input->getOption('licence'); + if (!$licence) { + $licence = $this->io->askQuestion(new Question("'Licence. Écrire `null` ou faire Entrer pour ne pas en définir'")); + } + if ($licence === 'null') { + $licence = null; + } + + $this->createAccount($email, $password, $firstname, $lastname, $role, $licence); + + return Command::SUCCESS; + } + + private function createAccount(string $email, string $password, string $firstname, string $lastname, MemberRole $role, ?string $licence = null): void { + $member = new Member(); + $member + ->setFirstname($firstname) + ->setLastname($lastname) + ->setEmail($email) + ->setLicence($licence) + ->setPlainPassword($password) + ->setRole($role) + ->setAccountActivated(true); + + $errors = $this->validator->validate($member); + if (count($errors) > 0) { + $this->io->error('Erreur lors la création du compte'); + $this->io->error((string) $errors); + return; + } + + $this->em->persist($member); + $this->em->flush(); + + $this->io->success('Compte créé.'); + } + +}