V
V
vasx32020-10-06 20:14:28
symfony
vasx3, 2020-10-06 20:14:28

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

2 answer(s)
B
BoShurik, 2020-10-07
@vasx3

If FOSUserBundle is used, then the command must be used

bin/console fos:user:create username [email protected] '[email protected]'
.
In the case of your decision, this team must be created by yourself. For me, it looks something like this (in fact, it repeats the controller action code with its own nuances)
CreateCommand

/**
 * @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
Creating users, and even more so administrators, through migrations and fixtures is a risk, because. in the event of a code leak, all this data will be available to third parties

G
golentor, 2020-10-06
@golentor

In console

php bin/console make:auth
php bin/console make:user

Then follow the instructions.
https://symfony.com/doc/master/security.html
No thanks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question