/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $context = $this->container->getSecurityContext(); if ($context->getToken() && $context->getToken()->getUser()) { $this->container->getSession()->set($this->container->getParameter('cms_authentication.session.storage_key'), $context->getToken()); } }
/** * @param RequestResponseEvent $event */ public function listen(RequestResponseEvent $event) { $request = $event->getRequest(); $cmsPrefix = $this->container->getParameter('cms.prefix'); if (strpos($request->getPathInfo(), $cmsPrefix) === 0) { //in any way we should try to extract data from session $session = $this->container->getSession(); $tokenParameter = $this->container->getParameter('cms_authentication.session.storage_key'); $securityContext = $this->container->getSecurityContext(); if ($session->has($tokenParameter)) { $securityContext->setToken($session->get($tokenParameter)); $this->container->getEventDispatcher()->dispatch(AuthController::TOKEN_CHANGE_EVENT, new DataAgnosticEvent()); } //non-authorized users that are not on anonymous paths are getting redirected to login if ((!$securityContext->getToken() || !$securityContext->getToken()->getUser()) && !in_array($request->getPathInfo(), $this->container->getParameter('cms_authentication.paths.anonymous'))) { if ($request->isXmlHttpRequest()) { $event->setResponse(new Response(AuthController::EMPTY_BODY, AuthController::FAILURE_STATUS)); } else { $event->setResponse(new RedirectResponse($this->container->getRouter()->generate('cms_authentication_login'))); } $event->stopPropagation(); } //authorized users on login path are redirected to dashboard if ($securityContext->getToken() && $securityContext->getToken()->getUser() && strpos($request->getPathInfo(), $this->container->getParameter('cms_authentication.paths.login')) === 0) { $event->setResponse(new RedirectResponse($cmsPrefix)); $event->stopPropagation(); } } }