helper = $helper; $this->securityContext = $securityContext; $this->templating = $templating; $this->router = $router; $this->session = $session; $this->lhs = $lhs; $this->um = $um; } /** * Listen for request events * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event */ public function onCoreRequest(GetResponseEvent $event) { $token = $this->securityContext->getToken(); if (!$token) { return; } if (!$token instanceof UsernamePasswordToken) { return; } $key = $this->helper->getSessionKey($this->securityContext->getToken()); //var_dump($key);die; $request = $event->getRequest(); $session = $event->getRequest()->getSession(); /** * @var CustomUserInterface $user */ $user = $this->securityContext->getToken()->getUser(); ///* if ($request->getMethod() == 'POST' && !empty($request->get('_device_name'))) { $oldDeviceName = $this->helper->getMachineName(); if(!empty($oldDeviceName)){ $this->helper->setNewName($user,(string)$request->get('_device_name'),$oldDeviceName); $this->redirectToDashboard($event); return; } }//**/ //Check if user has to do two-factor authentication ///* if (!$session->has($key)) { $this->checkMachineNameSet($event); return; }//*/ if ($session->get($key) === true) { $this->checkMachineNameSet($event); return; } if ($request->getMethod() == 'POST') { //Check the authentication code $remember_place = $request->get('_remember_place',false);//true if checkbox checked if ($this->helper->checkCode($user, $request->get('_auth_code'), $remember_place) == true) { //Flag authentication complete $session->set($key, true); //Redirect to user's dashboard if(!$remember_place){ $this->helper->setTempDevice(true); } else { $this->helper->setTempDevice(false); } $this->lhs->addEntry(LoginHistory::LOGIN_SUCCESS, $user); $this->um->refreshLastSeen($user); $this->redirectToDashboard($event); return; } else { if($user->getFailedAttempt() == 3){ $this->securityContext->setToken(null); $this->lhs->addEntry(LoginHistory::LOGIN_FAILED3T, $user); $this->redirectToLogout($event); return; } else { $this->lhs->addEntry(LoginHistory::LOGIN_FAILED, $user); $this->session->putFlash("error", "The verification code is not valid."); } } } //Force authentication code dialog $args_auth = array('remember_place' => $user->isRememberPlaces()); try { $response = $this->templating->renderResponse('App/Default/User/step.twoauth.html.twig', $args_auth); } catch (Error $e) { die('error '.$e); } $event->setResponse($response); } /** * @param GetResponseEvent $event */ protected function checkMachineNameSet($event){ if(!$this->helper->isTempDevice()){ $deviceName = $this->helper->getMachineName(); if($deviceName == "NaN"){ $args = array(); //generate a name $args['default_name'] = $this->helper->getNewName(); //Force device name dialog try { $response = $this->templating->renderResponse( 'App/Default/User/set.device.name.html.twig', $args ); } catch (Error $e) { die('error '.$e); } $event->setResponse($response); } } } /** * @param GetResponseEvent $event */ protected function redirectToDashboard($event){ $redirect = new RedirectResponse($this->router->generate("panel_dashboard")); $event->setResponse($redirect); } /** * @param GetResponseEvent $event */ protected function redirectToLogout($event){ $redirect = new RedirectResponse($this->router->generate("login")); $event->setResponse($redirect); } }