-
-
Save jmather/3813952 to your computer and use it in GitHub Desktop.
<?php | |
namespace Application\Sonata\UserBundle\DataFixtures\ORM; | |
use Doctrine\Common\DataFixtures\AbstractFixture; | |
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Doctrine\Common\DataFixtures\FixtureInterface; | |
use Application\Sonata\UserBundle\Entity\User; | |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | |
use FOS\UserBundle\Doctrine\UserManager; | |
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface | |
{ | |
private UserManager $userManager; | |
private UserPasswordEncoderInterface $passwordEncoder; | |
/** | |
* {@inheritDoc} | |
*/ | |
public function setContainer(UserManager $userManager, UserPasswordEncoderInterface $passwordEncoder) | |
{ | |
$this->userManager = $userManager; | |
$this->passwordEncoder = $passwordEncoder; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function load(ObjectManager $manager) | |
{ | |
$test_password = 'test'; | |
/** @var $user \Application\Sonata\UserBundle\Entity\User */ | |
$user = $this->userManager->createUser(); | |
$user->setUsername('superadmin'); | |
$user->setEmail('superadmin@example.com'); | |
$user->setFirstname('Super'); | |
$user->setLastname('Admin'); | |
$user->setRoles(array('ROLE_SUPER_ADMIN')); | |
$user->setEnabled(true); | |
$password = $this->passwordEncoder->encodePassword($test_password, $user->getSalt()); | |
$user->setPassword($password); | |
$this->userManager->updateUser($user); | |
$this->addReference('user.super_admin', $user); | |
unset($user); | |
/** @var $user \Application\Sonata\UserBundle\Entity\User */ | |
$user = $this->userManager->createUser(); | |
$user->setUsername('admin'); | |
$user->setPlainPassword($test_password); | |
$user->setEmail('admin@example.com'); | |
$user->setFirstname('Regular'); | |
$user->setLastname('Admin'); | |
$user->setRoles(array('ROLE_ADMIN')); | |
$user->setEnabled(true); | |
$password = $this->passwordEncoder->encodePassword($test_password, $user->getSalt()); | |
$user->setPassword($password); | |
$this->userManager->updateUser($user); | |
$this->addReference('user.admin', $user); | |
$faker = \Faker\Factory::create(); | |
for ($i = 0; $i < 10; $i++) | |
{ | |
/** @var $user \Application\Sonata\UserBundle\Entity\User */ | |
$user = $this->userManager->createUser(); | |
$user->setUsername($faker->userName); | |
$user->setPlainPassword($test_password); | |
$user->setEmail($faker->safeEmail); | |
$user->setFirstname($faker->firstName); | |
$user->setLastname($faker->lastName); | |
$user->setRoles(array('ROLE_USER')); | |
$user->setEnabled(true); | |
$password = $this->passwordEncoder->encodePassword($test_password, $user->getSalt()); | |
$user->setPassword($password); | |
$this->userManager->updateUser($user); | |
$this->addReference('user.demo_'.$i, $user); | |
} | |
} | |
/** | |
* Get the order of this fixture | |
* | |
* @return integer | |
*/ | |
function getOrder() | |
{ | |
return 1; | |
} | |
} |
good job!
Just a heads up for people trying to find a solution around the time of this comment: please note that this gist does not work for current versions of Symfony, as it would give you the following error:
The "security.encoder_factory" service or alias has been removed or inlined when the container wa
s compiled. You should either make it public, or stop using the container directly and use depend
ency injection instead.
Instead you can use something like this:
private UserPasswordEncoderInterface $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
public function load(ObjectManager $manager): void
{
...
$user->setPassword($this->passwordEncoder->encodePassword(
$user,
$thePlainPassword
));
...
}
That's... pretty impressive that after 9 years all that changed was that i should have used more dependency injection. Totes my bad, but I'll take it for a one-off script. :) Thanks for the update!
I've tried to update it to incorporate the comment and kinda bump up it's stuff a little, but i've not touched PHP in a long time now, so YMMV on results. :)
Awesome mate! It is to be expected coding standards change, and maybe even more impressive that Google still found this gist during a regular search. Thanks for sharing your code. Have a nice day!
Thanks for this gist !