- Symfony 8.1 webapp skeleton on PHP 8.5 (AssetMapper/importmap, no Node)
- Form-login gate: User entity, security.yaml, SecurityController,
app:user:create command, initial migration (user + messenger_messages)
- Bootstrap 5 + DataTables layout (base/login/home templates); any
table.datatable is auto-initialised
- trusted_proxies/headers wired for the shared Nginx Proxy Manager
- Docker (servers only): docker-compose.yml (php:8.5-fpm-alpine,
nginx:1.30-alpine, mariadb:12.3; host ports 8085/3310; external
docker_default network) + docker/{Dockerfile,php.ini,nginx,setup.sh,restart.sh}
- .env with CHANGEME placeholders (no secrets), .gitattributes enforces
LF so the deploy scripts stay runnable on Linux
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
85 lines
2.7 KiB
PHP
85 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Entity\User;
|
|
use App\Repository\UserRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
#[AsCommand(
|
|
name: 'app:user:create',
|
|
description: 'Create a dmtools user (there is no self-registration).',
|
|
)]
|
|
class CreateUserCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $em,
|
|
private readonly UserRepository $users,
|
|
private readonly UserPasswordHasherInterface $hasher,
|
|
private readonly ValidatorInterface $validator,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('email', InputArgument::REQUIRED, 'Login email')
|
|
->addArgument('password', InputArgument::OPTIONAL, 'Password (prompted if omitted)')
|
|
->addOption('admin', null, InputOption::VALUE_NONE, 'Grant ROLE_ADMIN');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
$email = (string) $input->getArgument('email');
|
|
$password = $input->getArgument('password');
|
|
|
|
if (null === $password || '' === $password) {
|
|
$password = $io->askHidden('Password');
|
|
}
|
|
if (null === $password || '' === $password) {
|
|
$io->error('A password is required.');
|
|
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
if ($this->users->findOneBy(['email' => $email])) {
|
|
$io->error(sprintf('A user with email "%s" already exists.', $email));
|
|
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$user = new User();
|
|
$user->setEmail($email);
|
|
$user->setRoles($input->getOption('admin') ? ['ROLE_ADMIN'] : []);
|
|
$user->setPassword($this->hasher->hashPassword($user, $password));
|
|
|
|
$violations = $this->validator->validate($user);
|
|
if (\count($violations) > 0) {
|
|
foreach ($violations as $violation) {
|
|
$io->error((string) $violation->getMessage());
|
|
}
|
|
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$this->em->persist($user);
|
|
$this->em->flush();
|
|
|
|
$io->success(sprintf('Created %s%s', $email, $input->getOption('admin') ? ' (admin)' : ''));
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|