Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli account creation #32

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 19 additions & 38 deletions src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
135 changes: 135 additions & 0 deletions src/Command/MemberCreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace App\Command;

use App\Entity\Member;
use App\Enum\MemberRole;
use App\Repository\MemberRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Validator\Validator\ValidatorInterface;

#[AsCommand(name: 'member:create', description: 'Create a member')]
class MemberCreateCommand extends Command {
private SymfonyStyle $io;

public function __construct(
private readonly EntityManagerInterface $em,
private readonly MemberRepository $memberRepository,
private readonly ValidatorInterface $validator,
) {
parent::__construct();
}

protected function configure(): void {
$this->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éé.');
}

}