[英]refresh security.context in symfony2
I have the following app flow: 我有以下应用程序流程:
Add role action: 添加角色操作:
public function addroleAction($role) {
$securityContext = $this->get('security.context');
$em = $this->getDoctrine()->getManager();
$user = $this->get('security.context')->getToken()->getUser();
$userId = $user->getId();
$userObj = $em->getRepository('ProjectEntityBundle:User')->find($userId);
switch ($role) {
case 6://student
$userObj->addRole('ROLE_STUDENT');
$em->flush();
$securityContext->getToken()->setUser($userObj);
break;
}
return $this->redirect($this->generateUrl('check_role'));
}
Check role action: 检查角色动作:
public function checkroleAction() {
$securityContext = $this->get('security.context');
if ($securityContext->isGranted('ROLE_STUDENT')) {
return $this->redirect($this->generateUrl('student_profile'));
} else {
return $this->render('ProjectEduBundle:User:selectrole.html.twig');
}
}
The first action adds role to the database and redirect is made to checkroleAction. 第一个动作将角色添加到数据库,并重定向到checkroleAction。 The if
condition is not met(returns false). if
不满足if
条件(返回false)。 I guess this happens because security context is not refreshed after the database operation of adding role is done. 我猜这是因为添加角色的数据库操作完成后安全上下文没有刷新。 How do I solve this issue? 我该如何解决这个问题?
You can set the user on the token again... 您可以再次在令牌上设置用户...
$this->get('security.context')->getToken()->setUser($userObject);
Add that at the end of your addroleAction()
在addroleAction()
的末尾添加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.