Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSUserBundle Not Setting Salt In DoctrineFixtures

I'm trying to load some users into my database for testing using the DoctrineFixturesBundle. I have done alot of searching here on StackOverflow and have found alot about other issues. None seem to have my answer though. I'm loading my users in using the FOS User Manager. Below is my fixture class.

namespace Kandidly\UserBundle\DataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    private $container;

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $userManager = $this->container->get('fos_user.user_manager');

        $user1 = $userManager->createUser();
        $user1->setFirstName('Admin');
        $user1->setLastName('User');
        $user1->setCity('Somewhere');
        $user1->setState('TN');
        $user1->setUsername('admin');
        $user1->setEmail('[email protected]');
        $user1->setPlainPassword('admin');
        $user1->setEnabled(true);
        $user1->setRoles(
            array(
                 'ROLE_ADMIN'
            )
        );

        $user2 = $userManager->createUser();
        $user2->setFirstName('Normal');
        $user2->setLastName('User');
        $user2->setCity('Another');
        $user2->setState('CA');
        $user2->setUsername('user');
        $user2->setEmail('[email protected]');
        $user2->setPlainPassword('user');
        $user2->setEnabled(true);
        $user2->setRoles(
            array(
                 'ROLE_USER'
            )
        );

        $userManager->updateUser($user1);
        $userManager->updateUser($user2);

        $this->addReference('admin-user', $user1);
    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    function getOrder()
    {
        return 1;
    }

    /**
     * Sets the Container.
     *
     * @param ContainerInterface|null $container A ContainerInterface instance or null
     *
     * @api
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
}

However, when I run the fixtures, I get an error that says the salt is not being set. Can anyone help?

like image 294
searsaw Avatar asked Jun 27 '13 23:06

searsaw


1 Answers

Guessing you have a constructor in your user class? Because the salt is set in the constructor of the base User class.

// FOS\UserBundle\Model\User

public function __construct()
{
    $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    $this->enabled = false;
    $this->locked = false;
    $this->expired = false;
    $this->roles = array();
    $this->credentialsExpired = false;
}

If you have your own constructor don't forget to call the parent constructor with parent::__construct() to get it working. See also in the FOSUserBundle docs, where the call to the parent constructor is mentioned.

From the PHP docs: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

like image 157
Emii Khaos Avatar answered Oct 23 '22 07:10

Emii Khaos