Skip to content

Commit

Permalink
Adds ability to disable notification send on specific instance
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganTFox committed Oct 3, 2024
1 parent 81b1253 commit ef21af1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Jobs/ProcessNewIPAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand Down
8 changes: 5 additions & 3 deletions src/Services/IPAddressDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
{
}
Expand Down Expand Up @@ -51,6 +52,7 @@ public function handle(): void
ipLocationData: $ipLocationData,
userId: $this->userId,
userType: $this->userType,
sendNewIpNotification: $this->sendNewIpNotification,
);
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Jobs/ProcessNewIPAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit ef21af1

Please sign in to comment.