|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Controller; |
|
20
|
|
|
|
|
21
|
|
|
use DateTime; |
|
22
|
|
|
use Exception; |
|
23
|
|
|
use Surfnet\StepupBundle\EventListener\RequestIdRequestResponseListener; |
|
24
|
|
|
use Surfnet\StepupBundle\Exception\Art; |
|
25
|
|
|
use Surfnet\StepupBundle\Exception\UserErrorProviderInterface; |
|
26
|
|
|
use Surfnet\StepupBundle\Exception\UserMessageException; |
|
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
30
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
|
32
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
33
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
34
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
35
|
|
|
|
|
36
|
|
|
class ExceptionController extends FrameworkController |
|
37
|
|
|
{ |
|
38
|
|
|
public function showAction(Request $request, Exception $exception) |
|
39
|
|
|
{ |
|
40
|
|
|
$statusCode = $this->getStatusCode($exception); |
|
41
|
|
|
|
|
42
|
|
|
if ($statusCode == 404) { |
|
43
|
|
|
$template = 'SurfnetStepupBundle:Exception:error404.html.twig'; |
|
44
|
|
|
} else { |
|
45
|
|
|
$template = 'SurfnetStepupBundle:Exception:error.html.twig'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$response = new Response('', $statusCode); |
|
49
|
|
|
|
|
50
|
|
|
$timestamp = (new DateTime)->format(DateTime::ISO8601); |
|
51
|
|
|
$hostname = $request->getHost(); |
|
52
|
|
|
$requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-'); |
|
53
|
|
|
$errorCode = Art::forException($exception); |
|
54
|
|
|
$userAgent = $request->headers->get('User-Agent'); |
|
55
|
|
|
$ipAddress = $request->getClientIp(); |
|
56
|
|
|
|
|
57
|
|
|
return $this->render( |
|
58
|
|
|
$template, |
|
59
|
|
|
[ |
|
60
|
|
|
'timestamp' => $timestamp, |
|
61
|
|
|
'hostname' => $hostname, |
|
62
|
|
|
'request_id' => $requestId, |
|
63
|
|
|
'error_code' => $errorCode, |
|
64
|
|
|
'user_agent' => $userAgent, |
|
65
|
|
|
'ip_address' => $ipAddress, |
|
66
|
|
|
] + $this->getPageTitleAndDescription($exception), |
|
67
|
|
|
$response |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Exception $exception |
|
73
|
|
|
* @return int HTTP status code |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getStatusCode(Exception $exception) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($exception instanceof AuthenticationException) { |
|
78
|
|
|
return Response::HTTP_UNAUTHORIZED; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($exception instanceof AccessDeniedException) { |
|
82
|
|
|
return Response::HTTP_FORBIDDEN; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($exception instanceof HttpExceptionInterface) { |
|
86
|
|
|
return $exception->getStatusCode(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Unknown exceptions are server errors! |
|
90
|
|
|
return 500; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Exception $exception |
|
95
|
|
|
* @return array View parameters 'title' and 'description' |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getPageTitleAndDescription(Exception $exception) |
|
98
|
|
|
{ |
|
99
|
|
|
$translator = $this->getTranslator(); |
|
100
|
|
|
$parameters = []; |
|
101
|
|
|
|
|
102
|
|
|
// If the exception wraps a UserErrorProviderInterface, base the error |
|
103
|
|
|
// page on the wrapped exception. This happens when symfony converts |
|
104
|
|
|
// AccessDeniedException objects into AccessDeniedHttpException |
|
105
|
|
|
// objects. |
|
106
|
|
|
if (!$exception instanceof UserErrorProviderInterface && |
|
107
|
|
|
$exception->getPrevious() instanceof UserErrorProviderInterface) { |
|
108
|
|
|
$exception = $exception->getPrevious(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($exception instanceof UserErrorProviderInterface) { |
|
112
|
|
|
$parameters['title'] = $translator->trans( |
|
113
|
|
|
$exception->getErrorPageTitleMessageId() |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$parameters['description'] = $translator->trans( |
|
117
|
|
|
$exception->getErrorPageDescriptionMessageId(), |
|
118
|
|
|
$exception->getErrorPageDescriptionArguments() |
|
119
|
|
|
); |
|
120
|
|
|
} elseif ($exception instanceof AuthenticationException) { |
|
121
|
|
|
$parameters['title'] = $translator->trans('stepup.error.authentication_error_title'); |
|
122
|
|
|
$parameters['description'] = $translator->trans('stepup.error.authentication_error_description'); |
|
123
|
|
|
} else { |
|
124
|
|
|
$parameters['title'] = $translator->trans('stepup.error.generic_error_title'); |
|
125
|
|
|
$parameters['description'] = $translator->trans('stepup.error.generic_error_description'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $parameters; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return TranslatorInterface |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getTranslator() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->get('translator'); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|