1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Exceptions; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use Illuminate\Auth\AuthenticationException; |
6
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
9
|
|
|
|
10
|
|
|
class Handler extends ExceptionHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* A list of the exception types that should not be reported. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $dontReport = [ |
18
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
19
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
20
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
21
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
22
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
23
|
|
|
\Illuminate\Validation\ValidationException::class |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Report or log an exception. |
28
|
|
|
* |
29
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
30
|
|
|
* |
31
|
|
|
* @param \Exception $exception |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function report(Exception $exception) |
35
|
|
|
{ |
36
|
|
|
parent::report($exception); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Render an exception into an HTTP response. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Http\Request $request |
43
|
|
|
* @param \Exception $exception |
44
|
|
|
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response |
45
|
|
|
*/ |
46
|
|
|
public function render($request, Exception $exception) |
47
|
|
|
{ |
48
|
|
|
if ($exception instanceof \Ultraware\Roles\Exceptions\RoleDeniedException || |
49
|
|
|
$exception instanceof \Ultraware\Roles\Exceptions\PermissionDeniedException || |
50
|
|
|
$exception instanceof \Ultraware\Roles\Exceptions\LevelDeniedException) { |
51
|
|
|
//If the user is banished, redirect him to the banished page. |
52
|
|
|
if (Auth::check() && Auth::user()->hasRole('banished')) { |
|
|
|
|
53
|
|
|
return redirect() |
54
|
|
|
->route('page.banished'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return redirect() |
58
|
|
|
->route('page.index') |
59
|
|
|
->with('danger', 'You don\'t have the permission to view this page.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return parent::render($request, $exception); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Convert an authentication exception into an unauthenticated response. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Http\Request $request |
69
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Http\Response |
72
|
|
|
*/ |
73
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
74
|
|
|
{ |
75
|
|
|
if ($request->expectsJson()) { |
76
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return redirect() |
80
|
|
|
->guest(route('users.auth.login')) |
81
|
|
|
->with('danger', 'You don\'t have the permission to view this page.'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: