Skip to content

Commit e60b432

Browse files
authored
feat:(LAR-89) add tracking for user (#259)
Mettre en place la possibilité de tracker l'activité d'un utilisateur
2 parents 82fcb62 + bf656c7 commit e60b432

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Middleware;
6+
7+
use Carbon\Carbon;
8+
use Closure;
9+
use Illuminate\Http\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
final class TrackLastActivity
13+
{
14+
public function handle(Request $request, Closure $next): Response
15+
{
16+
if ($user = $request->user()) {
17+
$lastActive = $user->last_active_at;
18+
19+
if (! $lastActive || $lastActive->diffInMinutes(Carbon::now()) >= 3) {
20+
$user->update([
21+
'last_active_at' => now(),
22+
]);
23+
}
24+
}
25+
26+
return $next($request);
27+
}
28+
}

app/Models/User.php

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @property Carbon | null $banned_at
5555
* @property Carbon $created_at
5656
* @property Carbon $updated_at
57+
* @property Carbon | null $last_active_at
5758
* @property Collection | Activity[] $activities
5859
* @property Collection | Article[] $articles
5960
* @property Collection | Thread[] $threads
@@ -96,20 +97,23 @@ final class User extends Authenticatable implements FilamentUser, HasAvatar, Has
9697
'banned_at',
9798
'banned_reason',
9899
'opt_in',
100+
'last_active_at',
99101
];
100102

101103
protected $hidden = [
102104
'password',
103105
'remember_token',
104106
'two_factor_recovery_codes',
105107
'two_factor_secret',
108+
'last_active_at',
106109
];
107110

108111
protected $casts = [
109112
'email_verified_at' => 'datetime',
110113
'last_login_at' => 'datetime',
111114
'banned_at' => 'datetime',
112115
'settings' => 'array',
116+
'last_active_at' => 'datetime',
113117
];
114118

115119
public function hasProvider(string $provider): bool

bootstrap/app.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
]);
2121
$middleware->web(append: [
2222
LocaleMiddleware::class,
23+
\App\Http\Middleware\TrackLastActivity::class,
2324
]);
2425
})
2526
->withExceptions(function (Exceptions $exceptions): void {

composer.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
public function up(): void
12+
{
13+
Schema::table('users', static function (Blueprint $table): void {
14+
$table->timestamp('last_active_at')->nullable()->after('last_login_ip');
15+
});
16+
}
17+
};

tests/Feature/UserActivitiesTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Activity;
66
use App\Models\Article;
77
use Carbon\Carbon;
8+
use Illuminate\Support\Facades\Route;
89

910
it('records activity when an article is created', function (): void {
1011
$user = $this->login();
@@ -39,3 +40,12 @@
3940
Carbon::now()->subWeek()->format('Y-m-d')
4041
));
4142
});
43+
44+
it('does not update the last activity for unauthenticated users', function (): void {
45+
Route::middleware(\App\Http\Middleware\TrackLastActivity::class)
46+
->get('/activity-user', fn () => 'ok');
47+
48+
$this->get('/activity-user')->assertOk();
49+
50+
$this->assertDatabaseMissing('users', ['last_active_at' => now()]);
51+
});

0 commit comments

Comments
 (0)