0

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();
    }

}
?>
Erich
  • 499
  • 1
  • 13
  • 34

1 Answers1

0

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
William Bridge
  • 571
  • 3
  • 12
  • Thank you William for the reply! When I try running the command, I'm getting this error: In UserFixtures.php line 33: Compile Error: Declaration of App\DataFixtures\UserFixtures::load(App\DataF ixtures\ObjectManager $manager) must be compatible with Doctrine\Common\Dat aFixtures\FixtureInterface::load(Doctrine\Common\Persistence\ObjectManager $manager) – Erich Jan 31 '19 at 21:22
  • I suspect this is because the namespace in the PHP you provided - which I named "UserFixtures.php" - is the same as the namespace in AppFixtures.php, which is created by default when the bundle was installed. Any thoughts on how to resolve this? – Erich Jan 31 '19 at 21:28
  • I figured out that I just needed to add use Doctrine\Common\Persistence\ObjectManager; to the top of the class! However, when I run it, it warns me that "Careful, database "roi_report" will be purged. Do you want to continue?" which sounds pretty scary! I'm looking into that now. Any suggestions are appreciated! – Erich Jan 31 '19 at 21:41
  • This is why we generally use fixtures in development mode. + So, you can save your current DB before executing this command + or you can use the --append option which will avoid purging the database. ``` php bin/console doctrine:fixtures:load --append ``` – William Bridge Jan 31 '19 at 22:24