Skip to content

Commit 8513761

Browse files
committed
Update: Use command for initial admin insert
1 parent 2b018a8 commit 8513761

File tree

2 files changed

+100
-49
lines changed

2 files changed

+100
-49
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\Helper\QuestionHelper;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Question\Question;
18+
19+
#[AsCommand(
20+
name: 'phplist:defaults:import',
21+
description: 'Imports default values into the database (e.g., default admin with all privileges).'
22+
)]
23+
class ImportDefaultsCommand extends Command
24+
{
25+
private const DEFAULT_LOGIN = 'admin';
26+
private const DEFAULT_EMAIL = '[email protected]';
27+
28+
public function __construct(
29+
private readonly AdministratorRepository $administratorRepository,
30+
private readonly AdministratorManager $administratorManager,
31+
private readonly EntityManagerInterface $entityManager,
32+
) {
33+
parent::__construct();
34+
}
35+
36+
protected function execute(InputInterface $input, OutputInterface $output): int
37+
{
38+
$login = self::DEFAULT_LOGIN;
39+
$email = self::DEFAULT_EMAIL;
40+
$envPassword = getenv('PHPLIST_ADMIN_PASSWORD');
41+
$envPassword = is_string($envPassword) && trim($envPassword) !== '' ? $envPassword : null;
42+
43+
$allPrivileges = $this->allPrivilegesGranted();
44+
45+
$existing = $this->administratorRepository->findOneBy(['loginName' => $login]);
46+
if ($existing === null) {
47+
// If creating the default admin, require a password. Prefer env var, else prompt for input.
48+
$password = $envPassword;
49+
if ($password === null) {
50+
/** @var QuestionHelper $helper */
51+
$helper = $this->getHelper('question');
52+
$question = new Question('Enter password for default admin (login "admin"): ');
53+
$question->setHidden(true);
54+
$question->setHiddenFallback(false);
55+
$password = (string) $helper->ask($input, $output, $question);
56+
if (trim($password) === '') {
57+
$output->writeln('<error>Password must not be empty.</error>');
58+
return Command::FAILURE;
59+
}
60+
}
61+
62+
$dto = new CreateAdministratorDto(
63+
loginName: $login,
64+
password: $password,
65+
email: $email,
66+
isSuperUser: true,
67+
privileges: $allPrivileges,
68+
);
69+
$admin = $this->administratorManager->createAdministrator($dto);
70+
$this->entityManager->flush();
71+
72+
$output->writeln(sprintf(
73+
'Default admin created: login="%s", email="%s", superuser=yes, privileges=all',
74+
$admin->getLoginName(),
75+
$admin->getEmail()
76+
));
77+
} else {
78+
$output->writeln(sprintf(
79+
'Default admin already exists: login="%s", email="%s"',
80+
$existing->getLoginName(),
81+
$email,
82+
));
83+
}
84+
85+
return Command::SUCCESS;
86+
}
87+
88+
/**
89+
* @return array<string,bool>
90+
* @SuppressWarnings(PHPMD.StaticAccess)
91+
*/
92+
private function allPrivilegesGranted(): array
93+
{
94+
$all = [];
95+
foreach (PrivilegeFlag::cases() as $flag) {
96+
$all[$flag->value] = true;
97+
}
98+
return $all;
99+
}
100+
}

src/Migrations/Version20251103SeedInitialAdmin.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)