44
55use Bugsnag \BugsnagBundle \BugsnagBundle ;
66use Bugsnag \BugsnagBundle \Request \SymfonyResolver ;
7+ use Bugsnag \Callbacks \CustomUser ;
78use Bugsnag \Client ;
89use 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 ;
913
1014class ClientFactory
1115{
@@ -16,10 +20,24 @@ class ClientFactory
1620 */
1721 protected $ resolver ;
1822
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+
1937 /**
2038 * The api key.
2139 *
22- * @var string
40+ * @var string|null
2341 */
2442 protected $ key ;
2543
@@ -37,6 +55,13 @@ class ClientFactory
3755 */
3856 protected $ callbacks ;
3957
58+ /**
59+ * User detection enabled.
60+ *
61+ * @var bool
62+ */
63+ protected $ user ;
64+
4065 /**
4166 * The type.
4267 *
@@ -117,29 +142,35 @@ class ClientFactory
117142 /**
118143 * Create a new client factory instance.
119144 *
120- * @param \Bugsnag\BugsnagBundle\Request\SymfonyResolver $resolver
121- * @param string $key
122- * @param string|null $endpoint
123- * @param bool $callbacks
124- * @param string|null $type
125- * @param string|null $version
126- * @param bool $batch
127- * @param string|null $hostname
128- * @param bool $code
129- * @param string|null $strip
130- * @param string|null $project
131- * @param string|null $root
132- * @param string|null $stage
133- * @param string[]|null $stages
134- * @param string[]|null $filters
145+ * @param \Bugsnag\BugsnagBundle\Request\SymfonyResolver $resolver
146+ * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|null $tokens
147+ * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null $checker
148+ * @param string|null $key
149+ * @param string|null $endpoint
150+ * @param bool $callbacks
151+ * @param bool $user
152+ * @param string|null $type
153+ * @param string|null $version
154+ * @param bool $batch
155+ * @param string|null $hostname
156+ * @param bool $code
157+ * @param string|null $strip
158+ * @param string|null $project
159+ * @param string|null $root
160+ * @param string|null $stage
161+ * @param string[]|null $stages
162+ * @param string[]|null $filters
135163 *
136164 * @return void
137165 */
138166 public function __construct (
139167 SymfonyResolver $ resolver ,
140- $ key ,
168+ TokenStorageInterface $ tokens = null ,
169+ AuthorizationCheckerInterface $ checker = null ,
170+ $ key = null ,
141171 $ endpoint = null ,
142172 $ callbacks = true ,
173+ $ user = true ,
143174 $ type = null ,
144175 $ version = true ,
145176 $ batch = null ,
@@ -153,9 +184,12 @@ public function __construct(
153184 array $ filters = null
154185 ) {
155186 $ this ->resolver = $ resolver ;
187+ $ this ->tokens = $ tokens ;
188+ $ this ->checker = $ checker ;
156189 $ this ->key = $ key ;
157190 $ this ->endpoint = $ endpoint ;
158191 $ this ->callbacks = $ callbacks ;
192+ $ this ->user = $ user ;
159193 $ this ->type = $ type ;
160194 $ this ->version = $ version ;
161195 $ this ->batch = $ batch ;
@@ -178,12 +212,16 @@ public function make()
178212 {
179213 $ guzzle = Client::makeGuzzle ($ this ->endpoint );
180214
181- $ client = new Client (new Config ($ this ->key ), $ this ->resolver , $ guzzle );
215+ $ client = new Client (new Config ($ this ->key ?: '' ), $ this ->resolver , $ guzzle );
182216
183217 if ($ this ->callbacks ) {
184218 $ client ->registerDefaultCallbacks ();
185219 }
186220
221+ if ($ this ->tokens && $ this ->checker && $ this ->user ) {
222+ $ this ->setupUserDetection ($ client , $ this ->tokens , $ this ->checker );
223+ }
224+
187225 $ this ->setupPaths ($ client , $ this ->strip , $ this ->project , $ this ->root );
188226
189227 $ client ->setReleaseStage ($ this ->stage === 'prod ' ? 'production ' : $ this ->stage );
@@ -213,6 +251,34 @@ public function make()
213251 return $ client ;
214252 }
215253
254+ /**
255+ * Setup user detection.
256+ *
257+ * @param \Bugsnag\Client $client
258+ * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokens
259+ * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $checker
260+ *
261+ * @return void
262+ */
263+ protected function setupUserDetection (Client $ client , TokenStorageInterface $ tokens , AuthorizationCheckerInterface $ checker )
264+ {
265+ $ client ->registerCallback (new CustomUser (function () use ($ tokens , $ checker ) {
266+ $ token = $ tokens ->getToken ();
267+
268+ if (!$ token || !$ checker ->isGranted ('IS_AUTHENTICATED_REMEMBERED ' )) {
269+ return ;
270+ }
271+
272+ $ user = $ token ->getUser ();
273+
274+ if ($ user instanceof UserInterface) {
275+ return ['id ' => $ user ->getUsername ()];
276+ }
277+
278+ return ['id ' => (string ) $ user ];
279+ }));
280+ }
281+
216282 /**
217283 * Setup the client paths.
218284 *
@@ -223,7 +289,7 @@ public function make()
223289 *
224290 * @return void
225291 */
226- protected function setupPaths ($ client , $ strip , $ project , $ root )
292+ protected function setupPaths (Client $ client , $ strip , $ project , $ root )
227293 {
228294 if ($ strip ) {
229295 $ client ->setStripPath ($ strip );
0 commit comments