Search code examples
symfonysecuritysymfony4symfony-security

How to properly add a user after using makerbundle make:auth


I'm setting up login authentication for a Symfony 4 app using MakerBundle 1.8's feature listed below:

php bin/console make:auth

This feature is described in this article: https://symfony.com/blog/new-in-makerbundle-1-8-instant-user-login-form-commands

I am able to view the form and attempt logging in, but I can't figure out how to properly add users with passwords and roles.

I've tried just creating the user through the database, using a string like this for the roles field:

{"path":"^/admin","roles":"ROLE_ADMIN"}

(I found that JSON under "access_control" on security.yml)

But when I try logging in as that user, I get a message saying "Invalid credentials." I suspect this is because security is using encryption and the user I added into the database is in plain text.

Please let me know if you have any suggestions on how I can add a user to test the security that is added with MakerBundle's make:auth feature.

UPDATE: Thanks William for the answer! Here is the modified version of the fixture you provided that has allowed me to log in.

<?php
namespace App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManage;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;

class UserFixtures extends Fixture 
{

    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    public function load(ObjectManager $manager)
    {

            $user = new User();
            $user->setEmail("myemail@somedomain.io");

            $roles = array("path" => "^/admin", "roles" => "ROLE_ADMIN");

            $user->setRoles($roles);

            $password = $this->encoder->encodePassword($user, 'pass_1234');
            $user->setPassword($password);

            $manager->persist($user);

        $manager->flush();
    }

}
?>

Solution

  • one of the solutions would be to use the fixtures. According to the documentation : DoctrineFixturesBundle, I think you could do a user fixture class like :

    <?php
    namespace App\DataFixtures;
    
    use Doctrine\Bundle\FixturesBundle\Fixture;
    use Doctrine\Common\Persistence\ObjectManage;
    use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
    use App\Entity\User;
    
    class UserFixtures extends Fixture 
    {
    
        public function __construct(UserPasswordEncoderInterface $encoder)
        {
            $this->encoder = $encoder;
        }
    
        public function load(ObjectManager $manager)
        {
            $numberOfUsers = 10; 
            for($i = 0; $i < $numberOfUsers; $i++) {
                 $user = new User();
                 $user->setUsername(sprintf('test%d', $i));
                 $user->addRole('ROLE_ADMIN'); 
                 $password = $this->encoder->encodePassword($user, 'pass_1234');
                 $user->setPassword($password);
    
                 $manager->persist($user);
            }
    
            $manager->flush();
        }
    
    }
    ?>
    

    Finaly, you execute the command :

    php bin/console doctrine:fixtures:load