Skip to content

Commit bdda099

Browse files
author
Sam Stevens
committed
refactor the user setting code into a callback
1 parent 2b4bd77 commit bdda099

File tree

3 files changed

+82
-69
lines changed

3 files changed

+82
-69
lines changed

Callbacks/UserSettingCallback.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Bugsnag\BugsnagBundle\Callbacks;
4+
5+
use Bugsnag\Report;
6+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Symfony\Component\Security\Core\User\UserInterface;
9+
10+
class UserSettingCallback
11+
{
12+
/**
13+
* The token resolver.
14+
*
15+
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|null
16+
*/
17+
protected $tokens;
18+
19+
/**
20+
* The auth checker.
21+
*
22+
* @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null
23+
*/
24+
protected $checker;
25+
26+
/**
27+
* @var bool
28+
*/
29+
protected $setUser;
30+
31+
/**
32+
* @param null|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokens
33+
* @param null|\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $checker
34+
* @param bool $setUser
35+
*/
36+
public function __construct(
37+
TokenStorageInterface $tokens = null,
38+
AuthorizationCheckerInterface $checker = null,
39+
$setUser = true
40+
) {
41+
$this->tokens = $tokens;
42+
$this->checker = $checker;
43+
$this->setUser = $setUser;
44+
}
45+
46+
/**
47+
* @param \Bugsnag\Report $report
48+
*/
49+
public function registerCallback(Report $report)
50+
{
51+
// If told to not set the user, or the security services were not passed in
52+
// (not registered in the container), then exit early
53+
if (! $this->setUser || is_null($this->tokens) || is_null($this->checker)) {
54+
return;
55+
}
56+
57+
$token = $this->tokens->getToken();
58+
59+
if (!$token || !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
60+
return;
61+
}
62+
63+
$user = $token->getUser();
64+
65+
if ($user instanceof UserInterface) {
66+
$bugsnagUser = ['id' => $user->getUsername()];
67+
} else {
68+
$bugsnagUser = ['id' => (string) $user];
69+
}
70+
71+
$report->setUser($bugsnagUser);
72+
}
73+
}

DependencyInjection/ClientFactory.php

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55
use Bugsnag\BugsnagBundle\BugsnagBundle;
66
use Bugsnag\BugsnagBundle\Request\SymfonyResolver;
7-
use Bugsnag\Callbacks\CustomUser;
87
use Bugsnag\Client;
98
use Bugsnag\Configuration as Config;
10-
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
12-
use Symfony\Component\Security\Core\User\UserInterface;
139

1410
class ClientFactory
1511
{
@@ -20,20 +16,6 @@ class ClientFactory
2016
*/
2117
protected $resolver;
2218

23-
/**
24-
* The token resolver.
25-
*
26-
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|null
27-
*/
28-
protected $tokens;
29-
30-
/**
31-
* The auth checker.
32-
*
33-
* @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null
34-
*/
35-
protected $checker;
36-
3719
/**
3820
* The api key.
3921
*
@@ -55,13 +37,6 @@ class ClientFactory
5537
*/
5638
protected $callbacks;
5739

58-
/**
59-
* User detection enabled.
60-
*
61-
* @var bool
62-
*/
63-
protected $user;
64-
6540
/**
6641
* The type.
6742
*
@@ -150,12 +125,9 @@ class ClientFactory
150125
* Create a new client factory instance.
151126
*
152127
* @param \Bugsnag\BugsnagBundle\Request\SymfonyResolver $resolver
153-
* @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|null $tokens
154-
* @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null $checker
155128
* @param string|null $key
156129
* @param string|null $endpoint
157130
* @param bool $callbacks
158-
* @param bool $user
159131
* @param string|null $type
160132
* @param string|null $version
161133
* @param bool $batch
@@ -172,12 +144,9 @@ class ClientFactory
172144
*/
173145
public function __construct(
174146
SymfonyResolver $resolver,
175-
TokenStorageInterface $tokens = null,
176-
AuthorizationCheckerInterface $checker = null,
177147
$key = null,
178148
$endpoint = null,
179149
$callbacks = true,
180-
$user = true,
181150
$type = null,
182151
$version = true,
183152
$batch = null,
@@ -191,12 +160,9 @@ public function __construct(
191160
array $filters = null
192161
) {
193162
$this->resolver = $resolver;
194-
$this->tokens = $tokens;
195-
$this->checker = $checker;
196163
$this->key = $key;
197164
$this->endpoint = $endpoint;
198165
$this->callbacks = $callbacks;
199-
$this->user = $user;
200166
$this->type = $type;
201167
$this->version = $version;
202168
$this->batch = $batch;
@@ -245,10 +211,6 @@ public function make()
245211
$client->registerDefaultCallbacks();
246212
}
247213

248-
if ($this->tokens && $this->checker && $this->user) {
249-
$this->setupUserDetection($client, $this->tokens, $this->checker);
250-
}
251-
252214
$this->setupPaths($client, $this->strip, $this->project, $this->root);
253215

254216
$client->setReleaseStage($this->stage === 'prod' ? 'production' : $this->stage);
@@ -282,34 +244,6 @@ public function make()
282244
return $client;
283245
}
284246

285-
/**
286-
* Setup user detection.
287-
*
288-
* @param \Bugsnag\Client $client
289-
* @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokens
290-
* @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $checker
291-
*
292-
* @return void
293-
*/
294-
protected function setupUserDetection(Client $client, TokenStorageInterface $tokens, AuthorizationCheckerInterface $checker)
295-
{
296-
$client->registerCallback(new CustomUser(function () use ($tokens, $checker) {
297-
$token = $tokens->getToken();
298-
299-
if (!$token || !$checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
300-
return;
301-
}
302-
303-
$user = $token->getUser();
304-
305-
if ($user instanceof UserInterface) {
306-
return ['id' => $user->getUsername()];
307-
}
308-
309-
return ['id' => (string) $user];
310-
}));
311-
}
312-
313247
/**
314248
* Setup the client paths.
315249
*

Resources/config/services.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ services:
66
class: '%bugsnag.factory%'
77
arguments:
88
- '@bugsnag.resolver'
9-
- '@?security.token_storage'
10-
- '@?security.authorization_checker'
119
- '%bugsnag.api_key%'
1210
- '%bugsnag.endpoint%'
1311
- '%bugsnag.callbacks%'
14-
- '%bugsnag.user%'
1512
- '%bugsnag.app_type%'
1613
- '%bugsnag.app_version%'
1714
- '%bugsnag.batch_sending%'
@@ -38,3 +35,12 @@ services:
3835
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 256 }
3936
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 128 }
4037
- { name: kernel.event_listener, event: console.exception, method: onConsoleException, priority: 128 }
38+
39+
bugsnag.callbacks.user_setting:
40+
class: Bugsnag\BugsnagBundle\Callbacks\UserSettingCallback
41+
arguments:
42+
- '@?security.token_storage'
43+
- '@?security.authorization_checker'
44+
- '%bugsnag.user%'
45+
tags:
46+
- { name: 'bugsnag.callback' }

0 commit comments

Comments
 (0)