/**
  * @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();
         }
     }
 }
Пример #2
0
 public function getSupraPath($name, $params = array())
 {
     return $this->container->getRouter()->generate($name, $params);
 }
Пример #3
0
 public function buildJsPack()
 {
     //@todo: caching here
     return $this->container->getRouter()->generate('cms_js_pack');
 }
Пример #4
0
 public function handle(Request $request)
 {
     try {
         $requestEvent = new RequestResponseEvent();
         $requestEvent->setRequest($request);
         $this->container->getEventDispatcher()->dispatch(KernelEvent::REQUEST, $requestEvent);
         //here event can be overridden by any listener, so check if we have event
         if ($requestEvent->hasResponse()) {
             return $requestEvent->getResponse();
         }
         if ($request->attributes->has('_controller') && $request->attributes->has('_action')) {
             $controllerName = $request->attributes->get('_controller');
             $action = $request->attributes->get('_action');
             $controllerObject = new $controllerName();
             $controllerObject->setContainer($this->container);
             $response = $controllerObject->{$action}($request);
         } else {
             $router = $this->container->getRouter();
             $configuration = $router->match($request);
             //@todo: recall correctly how symfony deals with that
             $request->attributes = new ParameterBag($configuration);
             //@todo: do not execute controller that ugly
             $controllerDefinition = $this->container->getApplication()->parseControllerName($configuration['controller']);
             //probably there should be a better implementation of a package setting
             $controllerObject = new $controllerDefinition['controller']();
             $controllerObject->setContainer($this->container);
             $action = $controllerDefinition['action'];
             $controllerEvent = new ControllerEvent();
             $controllerEvent->setController($controllerObject);
             $controllerEvent->setAction($action);
             $this->container->getEventDispatcher()->dispatch(KernelEvent::CONTROLLER_START, $controllerEvent);
             $response = $controllerObject->{$action}($request);
             $controllerEvent->setResponse($response);
             $this->container->getEventDispatcher()->dispatch(KernelEvent::CONTROLLER_END, $controllerEvent);
             $response = $controllerEvent->getResponse();
         }
         $responseEvent = new RequestResponseEvent();
         $responseEvent->setRequest($request);
         $responseEvent->setResponse($response);
         $this->container->getEventDispatcher()->dispatch(KernelEvent::RESPONSE, $responseEvent);
         if (!$response instanceof Response) {
             throw new \Exception('Response returned by your controller is not an instance of HttpFoundation\\Response');
         }
         return $response;
     } catch (\Exception $e) {
         //generic exception handler
         $exceptionEvent = new RequestResponseEvent();
         $exceptionEvent->setRequest($request);
         $exceptionEvent->setData($e);
         $this->container->getEventDispatcher()->dispatch(KernelEvent::EXCEPTION, $exceptionEvent);
         if ($exceptionEvent->hasResponse()) {
             $this->container->getEventDispatcher()->dispatch(KernelEvent::RESPONSE, $exceptionEvent);
             return $exceptionEvent->getResponse();
         }
         //process 404 exceptions
         if ($e instanceof ResourceNotFoundException) {
             $notFoundEvent = new RequestResponseEvent();
             $notFoundEvent->setRequest($request);
             $this->container->getEventDispatcher()->dispatch(KernelEvent::ERROR404, $notFoundEvent);
             if ($notFoundEvent->hasResponse()) {
                 $this->container->getEventDispatcher()->dispatch(KernelEvent::RESPONSE, $notFoundEvent);
                 return $notFoundEvent->getResponse();
             }
             if ($this->container->getParameter('debug')) {
                 //in debug env 404 errors are just thrown
                 throw $e;
             } else {
                 return $this->container['exception.controller']->exception404Action($e);
             }
         }
         //process all other exceptions
         if ($this->container->getParameter('debug')) {
             throw $e;
         } else {
             return $this->container['exception.controller']->exception500Action($e);
         }
     }
 }