1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\AuthenticationException; |
7
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use Response; |
10
|
|
|
|
11
|
|
|
class Handler extends ExceptionHandler |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* A list of the exception types that should not be reported. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $dontReport = [ |
19
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
20
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
21
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
22
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
23
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
24
|
|
|
\Illuminate\Validation\ValidationException::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Report or log an exception. |
29
|
|
|
* |
30
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
31
|
|
|
* |
32
|
|
|
* @param \Exception $exception |
33
|
|
|
* |
34
|
|
|
* @throws Exception |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function report(Exception $exception) |
39
|
|
|
{ |
40
|
|
|
parent::report($exception); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Render an exception into an HTTP response. |
45
|
|
|
* |
46
|
|
|
* @param \Illuminate\Http\Request $request |
47
|
|
|
* @param \Exception $exception |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function render($request, Exception $exception) |
52
|
|
|
{ |
53
|
|
|
$userLevelCheck = $exception instanceof \jeremykenedy\LaravelRoles\Exceptions\RoleDeniedException || |
54
|
|
|
$exception instanceof \jeremykenedy\LaravelRoles\Exceptions\RoleDeniedException || |
55
|
|
|
$exception instanceof \jeremykenedy\LaravelRoles\Exceptions\PermissionDeniedException || |
56
|
|
|
$exception instanceof \jeremykenedy\LaravelRoles\Exceptions\LevelDeniedException; |
57
|
|
|
|
58
|
|
|
if ($userLevelCheck) { |
59
|
|
|
if ($request->expectsJson()) { |
60
|
|
|
return Response::json([ |
|
|
|
|
61
|
|
|
'error' => 403, |
62
|
|
|
'message' => 'Unauthorized.', |
63
|
|
|
], 403); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
abort(403); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return parent::render($request, $exception); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Convert an authentication exception into an unauthenticated response. |
74
|
|
|
* |
75
|
|
|
* @param \Illuminate\Http\Request $request |
76
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Http\Response |
79
|
|
|
*/ |
80
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
81
|
|
|
{ |
82
|
|
|
if ($request->expectsJson()) { |
83
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return redirect()->guest(route('login')); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|