1

I have a system, that has 2 roles (admins and users). Authentication made using Security Symfony2 component. Admin doesn't know user password. But he should be able to login into the system as user. I have a grid with all users and want to add buttons like "Login as this user". How can I make it?

I have tried, but no prfit:

$userRepo = $this->getDoctrine()->getRepository('FrameFoxBackEndBundle:User');
$this->get('security.context')->getToken()->setUser($userRepo->find(1));
3
  • If you want the admin to be login with any user id make a global password in users table and if admin comes online with any user id with that global password simply login him
    – Wearybands
    Sep 7, 2012 at 9:20
  • A global password field which only admin will know and it will work for all users in your table
    – Wearybands
    Sep 7, 2012 at 9:21
  • @Umair Iqbal, sorry but it's a curve solution. See Bgi answer. Sep 7, 2012 at 9:51

3 Answers 3

11

Why not use built-in switch user option?

1
4

I use this code :

// use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
$entity = $userRepo->find(1);

// Authentication
// create the authentication token
$token = new UsernamePasswordToken(
    $entity,
    null,
    'user_db',
    $entity->getRoles());
// give it to the security context
$this->container->get('security.context')->setToken($token);
1
  • 1
    I know that UsernamePasswordToken works, but in this problem l3l0' answer is imho better Oct 10, 2013 at 10:40
0

I would use Symfony core support for that manner. Have a look at: http://symfony.com/doc/current/cookbook/security/impersonating_user.html.

You define a role which is allowed to switch user, and a parameter in the url that allows you to switch the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.