/** * {@inheritdoc} */ public static function doSetUpBeforeClass() { static::$schemaTool = new SchemaTool(static::$em); static::$schemaTool->dropSchema(static::getTablesMetadata()); static::$schemaTool->createSchema(static::getTablesMetadata()); static::$encoder = static::$container->get('security.encoder_factory'); static::$user = new User(); static::$user->setEmail('*****@*****.**'); static::$user->setPassword(static::$encoder->getEncoder(static::$user)->encodePassword('1234', static::$user->getSalt())); static::$user->setUsername('testUser'); $entityPermissionCategory = new PermissionCategory(); $entityPermissionCategory->setName('backend_user'); $entityPermissionCategory->setTechnicalName('backend_user'); static::$em->persist($entityPermissionCategory); $entityPermission = new Permission(); $entityPermission->setRoleName('IS_AUTHENTICATED_FULLY'); $entityPermission->setDescription('IS_AUTHENTICATED_FULLY'); $entityPermission->setName('IS_AUTHENTICATED_FULLY'); $entityPermission->setCategory($entityPermissionCategory); $entityPermission2 = new Permission(); $entityPermission2->setRoleName('ROLE_MANAGE_PERMISSIONS'); $entityPermission2->setDescription('ROLE_MANAGE_PERMISSIONS'); $entityPermission2->setName('ROLE_MANAGE_PERMISSIONS'); $entityPermission2->setCategory($entityPermissionCategory); $entityPermission3 = new Permission(); $entityPermission3->setRoleName('ROLE_ACCESS_BACKEND_TOOLS_SECURITY_SECTION'); $entityPermission3->setDescription('ROLE_ACCESS_BACKEND_TOOLS_SECURITY_SECTION'); $entityPermission3->setName('ROLE_ACCESS_BACKEND_TOOLS_SECURITY_SECTION'); $entityPermission3->setCategory($entityPermissionCategory); $entityPermission4 = new Permission(); $entityPermission4->setRoleName('ROLE_MANAGE_USER_PROFILES'); $entityPermission4->setDescription('ROLE_MANAGE_USER_PROFILES'); $entityPermission4->setName('ROLE_MANAGE_USER_PROFILES'); $entityPermission4->setCategory($entityPermissionCategory); static::$em->persist($entityPermission); static::$em->persist($entityPermission2); static::$em->persist($entityPermission3); static::$em->persist($entityPermission4); static::$em->flush(); $group = new Group(); $group->setRefName('BACKEND-USER'); $group->setName('backend-user'); $group->addPermission($entityPermission); $group->addPermission($entityPermission2); $group->addPermission($entityPermission3); $group->addPermission($entityPermission4); static::$user->addToGroup($group); static::$em->persist($group); static::$em->persist(static::$user); static::$em->flush(); }
public function testGetAuthenticationResponse() { $token = \Phake::mock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface'); $resp = Authenticator::getAuthenticationResponse($token); $this->assertInternalType('array', $resp); $this->assertArrayHasKey('success', $resp); $this->assertFalse($resp['success']); $user = new User(); $user->setFirstName('John'); $user->setLastName('Doe'); $user->setEmail('*****@*****.**'); $user->setUsername('john.doe'); $role = \Phake::mock('Symfony\\Component\\Security\\Core\\Role\\RoleInterface'); \Phake::when($role)->getRole()->thenReturn('ROLE_USER'); \Phake::when($token)->isAuthenticated()->thenReturn(true); \Phake::when($token)->getUser()->thenReturn($user); \Phake::when($token)->getRoles()->thenReturn(array($role)); $resp = Authenticator::getAuthenticationResponse($token); $this->assertInternalType('array', $resp); $this->assertArrayHasKey('success', $resp); $this->assertTrue($resp['success']); $this->assertArrayHasKey('profile', $resp); $this->assertInternalType('array', $resp['profile']); $this->assertEquals(array('id' => $user->getId(), 'name' => $user->getFullName(), 'email' => $user->getEmail(), 'username' => $user->getUsername()), $resp['profile']); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { /* @var EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $username = $input->getOption('username'); $email = $input->getOption('email'); $password = $input->getOption('password'); if (false === $input->getOption('no-interactions')) { /* @var DialogHelper $dialog */ $dialog = $this->getHelper('dialog'); $output->writeln('<info>This command will let you to create a test user that you can user to authenticated to administration interface</info>'); $output->write(PHP_EOL); $username = $dialog->ask($output, '<question>Username:</question> '); $email = $dialog->ask($output, '<question>Email:</question> '); do { $password = $dialog->askHiddenResponse($output, '<question>Password:</question> '); $passwordConfirm = $dialog->askHiddenResponse($output, '<question>Password again:</question> '); if ($password != $passwordConfirm) { $output->writeln('<error>Entered passwords do not match, please try again</error>'); } } while ($password != $passwordConfirm); } /* @var UserPasswordEncoderInterface $encoder */ $encoder = $this->getContainer()->get('security.password_encoder'); $user = new User(); $user->setEmail($email); $user->setUsername($username); $user->setPassword($encoder->encodePassword($user, $password)); $em->persist($user); $em->flush(); $output->writeln(''); $output->writeln(sprintf('<info>Great success! User "%s" has been successfully created!</info>', $user->getUsername())); }
private function createUser() { $u = new User(); $u->setFirstName('Joe'); $u->setLastName('Doe'); $u->setUsername('djatel'); $u->setEmail('*****@*****.**'); $u->setPassword(1234); self::$em->persist($u); self::$em->flush(); return $u; }
public function testGetByRole() { $user1 = new User(); $user2 = new User(); $user3 = new User(); $user1->setUsername('user1'); $user1->setPassword('pwd1'); $user1->setEmail('*****@*****.**'); $user2->setUsername('user2'); $user2->setPassword('pwd2'); $user2->setEmail('*****@*****.**'); $user3->setUsername('user3'); $user3->setPassword('pwd3'); $user3->setEmail('*****@*****.**'); $permission1 = new Permission(); $permission2 = new Permission(); $permission1->setRoleName('ROLE_USER'); $permission1->addUser($user1); $permission2->setRoleName('ROLE_ADMIN'); $permission2->addUser($user2); $group1 = new Group(); $group2 = new Group(); $group1->setName('User'); $group1->addPermission($permission1); $group1->addUser($user3); $group2->setName('Admin'); $group2->addPermission($permission2); $group2->addUser($user3); self::$em->persist($user1); self::$em->persist($user2); self::$em->persist($user3); self::$em->persist($permission1); self::$em->persist($permission2); self::$em->persist($group1); self::$em->persist($group2); self::$em->flush(); $rootUserHandler = \Phake::mock('Modera\\SecurityBundle\\RootUserHandling\\RootUserHandlerInterface'); $service = new UserService(self::$em, $rootUserHandler); $this->assertEquals(array($user1, $user3), $service->getUsersByRole('ROLE_USER')); $this->assertEquals(array($user2, $user3), $service->getUsersByRole('ROLE_ADMIN')); $this->assertEquals(array($user1->getId(), $user3->getId()), $service->getIdsByRole('ROLE_USER')); $this->assertEquals(array($user2->getId(), $user3->getId()), $service->getIdsByRole('ROLE_ADMIN')); }