Skip to content

Change in the structure to use Dynamic Model #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 config/authentication-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 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,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.');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Listeners/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
4 changes: 2 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,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,
]);
Expand Down
4 changes: 2 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,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,
]);
Expand Down
1 change: 1 addition & 0 deletions src/Models/AuthenticationLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AuthenticationLog extends Model
'logout_at',
'cleared_by_user',
'location',
'tenant_id'
];

protected $casts = [
Expand Down
24 changes: 12 additions & 12 deletions src/Notifications/FailedLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
]);
}

Expand All @@ -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') :
Expand Down
8 changes: 4 additions & 4 deletions src/Notifications/NewDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
Loading