Commit 5dd79243 authored by Nicolas Widart's avatar Nicolas Widart

Redirect to login page if a logged out user gets a 404 on the backend

parent 06865f7b
...@@ -6,9 +6,11 @@ use Exception; ...@@ -6,9 +6,11 @@ use Exception;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Modules\User\Contracts\Authentication;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Contracts\Container\Container;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
...@@ -24,6 +26,17 @@ class Handler extends ExceptionHandler ...@@ -24,6 +26,17 @@ class Handler extends ExceptionHandler
ValidationException::class, ValidationException::class,
]; ];
/**
* @var Authentication
*/
private $auth;
public function __construct(Container $container, Authentication $auth)
{
parent::__construct($container);
$this->auth = $auth;
}
/** /**
* Report or log an exception. * Report or log an exception.
* *
...@@ -56,10 +69,16 @@ class Handler extends ExceptionHandler ...@@ -56,10 +69,16 @@ class Handler extends ExceptionHandler
private function handleExceptions($e) private function handleExceptions($e)
{ {
if ($e instanceof ModelNotFoundException) { if ($e instanceof ModelNotFoundException) {
if ($this->auth->check() === false) {
return redirect()->route('login');
}
return response()->view('errors.404', [], 404); return response()->view('errors.404', [], 404);
} }
if ($e instanceof NotFoundHttpException) { if ($e instanceof NotFoundHttpException) {
if ($this->auth->check() === false) {
return redirect()->route('login');
}
return response()->view('errors.404', [], 404); return response()->view('errors.404', [], 404);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment