HVKotak
HVKotak

Reputation: 258

get Token parameter in action with symfony2

i have created login form manually and set credential in security controller like below

 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'));    
            }

now i want to use it in employee controller but i dont know that how to use it

Upvotes: 0

Views: 7716

Answers (2)

guillaumepotier
guillaumepotier

Reputation: 7448

Once logged though your controller, a employee should have a ROLE_ADMIN role thanks to this line:

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

Now, you could check in every controller under the firewall this:

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

To see if logged and with rights

Upvotes: 0

Carlos Granados
Carlos Granados

Reputation: 11351

Have you tried

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

?

Upvotes: 2

Related Questions