Skip to content

Commit 333eade

Browse files
committed
After review 2
1 parent aa4b938 commit 333eade

File tree

4 files changed

+87
-75
lines changed

4 files changed

+87
-75
lines changed

.coderabbit.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: "en-US"
22
reviews:
33
profile: "chill"
44
high_level_summary: true
5+
summary: "top"
56
auto_review:
67
enabled: true
78
base_branches:

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
### Summary
2-
3-
Provide a general description of the code changes in your pull request …
4-
were there any bugs you had fixed? If so, mention them. If these bugs have open
5-
GitHub issues, be sure to tag them here as well, to keep the conversation
6-
linked together.
7-
81

92
### Unit test
103

@@ -17,7 +10,7 @@ You can run the existing unit tests using this command:
1710

1811
### Code style
1912

20-
Have you checked that you code is well-documented and follows the PSR-2 coding
13+
Have you checked that your code is well-documented and follows the PSR-2 coding
2114
style?
2215

2316
You can check for this using this command:
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

src/Migrations/Version20251103SeedInitialAdmin.php

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

0 commit comments

Comments
 (0)