diff options
author | Olivier Paroz <github@oparoz.com> | 2015-01-06 13:24:42 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-01-06 13:24:42 +0300 |
commit | 70c5e85d84adea1df5016088f0bd18d5b017e9b1 (patch) | |
tree | cb0c3a3da53d1f9e9eac9377f0dea96f97ee6d0c /middleware | |
parent | d230e30f80782a3155c2fb89b2183790cc9d0b1c (diff) |
Shorter CheckMiddleware::afterException
Diffstat (limited to 'middleware')
-rw-r--r-- | middleware/checkmiddleware.php | 146 |
1 files changed, 88 insertions, 58 deletions
diff --git a/middleware/checkmiddleware.php b/middleware/checkmiddleware.php index f460ac9a..9456c13e 100644 --- a/middleware/checkmiddleware.php +++ b/middleware/checkmiddleware.php @@ -69,8 +69,9 @@ abstract class CheckMiddleware extends Middleware { } /** - * If a CheckException is being caught, ajax requests return a JSON - * error response and non ajax requests redirect an error page + * If a CheckException is being caught, clients who sent an ajax requests + * get a JSON error response while the others are redirected to an error + * page * * @inheritDoc */ @@ -91,63 +92,11 @@ abstract class CheckMiddleware extends Middleware { ) ); - if (stripos($this->request->getHeader('Accept'), 'html') === false - ) { - $response = new JSONResponse( - array( - 'message' => $message, - 'success' => false - ), - $code - ); - - $this->logger->debug( - "[TokenCheckException] JSON response", - array( - 'app' => $appName - ) - ); - + $acceptHtml = stripos($this->request->getHeader('Accept'), 'html'); + if ($acceptHtml === false) { + $response = $this->sendJsonResponse($acceptHtml, $code); } else { - $this->logger->debug( - "[CheckException] HTML response", - array( - 'app' => $appName - ) - ); - - if ($code === 401) { - $params = $this->request->getParams(); - - $this->logger->debug( - '[CheckException] Unauthorised Request params: {params}', - array( - 'app' => $appName, - 'params' => $params - ) - ); - - /** - * We need to render a template or we'll have an endless - * loop as this is called before the controller can render - * a template - */ - return new TemplateResponse( - $appName, 'authenticate', $params, - 'guest' - ); - - } else { - $url = $this->urlGenerator->linkToRoute( - $this->appName . '.page.error_page', - array( - 'message' => $message, - 'code' => $code - ) - ); - } - - $response = new RedirectResponse($url); + $response = $this->sendHtmlResponse($message, $code); } return $response; @@ -156,4 +105,85 @@ abstract class CheckMiddleware extends Middleware { throw $exception; } + /** + * Redirects the client to an error page or shows an authentication form + * + * @param string $message + * @param int $code + * + * @return RedirectResponse|TemplateResponse + */ + private function sendHtmlResponse($message, $code) { + $appName = $this->appName; + + $this->logger->debug( + "[CheckException] HTML response", + array( + 'app' => $appName + ) + ); + + if ($code === 401) { + $params = $this->request->getParams(); + + $this->logger->debug( + '[CheckException] Unauthorised Request params: {params}', + array( + 'app' => $appName, + 'params' => $params + ) + ); + + /** + * We need to render a template or we'll have an endless + * loop as this is called before the controller can render + * a template + */ + + return new TemplateResponse( + $appName, 'authenticate', $params, + 'guest' + ); + + } else { + $url = $this->urlGenerator->linkToRoute( + $this->appName . '.page.error_page', + array( + 'message' => $message, + 'code' => $code + ) + ); + } + + return new RedirectResponse($url); + } + + /** + * Returns a JSON response to the client + * + * @param string $message + * @param int $code + * + * @return JSONResponse + */ + private function sendJsonResponse($message, $code) { + $appName = $this->appName; + $response = new JSONResponse( + array( + 'message' => $message, + 'success' => false + ), + $code + ); + + $this->logger->debug( + "[TokenCheckException] JSON response", + array( + 'app' => $appName + ) + ); + + return $response; + } + }
\ No newline at end of file |