Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function definition(): array
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'login_count' => 0,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dateTime('last_login_at')->nullable();
$table->integer('login_count')->default(0);
});
}

public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['last_login_at', 'login_count']);
});
}
};
11 changes: 9 additions & 2 deletions src/EclipseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Eclipse\Core\Console\Commands\ClearCommand;
use Eclipse\Core\Console\Commands\DeployCommand;
use Eclipse\Core\Console\Commands\PostComposerUpdate;
use Eclipse\Core\Models\User;
use Eclipse\Core\Providers\AdminPanelProvider;
use Eclipse\Core\Providers\TelescopeServiceProvider;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Event;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand Down Expand Up @@ -40,6 +41,12 @@ public function register(): self

require_once __DIR__.'/Helpers/helpers.php';

Event::listen(Login::class, function ($event) {
if ($event->user instanceof User) {
$event->user->updateLoginTracking();
}
});

$this->app->register(AdminPanelProvider::class);

if ($this->app->environment('local')) {
Expand Down
17 changes: 16 additions & 1 deletion src/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public static function table(Table $table): Table
->searchable()
->sortable()
->toggleable(),
Tables\Columns\TextColumn::make('last_login_at')
->label('Last login')
->dateTime()
->sortable()
->toggleable(),
Tables\Columns\TextColumn::make('login_count')
->label('Total Logins')
->sortable()
->numeric()
->formatStateUsing(fn (?int $state) => $state ?? 0),
];

if (config('eclipse.email_verification')) {
Expand Down Expand Up @@ -145,6 +155,10 @@ public static function table(Table $table): Table
->label('Last name'),
TextConstraint::make('name')
->label('Full name'),
TextConstraint::make('last_login_at')
->label('Last login Date'),
TextConstraint::make('login_count')
->label('Total Logins'),
]),
];

Expand All @@ -155,7 +169,8 @@ public static function table(Table $table): Table
Tables\Actions\ActionGroup::make([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make()->disabled(fn (User $user) => $user->id === auth()->user()->id),
Tables\Actions\DeleteAction::make()
->disabled(fn (User $user) => $user->id === auth()->user()->id),
]),
])
->bulkActions([
Expand Down
15 changes: 15 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasMedia,
'last_name',
'email',
'password',
'last_login_at',
'login_count',
];

/**
Expand All @@ -66,6 +68,7 @@ protected function casts(): array
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'last_login_at' => 'datetime',
];
}

Expand Down Expand Up @@ -110,4 +113,16 @@ protected static function booted()
$user->name = trim("$user->first_name $user->last_name");
});
}

/**
* Update the user's last login timestamp and increment login count.
*
* @return void
*/
public function updateLoginTracking()
{
$this->last_login_at = now();
$this->increment('login_count');
$this->save();
}
}
34 changes: 34 additions & 0 deletions tests/Feature/LoginTrackingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Eclipse\Core\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('new users have no login history', function () {
$user = User::factory()->create();

expect($user->last_login_at)->toBeNull();
expect($user->login_count)->toBe(0);
});

test('user login updates last login timestamp and increments count', function () {
$user = User::factory()->create([
'last_login_at' => null,
'login_count' => 0,
]);

// Simulate login
Auth::login($user);
$user->refresh(); // Reload from DB to reflect changes

expect($user->last_login_at)->not->toBeNull();
expect($user->login_count)->toBe(1);
});

test('guest users do not have login tracking data', function () {
$this->get('/admin')->assertRedirect('admin/login');

expect(Auth::user())->toBeNull();
});