|
2 | 2 |
|
3 | 3 | namespace Sentry\SentryBundle\EventListener;
|
4 | 4 |
|
| 5 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
5 | 6 | use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
6 | 7 | use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
| 8 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 9 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 10 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
| 11 | +use Symfony\Component\Security\Core\User\UserInterface; |
7 | 12 |
|
| 13 | +/** |
| 14 | + * Class ExceptionListener |
| 15 | + * @package Sentry\SentryBundle\EventListener |
| 16 | + */ |
8 | 17 | class ExceptionListener
|
9 | 18 | {
|
10 |
| - public function __construct(\Raven_Client $client = null) |
11 |
| - { |
| 19 | + /** @var TokenStorageInterface */ |
| 20 | + private $tokenStorage; |
| 21 | + |
| 22 | + /** @var AuthorizationCheckerInterface */ |
| 23 | + private $authorizationChecker; |
| 24 | + |
| 25 | + /** @var \Raven_Client */ |
| 26 | + private $client; |
| 27 | + |
| 28 | + /** |
| 29 | + * ExceptionListener constructor. |
| 30 | + * @param TokenStorageInterface $tokenStorage |
| 31 | + * @param AuthorizationCheckerInterface $authorizationChecker |
| 32 | + * @param \Raven_Client $client |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + TokenStorageInterface $tokenStorage, |
| 36 | + AuthorizationCheckerInterface $authorizationChecker, |
| 37 | + \Raven_Client $client = null |
| 38 | + ) { |
12 | 39 | if (!$client) {
|
13 | 40 | $client = new \Raven_Client();
|
14 | 41 | }
|
| 42 | + |
| 43 | + $this->tokenStorage = $tokenStorage; |
| 44 | + $this->authorizationChecker = $authorizationChecker; |
15 | 45 | $this->client = $client;
|
16 | 46 | }
|
17 | 47 |
|
| 48 | + /** |
| 49 | + * @param \Raven_Client $client |
| 50 | + */ |
18 | 51 | public function setClient(\Raven_Client $client)
|
19 | 52 | {
|
20 | 53 | $this->client = $client;
|
21 | 54 | }
|
22 | 55 |
|
| 56 | + /** |
| 57 | + * Set the username from the security context by listening on core.request |
| 58 | + * |
| 59 | + * @param GetResponseEvent $event |
| 60 | + */ |
| 61 | + public function onKernelRequest(GetResponseEvent $event) |
| 62 | + { |
| 63 | + if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (null === $this->tokenStorage || null === $this->authorizationChecker) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + $token = $this->tokenStorage->getToken(); |
| 72 | + if (null !== $token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
| 73 | + $this->setUserValue($token->getUser()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param GetResponseForExceptionEvent $event |
| 79 | + */ |
23 | 80 | public function onKernelException(GetResponseForExceptionEvent $event)
|
24 | 81 | {
|
25 | 82 | $exception = $event->getException();
|
26 | 83 |
|
27 |
| - // dont capture HTTP responses |
28 |
| - if ($exception instanceof HttpException) { |
| 84 | + // don't capture HTTP responses |
| 85 | + if ($exception instanceof HttpExceptionInterface) { |
29 | 86 | return;
|
30 | 87 | }
|
31 | 88 |
|
32 | 89 | $this->client->captureException($exception);
|
33 | 90 | }
|
| 91 | + |
| 92 | + /** |
| 93 | + * @param UserInterface | object | string $user |
| 94 | + */ |
| 95 | + private function setUserValue($user) |
| 96 | + { |
| 97 | + switch (true) { |
| 98 | + case $user instanceof UserInterface: |
| 99 | + $this->client->set_user_data($user->getUsername()); |
| 100 | + return; |
| 101 | + case is_object($user): |
| 102 | + case is_string($user): |
| 103 | + $this->client->set_user_data((string) $user); |
| 104 | + return; |
| 105 | + } |
| 106 | + } |
34 | 107 | }
|
0 commit comments