9

I am trying to change the role of a user, using the FOSUserBundle. The problem is: if the user is logged at the same time, he has to relog to see the new role.

Any solutions ?

j0k
  • 22,600
  • 28
  • 79
  • 90
Joffrey
  • 91
  • 1
  • 4

3 Answers3

12

According to the Security Configuration Reference, there's a always_authenticate_before_granting option. There's not much informations about that, but I've tried it with custom roles loading from database and it does the trick. It looks like everytime you will make a new request, the security component will reload roles.

So if you want to make sure your roles are refreshed and reloaded in your user session just set this option to truein your security.yml :

# app/config/security.yml
security:
    always_authenticate_before_granting:  false
iamdto
  • 1,372
  • 11
  • 23
0

Try this in your controller:

$this->get('fos_user.user_manager')->refreshUser($user);

See FOS/UserBundle/Model/UserManager.php

rtyshyk
  • 942
  • 1
  • 15
  • 29
  • Thank you for your answer, but it's not really what i'm looking for. I can already refresh the roles of a user in the current session. What I'm trying to do is refresh the role of a distant user. Let's say I'm an admin, and I want to change the role of a connected user. He would have to relog to see the new role... How can I avoid that and make it instantanely change? Thank you. – Joffrey Feb 14 '12 at 04:01
-1

1: You must enable switch_role: true in your firewall configuration. 2: Next you must allow the role of ROLE_ADMIN to be able to switched into another role. 3: just make link to the ?_switch_role=userNameCanonical

its mostly done by symfony security fos has nothing to do with the role switching.

Max Małecki
  • 1,700
  • 2
  • 13
  • 18