|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Domain\Identity\Command; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use PhpList\Core\Domain\Identity\Model\Dto\CreateAdministratorDto; |
| 9 | +use PhpList\Core\Domain\Identity\Model\PrivilegeFlag; |
| 10 | +use PhpList\Core\Domain\Identity\Repository\AdministratorRepository; |
| 11 | +use PhpList\Core\Domain\Identity\Service\AdministratorManager; |
| 12 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +#[AsCommand( |
| 18 | + name: 'phplist:defaults:import', |
| 19 | + description: 'Imports default values into the database (e.g., default admin with all privileges).' |
| 20 | +)] |
| 21 | +class ImportDefaultsCommand extends Command |
| 22 | +{ |
| 23 | + private const DEFAULT_LOGIN = 'admin'; |
| 24 | + private const DEFAULT_EMAIL = '[email protected]'; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private readonly AdministratorRepository $administratorRepository, |
| 28 | + private readonly AdministratorManager $administratorManager, |
| 29 | + private readonly EntityManagerInterface $entityManager, |
| 30 | + ) { |
| 31 | + parent::__construct(); |
| 32 | + } |
| 33 | + |
| 34 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 35 | + { |
| 36 | + $login = self::DEFAULT_LOGIN; |
| 37 | + $email = self::DEFAULT_EMAIL; |
| 38 | + $envPassword = getenv('PHPLIST_ADMIN_PASSWORD'); |
| 39 | + $password = is_string($envPassword) && trim($envPassword) !== '' |
| 40 | + ? $envPassword |
| 41 | + : self::DEFAULT_LOGIN; |
| 42 | + |
| 43 | + $allPrivileges = $this->allPrivilegesGranted(); |
| 44 | + |
| 45 | + $existing = $this->administratorRepository->findOneBy(['loginName' => $login]); |
| 46 | + if ($existing === null) { |
| 47 | + $dto = new CreateAdministratorDto( |
| 48 | + loginName: $login, |
| 49 | + password: $password, |
| 50 | + email: $email, |
| 51 | + isSuperUser: true, |
| 52 | + privileges: $allPrivileges, |
| 53 | + ); |
| 54 | + $admin = $this->administratorManager->createAdministrator($dto); |
| 55 | + $this->entityManager->flush(); |
| 56 | + |
| 57 | + $output->writeln(sprintf( |
| 58 | + 'Default admin created: login="%s", email="%s", superuser=yes, privileges=all', |
| 59 | + $admin->getLoginName(), |
| 60 | + $admin->getEmail() |
| 61 | + )); |
| 62 | + } else { |
| 63 | + $output->writeln(sprintf( |
| 64 | + 'Default admin already exists: login="%s", email="%s"', |
| 65 | + $existing->getLoginName(), |
| 66 | + $email, |
| 67 | + )); |
| 68 | + } |
| 69 | + |
| 70 | + return Command::SUCCESS; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return array<string,bool> |
| 75 | + * @SuppressWarnings(PHPMD.StaticAccess) |
| 76 | + */ |
| 77 | + private function allPrivilegesGranted(): array |
| 78 | + { |
| 79 | + $all = []; |
| 80 | + foreach (PrivilegeFlag::cases() as $flag) { |
| 81 | + $all[$flag->value] = true; |
| 82 | + } |
| 83 | + return $all; |
| 84 | + } |
| 85 | +} |
0 commit comments