1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Auth\AuthenticationException as AuthenticationException; |
7
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
9
|
|
|
use Facades\App\Services\WhichPortal; |
10
|
|
|
use Illuminate\Session\TokenMismatchException; |
11
|
|
|
use Illuminate\Support\Facades\Lang; |
12
|
|
|
use Illuminate\Support\Facades\Log; |
13
|
|
|
use Illuminate\Support\Facades\Request; |
14
|
|
|
use Illuminate\Support\Facades\Auth; |
15
|
|
|
|
16
|
|
|
class Handler extends ExceptionHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* A list of the exception types that are not reported. |
20
|
|
|
* |
21
|
|
|
* @var array |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
protected $dontReport = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A list of the inputs that are never flashed for validation exceptions. |
27
|
|
|
* |
28
|
|
|
* @var array |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
protected $dontFlash = [ |
31
|
|
|
'password', |
32
|
|
|
'password_confirmation', |
33
|
|
|
'current_password', |
34
|
|
|
'new_password', |
35
|
|
|
'new_password_confirmation', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* OVERRIDE |
40
|
|
|
* A list of the internal exception types that should not be reported. |
41
|
11 |
|
* |
42
|
|
|
* @var array |
|
|
|
|
43
|
11 |
|
*/ |
44
|
11 |
|
protected $internalDontReport = [ |
45
|
|
|
AuthenticationException::class, |
46
|
|
|
AuthorizationException::class, |
47
|
|
|
HttpException::class, |
|
|
|
|
48
|
|
|
HttpResponseException::class, |
|
|
|
|
49
|
|
|
ModelNotFoundException::class, |
50
|
|
|
SuspiciousOperationException::class, |
51
|
|
|
// TokenMismatchException::class, |
52
|
|
|
ValidationException::class, |
53
|
11 |
|
]; |
54
|
|
|
|
55
|
11 |
|
/** |
56
|
|
|
* Report or log an exception. |
57
|
|
|
* |
58
|
11 |
|
* @param \Exception $exception |
|
|
|
|
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function report(Exception $exception) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if ($exception instanceof TokenMismatchException) { |
64
|
|
|
$logData = [ |
65
|
|
|
'requestToken' => request()->header('x-csrf-token'), |
66
|
|
|
'sessionToken' => session()->token(), |
67
|
|
|
'session' => session()->all(), |
68
|
1 |
|
'user' => request()->user(), |
69
|
|
|
'requestUrl' => request()->url() |
70
|
1 |
|
]; |
71
|
|
|
$message = '419 CSRF Token Mismatch. ' . collect($logData)->toJson(); |
72
|
|
|
Log::debug($message); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
parent::report($exception); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
/** |
79
|
|
|
* OVERRIDE |
80
|
|
|
* Get the default context variables for logging. |
81
|
|
|
* |
82
|
|
|
* @return array |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
protected function context() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
try { |
87
|
|
|
return array_filter([ |
88
|
2 |
|
'userId' => Auth::id(), |
89
|
|
|
// 'email' => optional(Auth::user())->email, |
90
|
2 |
|
'url' => Request::path(), |
91
|
2 |
|
'method' => Request::method(), |
92
|
2 |
|
'referer' => Request::header('referer', '') |
93
|
2 |
|
]); |
94
|
2 |
|
} catch (Throwable $e) { |
|
|
|
|
95
|
|
|
return []; |
96
|
|
|
} |
97
|
|
|
} |
98
|
2 |
|
|
99
|
|
|
/** |
100
|
|
|
* Render an exception into an HTTP response. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
103
|
|
|
* @param \Exception $exception |
|
|
|
|
104
|
|
|
* @return \Illuminate\Http\Response |
105
|
|
|
*/ |
106
|
|
|
public function render($request, Exception $exception) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
if ($exception instanceof AdminException) { |
109
|
|
|
return $exception->render($request); |
110
|
|
|
} |
111
|
|
|
if ($exception instanceof TokenMismatchException) { |
112
|
|
|
$newMessage = $exception->getMessage() . ' ' . Lang::get('errors.refresh_page'); |
113
|
|
|
$modifiedException = new TokenMismatchException($newMessage, $exception->getCode(), $exception); |
114
|
|
|
return parent::render($request, $modifiedException); |
115
|
|
|
} |
116
|
|
|
return parent::render($request, $exception); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Convert an authentication exception into an unauthenticated response. |
121
|
|
|
* |
122
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
123
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
|
|
|
|
124
|
|
|
* @return \Illuminate\Http\Response |
125
|
|
|
*/ |
126
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if ($request->expectsJson()) { |
129
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
130
|
|
|
} |
131
|
|
|
if (WhichPortal::isManagerPortal()) { |
132
|
|
|
$loginRoute = route('manager.login'); |
133
|
|
|
} else { |
134
|
|
|
$loginRoute = route('login'); |
135
|
|
|
} |
136
|
|
|
return redirect()->guest($loginRoute); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* OVERRIDE |
141
|
|
|
* Render the given HttpException. |
142
|
|
|
* |
143
|
|
|
* @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e |
|
|
|
|
144
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
145
|
|
|
*/ |
146
|
|
|
protected function renderHttpException(HttpExceptionInterface $e) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
if (!view()->exists("errors.{$e->getStatusCode()}")) { |
149
|
|
|
return response()->view('errors.default', [ |
150
|
|
|
'exception' => $e, |
151
|
|
|
'goc' => Lang::get('common/goc'), |
152
|
|
|
'alert' => Lang::get('common/alert'), |
153
|
|
|
'error' => [ |
154
|
|
|
'title' => 'Error' |
155
|
|
|
] |
156
|
|
|
], $e->getStatusCode(), $e->getHeaders()); |
157
|
|
|
} |
158
|
|
|
return parent::renderHttpException($e); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|