Created
December 2, 2011 16:24
-
-
Save jmoz/1423829 to your computer and use it in GitHub Desktop.
LoadSecurityData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace TimeOut\Bundle\SecurityBundle\DataFixtures; | |
| use Doctrine\Common\DataFixtures\FixtureInterface; | |
| use TimeOut\Bundle\SecurityBundle\Entity\User; | |
| use TimeOut\Bundle\SecurityBundle\Entity\Role; | |
| /** | |
| * Load test users. | |
| * @author James Morris <james@jmoz.co.uk> | |
| */ | |
| class LoadSecurityData implements FixtureInterface | |
| { | |
| /** | |
| * Take the password encoder factory | |
| * @param type $factory | |
| */ | |
| public function __construct( $factory ) | |
| { | |
| $this->factory = $factory; | |
| } | |
| public function load( $manager ) | |
| { | |
| $this->loadUsers( $manager ); | |
| $this->loadRoles( $manager ); | |
| $this->loadUsersRoles( $manager ); | |
| } | |
| public function truncate( $manager ) | |
| { | |
| $manager->getConnection()->exec('SET FOREIGN_KEY_CHECKS=0'); | |
| $sql = $manager->getConnection()->getDatabasePlatform()->getTruncateTableSQL( 'security_users_roles' ); | |
| $manager->getConnection()->executeUpdate( $sql ); | |
| $sql = $manager->getConnection()->getDatabasePlatform()->getTruncateTableSQL( 'security_users' ); | |
| $manager->getConnection()->executeUpdate( $sql ); | |
| $sql = $manager->getConnection()->getDatabasePlatform()->getTruncateTableSQL( 'security_roles' ); | |
| $manager->getConnection()->executeUpdate( $sql ); | |
| $manager->getConnection()->exec('SET FOREIGN_KEY_CHECKS=1'); | |
| } | |
| /** | |
| * Use the password encoder to set the $password on the $user | |
| * @param User $user | |
| * @param string $password | |
| */ | |
| private function setEncodedPassword( User $user, $password ) | |
| { | |
| $encoder = $this->factory->getEncoder( $user ); | |
| $password = $encoder->encodePassword( $password, $user->getSalt() ); | |
| $user->setPassword( $password ); | |
| } | |
| public function loadUsers( $manager ) | |
| { | |
| $u = new User(); | |
| $u->setUsername( 'testuser' ); | |
| $u->setAlgorithm( 'sha512' ); | |
| $this->setEncodedPassword( $u, 'testpass' ); | |
| $u->setEnabled( true ); | |
| $u->setEmail( 'testuser1@test.com' ); | |
| $manager->persist( $u ); | |
| $manager->flush(); | |
| $u = new User(); | |
| $u->setUsername( 'testuser2' ); | |
| $u->setAlgorithm( 'sha512' ); | |
| $this->setEncodedPassword( $u, 'testpass' ); | |
| $u->setEnabled( true ); | |
| $u->setEmail( 'testuser2@test.com' ); | |
| $manager->persist( $u ); | |
| $manager->flush(); | |
| } | |
| public function loadRoles( $manager ) | |
| { | |
| $role = new Role( 'ROLE_TEST1' ); | |
| $manager->persist( $role ); | |
| $role = new Role( 'ROLE_TEST2' ); | |
| $manager->persist( $role ); | |
| $role = new Role( 'ROLE_TAXONOMY_VIEW' ); | |
| $manager->persist( $role ); | |
| $manager->flush(); | |
| } | |
| /** | |
| * Using raw sql as the SecurityTest needs to test functionality of addRole() etc so can not use in here. | |
| */ | |
| public function loadUsersRoles( $manager ) | |
| { | |
| $manager->getConnection()->exec( "insert into security_users_roles values (1, 1), (1,2), (2,3)" ); | |
| $manager->clear(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment