diff --git a/config/authentication-log.php b/config/authentication-log.php index bcb0a4e..ea6b6e0 100644 --- a/config/authentication-log.php +++ b/config/authentication-log.php @@ -4,6 +4,7 @@ // The database table name // You can change this if the database keys get too long for your driver 'table_name' => 'authentication_log', + 'model'=> \Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog::class, // The database connection where the authentication_log table resides. Leave empty to use the default 'db_connection' => null, diff --git a/src/Commands/PurgeAuthenticationLogCommand.php b/src/Commands/PurgeAuthenticationLogCommand.php index a3a84c9..87baf32 100644 --- a/src/Commands/PurgeAuthenticationLogCommand.php +++ b/src/Commands/PurgeAuthenticationLogCommand.php @@ -3,7 +3,6 @@ namespace Rappasoft\LaravelAuthenticationLog\Commands; use Illuminate\Console\Command; -use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog; class PurgeAuthenticationLogCommand extends Command { @@ -15,7 +14,8 @@ 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.'); } diff --git a/src/Listeners/LoginListener.php b/src/Listeners/LoginListener.php index 185f338..8ca73b1 100644 --- a/src/Listeners/LoginListener.php +++ b/src/Listeners/LoginListener.php @@ -2,6 +2,7 @@ namespace Rappasoft\LaravelAuthenticationLog\Listeners; +use App\Models\Tenant; use Illuminate\Auth\Events\Login; use Illuminate\Http\Request; use Illuminate\Support\Carbon; @@ -47,6 +48,7 @@ public function handle($event): void 'login_at' => now(), 'login_successful' => true, 'location' => config('authentication-log.notifications.new-device.location') ? optional(geoip()->getLocation($ip))->toArray() : null, + 'tenant_id'=> Tenant::first()->id ]); if (! $known && ! $newUser && config('authentication-log.notifications.new-device.enabled')) { diff --git a/src/Listeners/LogoutListener.php b/src/Listeners/LogoutListener.php index 305b560..8a6abdd 100644 --- a/src/Listeners/LogoutListener.php +++ b/src/Listeners/LogoutListener.php @@ -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 @@ -41,7 +40,8 @@ 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, ]); diff --git a/src/Listeners/OtherDeviceLogoutListener.php b/src/Listeners/OtherDeviceLogoutListener.php index 161f21b..29891ad 100644 --- a/src/Listeners/OtherDeviceLogoutListener.php +++ b/src/Listeners/OtherDeviceLogoutListener.php @@ -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 @@ -41,7 +40,8 @@ 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, ]); diff --git a/src/Models/AuthenticationLog.php b/src/Models/AuthenticationLog.php index d160369..35817b4 100755 --- a/src/Models/AuthenticationLog.php +++ b/src/Models/AuthenticationLog.php @@ -31,6 +31,7 @@ class AuthenticationLog extends Model 'logout_at', 'cleared_by_user', 'location', + 'tenant_id' ]; protected $casts = [ diff --git a/src/Notifications/FailedLogin.php b/src/Notifications/FailedLogin.php index dea80de..c827d73 100644 --- a/src/Notifications/FailedLogin.php +++ b/src/Notifications/FailedLogin.php @@ -8,17 +8,17 @@ use Illuminate\Notifications\Messages\NexmoMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; -use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog; class FailedLogin extends Notification implements ShouldQueue { use Queueable; - public AuthenticationLog $authenticationLog; + public $authenticationLog; - public function __construct(AuthenticationLog $authenticationLog) + public function __construct($authenticationLog) { - $this->authenticationLog = $authenticationLog; + $modelClass = config('authentication-log.model'); + $this->authenticationLog = new $modelClass($authenticationLog->getAttributes()); } public function via($notifiable) @@ -31,11 +31,11 @@ public function toMail($notifiable) return (new MailMessage()) ->subject(__('A failed login to your account')) ->markdown('authentication-log::emails.failed', [ - 'account' => $notifiable, - 'time' => $this->authenticationLog->login_at, + 'account' => $notifiable, + 'time' => $this->authenticationLog->login_at, 'ipAddress' => $this->authenticationLog->ip_address, - 'browser' => $this->authenticationLog->user_agent, - 'location' => $this->authenticationLog->location, + 'browser' => $this->authenticationLog->user_agent, + 'location' => $this->authenticationLog->location, ]); } @@ -47,11 +47,11 @@ public function toSlack($notifiable) ->content(__('There has been a failed login attempt to your :app account.', ['app' => config('app.name')])) ->attachment(function ($attachment) use ($notifiable) { $attachment->fields([ - __('Account') => $notifiable->email, - __('Time') => $this->authenticationLog->login_at->toCookieString(), + __('Account') => $notifiable->email, + __('Time') => $this->authenticationLog->login_at->toCookieString(), __('IP Address') => $this->authenticationLog->ip_address, - __('Browser') => $this->authenticationLog->user_agent, - __('Location') => + __('Browser') => $this->authenticationLog->user_agent, + __('Location') => $this->authenticationLog->location && $this->authenticationLog->location['default'] === false ? ($this->authenticationLog->location['city'] ?? 'N/A') . ', ' . ($this->authenticationLog->location['state'] ?? 'N/A') : diff --git a/src/Notifications/NewDevice.php b/src/Notifications/NewDevice.php index a525765..0dedab4 100644 --- a/src/Notifications/NewDevice.php +++ b/src/Notifications/NewDevice.php @@ -8,17 +8,17 @@ use Illuminate\Notifications\Messages\NexmoMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; -use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog; class NewDevice extends Notification implements ShouldQueue { use Queueable; - public AuthenticationLog $authenticationLog; + public $authenticationLog; - public function __construct(AuthenticationLog $authenticationLog) + public function __construct($authenticationLog) { - $this->authenticationLog = $authenticationLog; + $modelClass = config('authentication-log.model'); + $this->authenticationLog = new $modelClass($authenticationLog->getAttributes()); } public function via($notifiable) diff --git a/src/Traits/AuthenticationLoggable.php b/src/Traits/AuthenticationLoggable.php index a7cb320..644f354 100644 --- a/src/Traits/AuthenticationLoggable.php +++ b/src/Traits/AuthenticationLoggable.php @@ -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