简体   繁体   English

如何在不重新登录的情况下更新 Symfony 4 中安全令牌中的角色

[英]How to update roles in security token in Symfony 4 without re-logging in

I'm trying to update User's roles after the user confirms its phone number.在用户确认其电话号码后,我正在尝试更新用户的角色。

I make the update in the database:我在数据库中进行更新:

$user->setRoles(["ROLE_USER"]);
$em->persist($user);

That works fine and updates the users's role in the database.这工作正常并更新了用户在数据库中的角色。 (Before, users have "ROLE_UNACTIVATED" group). (以前,用户有“ROLE_UNACTIVATED”组)。

However, it doesn't update the user's roles in the session (security token), so the user needs to logout and then log in one more time.但是,它不会更新用户在会话中的角色(安全令牌),因此用户需要注销然后再登录一次。

So, the question is how to update User's roles in security token?那么,问题是如何更新用户在安全令牌中的角色?

I'm not completely sure that it's a right way to do that, but what I did is manually re-authenticate the user我不完全确定这是正确的方法,但我所做的是手动重新验证用户

public function activation(Request $request, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator)
{
    ...
    //re-authenticate user to update roles in security token
    $guardHandler->authenticateUserAndHandleSuccess(
        $user,
        $request,
        $formAuthenticator,
        'main'
    );
}

and that automatically updates security token of a user without logging out.并且无需注销即可自动更新用户的安全令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM