Skip to content

Commit 91c325f

Browse files
committed
Set user on Laravel 4
1 parent 238ff4f commit 91c325f

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

src/Integrations/Integrations/Laravel/LaravelIntegration.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function ($This, $scope, $args) use ($integration) {
398398
// Used by Laravel < 5.0
399399
\DDTrace\hook_method(
400400
'Illuminate\Auth\Guard',
401-
'setUser',
401+
'login',
402402
function ($This, $scope, $args) use ($integration) {
403403
$authClass = 'Illuminate\Auth\UserInterface';
404404
if (
@@ -418,6 +418,7 @@ function ($This, $scope, $args) use ($integration) {
418418
$metadata['email'] = $args[0]['email'];
419419
}
420420

421+
\DDTrace\set_user($args[0]->getAuthIdentifier(), $metadata);
421422
\datadog\appsec\track_user_login_success_event(
422423
\method_exists($args[0], 'getAuthIdentifier') ? $args[0]->getAuthIdentifier() : '',
423424
$metadata,
@@ -426,6 +427,35 @@ function ($This, $scope, $args) use ($integration) {
426427
}
427428
);
428429

430+
// Used by Laravel < 5.0
431+
\DDTrace\hook_method(
432+
'Illuminate\Auth\Guard',
433+
'user',
434+
null,
435+
function ($This, $scope, $args, $user) use ($integration) {
436+
$authClass = 'Illuminate\Auth\UserInterface';
437+
if (
438+
!function_exists('\datadog\appsec\track_user_login_success_event') ||
439+
!isset($user) ||
440+
!$user ||
441+
!($user instanceof $authClass) ||
442+
!\method_exists($user, 'getAuthIdentifier')
443+
) {
444+
return;
445+
}
446+
447+
$metadata = [];
448+
if (isset($user['name'])) {
449+
$metadata['name'] = $user['name'];
450+
}
451+
if (isset($user['email'])) {
452+
$metadata['email'] = $user['email'];
453+
}
454+
455+
\DDTrace\set_user($user->getAuthIdentifier(), $metadata);
456+
}
457+
);
458+
429459
// Used by Laravel < 5.0
430460
\DDTrace\hook_method(
431461
'Illuminate\Auth\Guard',
@@ -461,6 +491,7 @@ function ($This, $scope, $args) use ($integration) {
461491
}
462492
);
463493

494+
// Used by Laravel >= 5.0
464495
\DDTrace\hook_method(
465496
'Illuminate\Auth\Events\Authenticated',
466497
'__construct',

tests/Frameworks/Laravel/Version_4_2/app/controllers/LoginTestController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public function register()
3030

3131
return "registered";
3232
}
33+
34+
public function behind_auth()
35+
{
36+
return "page behind auth";
37+
}
3338
}

tests/Frameworks/Laravel/Version_4_2/app/routes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
Route::get('/eloquent/refresh', 'EloquentTestController@refresh');
2424
Route::get('/login/auth', 'LoginTestController@auth');
2525
Route::get('/login/signup', 'LoginTestController@register');
26+
Route::group(array('before' => 'auth'), function()
27+
{
28+
Route::get('/behind_auth', 'LoginTestController@behind_auth');
29+
});

tests/Integrations/Laravel/V4/LoginEventsTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,20 @@ public function testUserLoginSuccessEvent()
7171
$this->assertEquals('track_user_login_success_event', $events[0]['eventName']);
7272
}
7373

74-
public function testUserLoginSuccessEvent()
74+
public function testLoggedInCalls()
7575
{
7676
$id = 1234;
7777
$name = 'someName';
7878
$email = '[email protected]';
7979
$this->createUser($id, $name, $email);
8080

81-
$this->login($email);
81+
//First log in
82+
$traces = $this->login($email);
83+
84+
$meta = $traces[0][0]['meta'];
85+
$this->assertEquals($id, $meta['usr.id']);
86+
$this->assertEquals($name, $meta['usr.name']);
87+
$this->assertEquals($email, $meta['usr.email']);
8288

8389
$events = AppsecStatus::getInstance()->getEvents();
8490
$this->assertEquals(1, count($events));
@@ -87,6 +93,19 @@ public function testUserLoginSuccessEvent()
8793
$this->assertEquals($email, $events[0]['metadata']['email']);
8894
$this->assertTrue($events[0]['automated']);
8995
$this->assertEquals('track_user_login_success_event', $events[0]['eventName']);
96+
97+
//Now we are logged in lets do another call
98+
AppsecStatus::getInstance()->setDefaults(); //Remove all events
99+
$traces = $this->tracesFromWebRequest(function () {
100+
$this->call(GetSpec::create('Behind auth', '/behind_auth'));
101+
});
102+
103+
$events = AppsecStatus::getInstance()->getEvents();
104+
$this->assertEquals(0, count($events)); //Auth does not generate appsec events
105+
$meta = $traces[0][0]['meta'];
106+
$this->assertEquals($id, $meta['usr.id']);
107+
$this->assertEquals($name, $meta['usr.name']);
108+
$this->assertEquals($email, $meta['usr.email']);
90109
}
91110

92111
public function testUserLoginFailureEvent()

0 commit comments

Comments
 (0)