From 5046d260328c0a9d89e21f173adbaed52903612f Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 23 May 2020 08:58:07 -0400 Subject: [PATCH] Explicitly set theme in error handler, refactor middleware #715 --- app/Exceptions/Handler.php | 33 +++-------------- app/Http/Middleware/SetActiveTheme.php | 49 +++++++++++++++++--------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 79f7eb39..cbdb9618 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -5,6 +5,7 @@ namespace App\Exceptions; use App\Exceptions\Converters\GenericExceptionAbstract; use App\Exceptions\Converters\SymfonyException; use App\Exceptions\Converters\ValidationException; +use App\Http\Middleware\SetActiveTheme; use Exception; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; @@ -17,7 +18,6 @@ use Illuminate\Support\Facades\Log; use Illuminate\Validation\ValidationException as IlluminateValidationException; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException as SymfonyHttpException; -use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; use Whoops\Handler\HandlerInterface; @@ -54,8 +54,8 @@ class Handler extends ExceptionHandler return $this->handleApiError($request, $exception); } - if ($exception instanceof AbstractHttpException - && $exception->getStatusCode() === 403) { + (new SetActiveTheme())->setTheme($request); + if ($exception instanceof AbstractHttpException && $exception->getStatusCode() === 403) { return redirect()->guest('login'); } @@ -119,38 +119,13 @@ class Handler extends ExceptionHandler { if ($request->expectsJson() || $request->is('api/*')) { $error = new Unauthenticated(); + return $error->getResponse(); } return redirect()->guest('login'); } - /** - * Render the given HttpException. - * - * @param AbstractHttpException $e - * - * @return \Illuminate\Http\Response|Response - */ - protected function renderHttpException(HttpExceptionInterface $e) - { - $status = $e->getStatusCode(); - view()->replaceNamespace('errors', [ - resource_path('views/layouts/'.setting('general.theme', 'default').'/errors'), - resource_path('views/errors'), - __DIR__.'/views', - ]); - - if (view()->exists("errors::{$status}")) { - return response()->view("errors::{$status}", [ - 'exception' => $e, - 'SKIN_NAME' => setting('general.theme', 'default'), - ], $status, $e->getHeaders()); - } - - return $this->convertExceptionToResponse($e); - } - /** * Ignition error page integration */ diff --git a/app/Http/Middleware/SetActiveTheme.php b/app/Http/Middleware/SetActiveTheme.php index ec915ed1..d8f2d2d2 100644 --- a/app/Http/Middleware/SetActiveTheme.php +++ b/app/Http/Middleware/SetActiveTheme.php @@ -13,23 +13,42 @@ use Illuminate\Support\Facades\Log; */ class SetActiveTheme implements Middleware { + private static $skip = [ + 'admin', + 'admin/*', + 'api', + 'api/*', + 'importer', + 'importer/*', + 'install', + 'install/*', + 'update', + 'update/*', + ]; + + /** + * Handle the request + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * + * @return mixed + */ public function handle(Request $request, Closure $next) { - $skip = [ - 'admin', - 'admin/*', - 'api', - 'api/*', - 'importer', - 'importer/*', - 'install', - 'install/*', - 'update', - 'update/*', - ]; + $this->setTheme($request); + return $next($request); + } - if ($request->is($skip)) { - return $next($request); + /** + * Set the theme for the current middleware + * + * @param \Illuminate\Http\Request $request + */ + public function setTheme(Request $request) + { + if ($request->is(self::$skip)) { + return; } try { @@ -42,7 +61,5 @@ class SetActiveTheme implements Middleware if (!empty($theme)) { Theme::set($theme); } - - return $next($request); } }