Skip to content

Commit d521013

Browse files
authored
Merge pull request #265 from cklm/patch-1
Update RequestListener.php
2 parents 58050af + 1156946 commit d521013

File tree

4 files changed

+15
-89
lines changed

4 files changed

+15
-89
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## Unreleased
88
- Fix handling of command with no name on `ConsoleListener` (#261)
9+
- Remove check by AuthorizationChecker in `RequestListener` (#264)
10+
- Fixed undefined variable in `RequestListener` (#263)
911

1012
## 3.2.0 (2019-10-04)
1113

src/EventListener/RequestListener.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
99
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1010
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
12-
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
1311
use Symfony\Component\Security\Core\User\UserInterface;
1412

1513
/**
@@ -24,23 +22,17 @@ final class RequestListener
2422
/** @var TokenStorageInterface|null */
2523
private $tokenStorage;
2624

27-
/** @var AuthorizationCheckerInterface|null */
28-
private $authorizationChecker;
29-
3025
/**
3126
* RequestListener constructor.
3227
* @param HubInterface $hub
3328
* @param TokenStorageInterface|null $tokenStorage
34-
* @param AuthorizationCheckerInterface|null $authorizationChecker
3529
*/
3630
public function __construct(
3731
HubInterface $hub,
38-
?TokenStorageInterface $tokenStorage,
39-
?AuthorizationCheckerInterface $authorizationChecker
32+
?TokenStorageInterface $tokenStorage
4033
) {
4134
$this->hub = $hub; // not used, needed to trigger instantiation
4235
$this->tokenStorage = $tokenStorage;
43-
$this->authorizationChecker = $authorizationChecker;
4436
}
4537

4638
/**
@@ -65,11 +57,12 @@ public function onKernelRequest(GetResponseEvent $event): void
6557
$token = $this->tokenStorage->getToken();
6658
}
6759

60+
$userData = [];
61+
6862
if (
6963
null !== $token
70-
&& null !== $this->authorizationChecker
7164
&& $token->isAuthenticated()
72-
&& $this->authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
65+
&& $token->getUser()
7366
) {
7467
$userData = $this->getUserData($token->getUser());
7568
}

src/Resources/config/services.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<service id="Sentry\SentryBundle\EventListener\RequestListener" class="Sentry\SentryBundle\EventListener\RequestListener" public="false">
4141
<argument type="service" id="Sentry\State\HubInterface" />
4242
<argument type="service" id="security.token_storage" on-invalid="ignore" />
43-
<argument type="service" id="security.authorization_checker" on-invalid="ignore" />
4443

4544
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="%sentry.listener_priorities.request%" />
4645
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" priority="%sentry.listener_priorities.request%" />

test/EventListener/RequestListenerTest.php

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1717
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1818
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20-
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
2119
use Symfony\Component\Security\Core\User\UserInterface;
2220

2321
class RequestListenerTest extends TestCase
@@ -59,7 +57,6 @@ protected function setUp()
5957
public function testOnKernelRequestUserDataIsSetToScope($user): void
6058
{
6159
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
62-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
6360
$event = $this->prophesize(GetResponseEvent::class);
6461
$request = $this->prophesize(Request::class);
6562
$token = $this->prophesize(TokenInterface::class);
@@ -72,8 +69,6 @@ public function testOnKernelRequestUserDataIsSetToScope($user): void
7269

7370
$token->isAuthenticated()
7471
->willReturn(true);
75-
$authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
76-
->willReturn(true);
7772

7873
$token->getUser()
7974
->willReturn($user);
@@ -85,8 +80,7 @@ public function testOnKernelRequestUserDataIsSetToScope($user): void
8580

8681
$listener = new RequestListener(
8782
$this->currentHub->reveal(),
88-
$tokenStorage->reveal(),
89-
$authorizationChecker->reveal()
83+
$tokenStorage->reveal()
9084
);
9185

9286
$listener->onKernelRequest($event->reveal());
@@ -113,7 +107,6 @@ public function userDataProvider(): \Generator
113107
public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
114108
{
115109
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
116-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
117110
$event = $this->prophesize(GetResponseEvent::class);
118111

119112
$event->isMasterRequest()
@@ -126,8 +119,7 @@ public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
126119

127120
$listener = new RequestListener(
128121
$this->currentHub->reveal(),
129-
$tokenStorage->reveal(),
130-
$authorizationChecker->reveal()
122+
$tokenStorage->reveal()
131123
);
132124

133125
$listener->onKernelRequest($event->reveal());
@@ -138,7 +130,6 @@ public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
138130
public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
139131
{
140132
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
141-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
142133
$event = $this->prophesize(GetResponseEvent::class);
143134

144135
$event->isMasterRequest()
@@ -151,8 +142,7 @@ public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
151142

152143
$listener = new RequestListener(
153144
$this->currentHub->reveal(),
154-
$tokenStorage->reveal(),
155-
$authorizationChecker->reveal()
145+
$tokenStorage->reveal()
156146
);
157147

158148
$listener->onKernelRequest($event->reveal());
@@ -162,55 +152,19 @@ public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
162152

163153
public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): void
164154
{
165-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
166-
$event = $this->prophesize(GetResponseEvent::class);
167-
$request = $this->prophesize(Request::class);
168-
169-
$event->isMasterRequest()
170-
->willReturn(true);
171-
172-
$authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
173-
->shouldNotBeCalled();
174-
175-
$event->getRequest()
176-
->willReturn($request->reveal());
177-
$request->getClientIp()
178-
->willReturn('1.2.3.4');
179-
180-
$listener = new RequestListener(
181-
$this->currentHub->reveal(),
182-
null,
183-
$authorizationChecker->reveal()
184-
);
185-
186-
$listener->onKernelRequest($event->reveal());
187-
188-
$expectedUserData = [
189-
'ip_address' => '1.2.3.4',
190-
];
191-
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
192-
}
193-
194-
public function testOnKernelRequestUsernameIsNotSetIfAuthorizationCheckerIsAbsent(): void
195-
{
196-
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
197155
$event = $this->prophesize(GetResponseEvent::class);
198156
$request = $this->prophesize(Request::class);
199157

200158
$event->isMasterRequest()
201159
->willReturn(true);
202160

203-
$tokenStorage->getToken()
204-
->willReturn($this->prophesize(TokenInterface::class)->reveal());
205-
206161
$event->getRequest()
207162
->willReturn($request->reveal());
208163
$request->getClientIp()
209164
->willReturn('1.2.3.4');
210165

211166
$listener = new RequestListener(
212167
$this->currentHub->reveal(),
213-
$tokenStorage->reveal(),
214168
null
215169
);
216170

@@ -225,7 +179,6 @@ public function testOnKernelRequestUsernameIsNotSetIfAuthorizationCheckerIsAbsen
225179
public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
226180
{
227181
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
228-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
229182
$event = $this->prophesize(GetResponseEvent::class);
230183
$request = $this->prophesize(Request::class);
231184

@@ -235,18 +188,14 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
235188
$tokenStorage->getToken()
236189
->willReturn(null);
237190

238-
$authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
239-
->shouldNotBeCalled();
240-
241191
$event->getRequest()
242192
->willReturn($request->reveal());
243193
$request->getClientIp()
244194
->willReturn('1.2.3.4');
245195

246196
$listener = new RequestListener(
247197
$this->currentHub->reveal(),
248-
$tokenStorage->reveal(),
249-
$authorizationChecker->reveal()
198+
$tokenStorage->reveal()
250199
);
251200

252201
$listener->onKernelRequest($event->reveal());
@@ -263,7 +212,6 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
263212
public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated(): void
264213
{
265214
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
266-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
267215
$token = $this->prophesize(TokenInterface::class);
268216
$event = $this->prophesize(GetResponseEvent::class);
269217
$request = $this->prophesize(Request::class);
@@ -277,18 +225,14 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated():
277225
$token->isAuthenticated()
278226
->willReturn(false);
279227

280-
$authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
281-
->shouldNotBeCalled();
282-
283228
$event->getRequest()
284229
->willReturn($request->reveal());
285230
$request->getClientIp()
286231
->willReturn('1.2.3.4');
287232

288233
$listener = new RequestListener(
289234
$this->currentHub->reveal(),
290-
$tokenStorage->reveal(),
291-
$authorizationChecker->reveal()
235+
$tokenStorage->reveal()
292236
);
293237

294238
$listener->onKernelRequest($event->reveal());
@@ -302,7 +246,6 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated():
302246
public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
303247
{
304248
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
305-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
306249
$event = $this->prophesize(GetResponseEvent::class);
307250
$request = $this->prophesize(Request::class);
308251

@@ -312,18 +255,14 @@ public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
312255
$tokenStorage->getToken()
313256
->willReturn(null);
314257

315-
$authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
316-
->willReturn(false);
317-
318258
$event->getRequest()
319259
->willReturn($request->reveal());
320260
$request->getClientIp()
321261
->willReturn('1.2.3.4');
322262

323263
$listener = new RequestListener(
324264
$this->currentHub->reveal(),
325-
$tokenStorage->reveal(),
326-
$authorizationChecker->reveal()
265+
$tokenStorage->reveal()
327266
);
328267

329268
$listener->onKernelRequest($event->reveal());
@@ -347,8 +286,7 @@ public function testOnKernelControllerAddsRouteTag(): void
347286

348287
$listener = new RequestListener(
349288
$this->currentHub->reveal(),
350-
$this->prophesize(TokenStorageInterface::class)->reveal(),
351-
$this->prophesize(AuthorizationCheckerInterface::class)->reveal()
289+
$this->prophesize(TokenStorageInterface::class)->reveal()
352290
);
353291

354292
$listener->onKernelController($event->reveal());
@@ -371,8 +309,7 @@ public function testOnKernelControllerRouteTagIsNotSetIfRequestDoesNotHaveARoute
371309

372310
$listener = new RequestListener(
373311
$this->currentHub->reveal(),
374-
$this->prophesize(TokenStorageInterface::class)->reveal(),
375-
$this->prophesize(AuthorizationCheckerInterface::class)->reveal()
312+
$this->prophesize(TokenStorageInterface::class)->reveal()
376313
);
377314

378315
$listener->onKernelController($event->reveal());
@@ -384,7 +321,6 @@ public function testOnKernelRequestUserDataAndTagsAreNotSetInSubRequest(): void
384321
->shouldNotBeCalled();
385322

386323
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
387-
$authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);
388324
$event = $this->prophesize(GetResponseEvent::class);
389325

390326
$event->isMasterRequest()
@@ -393,13 +329,9 @@ public function testOnKernelRequestUserDataAndTagsAreNotSetInSubRequest(): void
393329
$tokenStorage->getToken()
394330
->shouldNotBeCalled();
395331

396-
$authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
397-
->shouldNotBeCalled();
398-
399332
$listener = new RequestListener(
400333
$this->currentHub->reveal(),
401-
$tokenStorage->reveal(),
402-
$authorizationChecker->reveal()
334+
$tokenStorage->reveal()
403335
);
404336

405337
$listener->onKernelRequest($event->reveal());

0 commit comments

Comments
 (0)