Explicitly set theme in error handler, refactor middleware #715

This commit is contained in:
Nabeel Shahzad 2020-05-23 08:58:07 -04:00
parent 4cd7eef937
commit 5046d26032
2 changed files with 37 additions and 45 deletions

View File

@ -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
*/

View File

@ -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);
}
}