Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lrljoe authored Aug 16, 2024
1 parent b4322c9 commit 8c3f10f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions config/authentication-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

// The database connection where the authentication_log table resides. Leave empty to use the default
'db_connection' => null,

// The model to use for Authentication Log
'model'=> \Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog::class,

// The events the package listens for to log
'events' => [
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/PurgeAuthenticationLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Rappasoft\LaravelAuthenticationLog\Commands;

use Illuminate\Console\Command;
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;

class PurgeAuthenticationLogCommand extends Command
{
Expand All @@ -15,7 +14,7 @@ public function handle(): void
{
$this->comment('Clearing authentication log...');

$deleted = AuthenticationLog::where('login_at', '<', now()->subDays(config('authentication-log.purge'))->format('Y-m-d H:i:s'))->delete();
$deleted = config('authentication-log.model')::where('login_at', '<', now()->subDays(config('authentication-log.purge'))->format('Y-m-d H:i:s'))->delete();

$this->info($deleted . ' authentication logs cleared.');
}
Expand Down
5 changes: 3 additions & 2 deletions src/Listeners/LogoutListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Auth\Events\Logout;
use Illuminate\Http\Request;
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;

class LogoutListener
Expand Down Expand Up @@ -41,7 +40,9 @@ public function handle($event): void
$log = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->orderByDesc('login_at')->first();

if (! $log) {
$log = new AuthenticationLog([
$model = config('authentication-log.model');

$log = new $model([
'ip_address' => $ip,
'user_agent' => $userAgent,
]);
Expand Down
5 changes: 3 additions & 2 deletions src/Listeners/OtherDeviceLogoutListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Auth\Events\OtherDeviceLogout;
use Illuminate\Http\Request;
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;

class OtherDeviceLogoutListener
Expand Down Expand Up @@ -41,7 +40,9 @@ public function handle($event): void
$authenticationLog = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->first();

if (! $authenticationLog) {
$authenticationLog = new AuthenticationLog([
$model = config('authentication-log.model');

$authenticationLog = new $model([
'ip_address' => $ip,
'user_agent' => $userAgent,
]);
Expand Down
6 changes: 2 additions & 4 deletions src/Traits/AuthenticationLoggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace Rappasoft\LaravelAuthenticationLog\Traits;

use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;

trait AuthenticationLoggable
{
public function authentications()
{
return $this->morphMany(AuthenticationLog::class, 'authenticatable')->latest('login_at');
return $this->morphMany(config('authentication-log.model'), 'authenticatable')->latest('login_at');
}

public function latestAuthentication()
{
return $this->morphOne(AuthenticationLog::class, 'authenticatable')->latestOfMany('login_at');
return $this->morphOne(config('authentication-log.model'), 'authenticatable')->latestOfMany('login_at');
}

public function notifyAuthenticationLogVia(): array
Expand Down

0 comments on commit 8c3f10f

Please sign in to comment.