使用symfony2获取令牌参数

时间:2012-08-06 18:59:58

标签: symfony

我已手动创建登录表单并在安全控制器中设置凭证,如下所示

 public function loginCheckAction()
{
    $request = $this->getRequest();
    $session = $request->getSession();

            $oem = $this->getDoctrine()->getEntityManager();
            $username = $request->get('_username');
            $password = $request->get('_password');
            if($username){
                $user = $oem->getRepository('AdminEmployeeBundle:Employees')->loadUserByUsername($username);
                if($user){

                    $dbpassword = $password;
                    if($dbpassword == $user->getPassword()){
                        // secured and redirect
                    $token = new UsernamePasswordToken($user,null,'main',array('ROLE_ADMIN'));
                    // give it to the security context
                    $this->container->get('security.context')->setToken($token);
                    return $this->redirect($this->generateUrl('_employeeList'));    
                }
                    else{
                        $this->get('session')->setFlash('error','Please check username password');
                    }
                }
            return $this->redirect($this->generateUrl('login'));    
            }

现在我想在员工控制器中使用它,但我不知道如何使用它

2 个答案:

答案 0 :(得分:2)

你试过吗

$token = $this->get('security.context')->getToken();

答案 1 :(得分:0)

通过您的控制器进行记录后,员工应该拥有ROLE_ADMIN角色,这要归功于此行:

$token = new UsernamePasswordToken($user,null,'main',array('ROLE_ADMIN'));

现在,您可以检查防火墙下的每个控制器:

$this->get('security.context')->isGranted('ROLE_ADMIN');

查看是否记录并拥有权利