Skip to content

Commit ac7cac8

Browse files
estringanaclaude
andcommitted
Fix symfony7x login event tracking with version compat
Use function_exists() to call either the public automated tracking functions (dd-trace-php 1.23+) or the internal equivalents (1.20-1.22), so login/signup events are tracked explicitly in both CI and local builds. Revert makeDistantCall from HttpClient to curl to preserve http.url span tag. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9e89444 commit ac7cac8

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

utils/build/docker/php/weblogs/symfony7x/src/Controller/AppController.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,31 @@ public function makeDistantCall(Request $request): JsonResponse
6969
return new JsonResponse(['error' => 'url parameter required'], 400);
7070
}
7171

72-
$client = HttpClient::create();
73-
$response = $client->request('GET', $url);
74-
$statusCode = $response->getStatusCode();
72+
$requestHeaders = [];
7573
$responseHeaders = [];
76-
foreach ($response->getHeaders(false) as $name => $values) {
77-
$responseHeaders[$name] = implode(', ', $values);
74+
75+
$ch = curl_init($url);
76+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
77+
curl_setopt($ch, CURLOPT_HEADER, false);
78+
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
79+
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
80+
$parts = explode(':', $header, 2);
81+
if (count($parts) === 2) {
82+
$responseHeaders[strtolower(trim($parts[0]))] = trim($parts[1]);
83+
}
84+
return strlen($header);
85+
});
86+
curl_exec($ch);
87+
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
88+
$rawRequestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
89+
curl_close($ch);
90+
91+
foreach (explode("\r\n", $rawRequestHeaders ?: '') as $line) {
92+
$parts = explode(':', $line, 2);
93+
if (count($parts) === 2) {
94+
$requestHeaders[strtolower(trim($parts[0]))] = trim($parts[1]);
95+
}
7896
}
79-
$requestHeaders = $this->parseRequestHeadersFromDebug($response->getInfo('debug') ?? '');
8097

8198
return new JsonResponse([
8299
'url' => $url,

utils/build/docker/php/weblogs/symfony7x/src/Controller/LoginController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ public function signup(Request $request, EntityManagerInterface $entityManager):
4242

4343
$user = new User($id, $username, $hashedPassword, $username);
4444
$entityManager->persist($user);
45-
46-
// Signup event is tracked automatically by the tracer via Doctrine\ORM\UnitOfWork::executeInserts hook.
4745
$entityManager->flush();
4846

47+
if (function_exists('\datadog\appsec\track_user_signup_event_automated')) {
48+
\datadog\appsec\track_user_signup_event_automated('symfony', $username, $id, []);
49+
} elseif (function_exists('\datadog\appsec\internal\track_user_signup_event_automated')) {
50+
\datadog\appsec\internal\track_user_signup_event_automated('symfony', $username, $id, []);
51+
}
52+
4953
$this->security->login($user, AppAuthenticator::class, 'main');
5054

5155
return new Response('', 200);

utils/build/docker/php/weblogs/symfony7x/src/Security/AppAuthenticator.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ public function authenticate(Request $request): Passport
7171

7272
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
7373
{
74-
// Login success is tracked automatically by the tracer via
75-
// AuthenticatorManager::handleAuthenticationSuccess hook.
74+
$user = $token->getUser();
75+
$login = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : '';
76+
$userId = method_exists($user, 'getId') ? (string) $user->getId() : $login;
77+
$this->trackLoginSuccessAutomated($login, $userId);
78+
7679
$sdkTrigger = $request->query->get('sdk_trigger', '');
7780
$sdkEvent = $request->query->get('sdk_event', '');
7881
$sdkUser = $request->query->get('sdk_user', '');
@@ -87,8 +90,11 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
8790

8891
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
8992
{
90-
// Login failure is tracked automatically by the tracer via
91-
// AuthenticatorManager::handleAuthenticationFailure hook.
93+
$login = $request->getSession()?->get('_security.last_username') ?? '';
94+
$exists = !($exception instanceof \Symfony\Component\Security\Core\Exception\UserNotFoundException);
95+
$userId = $exists ? $login : '';
96+
$this->trackLoginFailureAutomated($login, $userId ?: null, $exists);
97+
9298
$sdkTrigger = $request->query->get('sdk_trigger', '');
9399
$sdkEvent = $request->query->get('sdk_event', '');
94100
$sdkUser = $request->query->get('sdk_user', '');
@@ -101,6 +107,24 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
101107
return new Response('', 401);
102108
}
103109

110+
private function trackLoginSuccessAutomated(string $login, string $userId): void
111+
{
112+
if (function_exists('\datadog\appsec\track_user_login_success_event_automated')) {
113+
\datadog\appsec\track_user_login_success_event_automated('symfony', $login, $userId, []);
114+
} elseif (function_exists('\datadog\appsec\internal\track_user_login_success_event_automated')) {
115+
\datadog\appsec\internal\track_user_login_success_event_automated('symfony', $login, $userId, []);
116+
}
117+
}
118+
119+
private function trackLoginFailureAutomated(string $login, ?string $userId, bool $exists): void
120+
{
121+
if (function_exists('\datadog\appsec\track_user_login_failure_event_automated')) {
122+
\datadog\appsec\track_user_login_failure_event_automated('symfony', $login, $userId, $exists, []);
123+
} elseif (function_exists('\datadog\appsec\internal\track_user_login_failure_event_automated')) {
124+
\datadog\appsec\internal\track_user_login_failure_event_automated('symfony', $login, $userId, $exists, []);
125+
}
126+
}
127+
104128
private function callSdk(string $event, string $user, bool $exists): void
105129
{
106130
if ($event === 'success') {

0 commit comments

Comments
 (0)