/** * Dispatch the request to the action of the controller * @return null */ private function dispatch() { if (!$this->dispatcher) { $this->dispatcher = new Dispatcher(new ObjectFactory()); } while ($this->request != null) { $this->eventManager->runEvent(self::EVENT_PRE_DISPATCH); if ($this->request == null) { continue; } try { $chainedRequest = $this->dispatcher->dispatch($this->request, $this->response); $this->setRequest($chainedRequest); } catch (Exception $e) { $this->eventManager->runEvent(self::EVENT_ERROR, $e); $this->setRequest(null); } $this->eventManager->runEvent(self::EVENT_POST_DISPATCH); } }
/** * Sets an unauthorized status code to the response and dispatch to the authentication form * @return null */ private function showAuthenticationForm() { $zibo = Zibo::getInstance(); $request = $zibo->getRequest(); $response = $zibo->getResponse(); $securityManager = SecurityManager::getInstance(); $user = $securityManager->getUser(); if ($user) { // already logged in, show blank page with error message $response->addMessage(new Message(self::TRANSLATION_ERROR_FORBIDDEN, Message::TYPE_ERROR)); $response->setStatusCode(Response::STATUS_CODE_FORBIDDEN); $response->setView(new BaseView()); return; } // not logged in, show authentication form $response->addMessage(new Message(self::TRANSLATION_ERROR_UNAUTHORIZED, Message::TYPE_ERROR)); $authenticator = $securityManager->getAuthenticator(); if ($authenticator instanceof HttpAuthenticator) { $response->addHeader(Response::HEADER_AUTHENTICATE, $authenticator->getAuthenticateHeader()); $response->setStatusCode(Response::STATUS_CODE_UNAUTHORIZED); } else { $response->setStatusCode(Response::STATUS_CODE_FORBIDDEN); } $dispatcher = $zibo->getDispatcher(); if (!$dispatcher) { $dispatcher = new Dispatcher(new ObjectFactory()); } $baseUrl = $request->getBaseUrl(); $basePath = $baseUrl . Request::QUERY_SEPARATOR . self::ROUTE_AUTHENTICATION; $controller = self::CONTROLLER_AUTHENTICATION; $request = new Request($baseUrl, $basePath, $controller, Dispatcher::ACTION_ASTERIX); $dispatcher->dispatch($request, $response); }