1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sententiaregum project. |
5
|
|
|
* |
6
|
|
|
* (c) Maximilian Bosch <[email protected]> |
7
|
|
|
* (c) Ben Bieler <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace AppBundle\EventListener; |
14
|
|
|
|
15
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\AssembleResponseEvent; |
16
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Listener for the response lifecycle of the apikey bundle. |
23
|
|
|
* This listener transforms a deeper structure with translations. |
24
|
|
|
* |
25
|
|
|
* 'Message' -> ['de' => ['Meldung'], 'en' => ['Message']] |
26
|
|
|
* |
27
|
|
|
* This is useful for the i18n system of the frontend which needs translations of the security errors. |
28
|
|
|
* |
29
|
|
|
* @author Maximilian Bosch <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class I18nSecurityResponseListener implements EventSubscriberInterface |
32
|
|
|
{ |
33
|
|
|
const AUTH_DEFAULT_ERROR = 'BACKEND_AUTH_FAILURE'; |
34
|
|
|
const TRANSLATION_DOMAIN = 'messages'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var mixed[] |
38
|
|
|
*/ |
39
|
|
|
private $locales = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TranslatorInterface |
43
|
|
|
*/ |
44
|
|
|
private $translator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param string[] $locales |
50
|
|
|
* @param TranslatorInterface $translator |
51
|
|
|
*/ |
52
|
|
|
public function __construct(array $locales, TranslatorInterface $translator) |
53
|
|
|
{ |
54
|
|
|
$this->locales = $locales; |
55
|
|
|
$this->translator = $translator; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public static function getSubscribedEvents() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE => ['onResponseCreation'], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handler for the response creation. |
70
|
|
|
* |
71
|
|
|
* @param AssembleResponseEvent $event |
72
|
|
|
*/ |
73
|
|
|
public function onResponseCreation(AssembleResponseEvent $event) |
74
|
|
|
{ |
75
|
|
|
if ($event->isSuccess()) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$error = $event->getException()->getMessage(); |
80
|
|
|
$translator = $this->translator; |
81
|
|
|
$data = array_combine( |
82
|
|
|
$this->locales, |
83
|
|
|
array_map(function ($locale) use ($translator, $error) { |
84
|
|
|
return $translator->trans($error ?: static::AUTH_DEFAULT_ERROR, [], static::TRANSLATION_DOMAIN, $locale); |
85
|
|
|
}, $this->locales) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$event->setResponse($this->createJSONResponseFromTranslationList($data)); |
89
|
|
|
$event->stopPropagation(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Builds the json response for the i18n translation list. |
94
|
|
|
* |
95
|
|
|
* @param array $data |
96
|
|
|
* |
97
|
|
|
* @return JsonResponse |
98
|
|
|
*/ |
99
|
|
|
private function createJSONResponseFromTranslationList(array $data) |
100
|
|
|
{ |
101
|
|
|
return new JsonResponse( |
102
|
|
|
['message' => $data], |
103
|
|
|
JsonResponse::HTTP_UNAUTHORIZED |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|