Change the role of a distant user without having t

2020-02-09 04:35发布

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 ?

3条回答
疯言疯语
2楼-- · 2020-02-09 05:20

Try this in your controller:

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

See FOS/UserBundle/Model/UserManager.php

查看更多
放荡不羁爱自由
3楼-- · 2020-02-09 05:22

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.

查看更多
够拽才男人
4楼-- · 2020-02-09 05:28

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
查看更多
登录 后发表回答