From ef21af1be31854e33f4b75151bea1af112832296 Mon Sep 17 00:00:00 2001 From: loganfox Date: Thu, 3 Oct 2024 11:38:07 -0500 Subject: [PATCH] Adds ability to disable notification send on specific instance --- src/Jobs/ProcessNewIPAddress.php | 3 +- src/Services/IPAddressDriver.php | 8 ++++-- tests/Unit/Jobs/ProcessNewIPAddressTest.php | 32 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Jobs/ProcessNewIPAddress.php b/src/Jobs/ProcessNewIPAddress.php index 023db87..99548ba 100644 --- a/src/Jobs/ProcessNewIPAddress.php +++ b/src/Jobs/ProcessNewIPAddress.php @@ -23,6 +23,7 @@ public function __construct( public readonly array $ipLocationData, public readonly int $userId, public readonly string $userType, + public readonly bool $sendNewIpNotification = true, ) { $this->user = $this->userType::find($this->userId); @@ -44,7 +45,7 @@ public function handle(): void 'location_data' => $this->ipLocationData, ]); - if (config('security-notifications.send_notifications')) { + if (config('security-notifications.send_notifications') && $this->sendNewIpNotification) { $notificationClass = config('security-notifications.notifications.secure_login'); $this->user->notify(new $notificationClass($login)); diff --git a/src/Services/IPAddressDriver.php b/src/Services/IPAddressDriver.php index ef9e0f0..e12ba4e 100644 --- a/src/Services/IPAddressDriver.php +++ b/src/Services/IPAddressDriver.php @@ -10,9 +10,10 @@ readonly class IPAddressDriver implements DigestIPAddress { public function __construct( - private readonly string $ipAddress, - private readonly int $userId, - private readonly string $userType, + private string $ipAddress, + private int $userId, + private string $userType, + private bool $sendNewIpNotification = true, ) { } @@ -51,6 +52,7 @@ public function handle(): void ipLocationData: $ipLocationData, userId: $this->userId, userType: $this->userType, + sendNewIpNotification: $this->sendNewIpNotification, ); } } diff --git a/tests/Unit/Jobs/ProcessNewIPAddressTest.php b/tests/Unit/Jobs/ProcessNewIPAddressTest.php index 6ff70b0..31e9955 100644 --- a/tests/Unit/Jobs/ProcessNewIPAddressTest.php +++ b/tests/Unit/Jobs/ProcessNewIPAddressTest.php @@ -41,4 +41,36 @@ Notification::assertSentTo($user, LoginFromNewIP::class, function ($notification) { return $notification->login->ip_address === '127.0.0.1'; }); +}); + +it('does not send notification if disabled', function () { + Notification::fake(); + + $user = User::factory()->create(); + + (new ProcessNewIPAddress( + ipLocationData: [ + 'query' => '127.0.0.1', + 'city' => 'Minneapolis', + 'region' => 'MN', + 'countryCode' => 'US', + ], + userId: $user->getKey(), + userType: $user->getMorphClass(), + sendNewIpNotification: false, + ))->handle(); + + assertDatabaseHas('logins', [ + 'ip_address' => '127.0.0.1', + 'user_id' => $user->getKey(), + 'user_type' => $user->getMorphClass(), + 'location_data' => json_encode([ + 'query' => '127.0.0.1', + 'city' => 'Minneapolis', + 'region' => 'MN', + 'countryCode' => 'US', + ]), + ]); + + Notification::assertNothingSent(); }); \ No newline at end of file