Answer the question
In order to leave comments, you need to log in
How to add a primary admin in Symfony?
Can you tell me how to add a primary administrator in Symfony 5?
How to manage users when there is already an administrator is clear, but how to add one?
in security.yaml?
I tried to set it in the in_memory provider, but it only allows me to set 2 parameters: password and role, but I have more required fields. And I'm not sure that it's safe enough to store accesses in the code.
Through fixtures?
So it seems to be recommended to use only for virgins.
How best to enlighten the beginner
Answer the question
In order to leave comments, you need to log in
If FOSUserBundle is used, then the command must be used
bin/console fos:user:create username [email protected] '[email protected]'
. /**
* @psalm-suppress PropertyNotSetInConstructor
*/
class CreateCommand extends AbstractCommand
{
private DocumentManager $documentManager;
private ValidatorInterface $validator;
private PasswordGenerator $passwordGenerator;
private AdministratorMapper $mapper;
public function __construct(
DocumentManager $documentManager,
ValidatorInterface $validator,
PasswordGenerator $passwordGenerator,
AdministratorMapper $mapper
) {
parent::__construct();
$this->documentManager = $documentManager;
$this->validator = $validator;
$this->passwordGenerator = $passwordGenerator;
$this->mapper = $mapper;
}
/**
* @psalm-suppress MissingReturnType
*/
protected function configure()
{
$this
->setName('app:administrator:create')
->addArgument('username', InputArgument::REQUIRED, 'Username')
->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'Password')
->setDescription('Creates administrator')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var string $username */
$username = $input->getArgument('username');
/** @var string|null $plainPassword */
$plainPassword = $input->getOption('password');
if (!$plainPassword) {
$plainPassword = $this->passwordGenerator->generate();
}
$model = new AdministratorModel();
$model->enabled = true;
$model->username = $username;
$model->password = $plainPassword;
$errors = $this->validator->validate($model);
if (\count($errors) > 0) {
$this->io->error('Can\'t create administrator');
$this->printConstraintViolations($errors);
return 1;
}
$administrator = $this->mapper->map($model);
$this->documentManager->persist($administrator);
$this->documentManager->flush();
$this->io->writeln(sprintf('Administrator <info>%s</info> with password <info>%s</info> has been created', $administrator->getUsername(), $plainPassword));
return 0;
}
}
bin/console app:administrator:create vasx3
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question