1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\AssembleResponseEvent; |
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents; |
7
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* ResponseCreationListener. |
14
|
|
|
* |
15
|
|
|
* Default listener which assembles the response for the API key request. |
16
|
|
|
*/ |
17
|
|
|
class ResponseCreationListener implements EventSubscriberInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var TranslatorInterface |
21
|
|
|
*/ |
22
|
|
|
private $translator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ClassMetadata |
26
|
|
|
*/ |
27
|
|
|
private $metadata; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param TranslatorInterface $translator |
33
|
|
|
* @param ClassMetadata $classMetadata |
34
|
|
|
*/ |
35
|
|
|
public function __construct(TranslatorInterface $translator, ClassMetadata $classMetadata) |
36
|
|
|
{ |
37
|
|
|
$this->translator = $translator; |
38
|
|
|
$this->metadata = $classMetadata; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public static function getSubscribedEvents() |
45
|
|
|
{ |
46
|
|
|
return array(Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE => array( |
47
|
|
|
array('onResponseCreation', -10) |
48
|
|
|
)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Assembles the response. |
53
|
|
|
* |
54
|
|
|
* @param AssembleResponseEvent $event |
55
|
|
|
*/ |
56
|
|
|
public function onResponseCreation(AssembleResponseEvent $event) |
57
|
|
|
{ |
58
|
|
|
if ($event->isSuccess()) { |
59
|
|
|
$event->setResponse(new JsonResponse(array( |
60
|
|
|
'apiKey' => $this->metadata->getPropertyValue($event->getUser(), ClassMetadata::API_KEY_PROPERTY) |
|
|
|
|
61
|
|
|
))); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$event->setResponse(new JsonResponse( |
67
|
|
|
array('message' => $this->translator->trans($event->getException()->getMessage() ?: 'Credentials refused!')), |
68
|
|
|
JsonResponse::HTTP_UNAUTHORIZED |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.