mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-04-02 20:06:16 +02:00
Add a listener to automatically log notification sends
This commit is contained in:
parent
1e42008be7
commit
04078b09aa
35
app/Listeners/LogNotification.php
Normal file
35
app/Listeners/LogNotification.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Notifications\Events\NotificationSent;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class LogNotification
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the event listener.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(NotificationSent $event)
|
||||||
|
{
|
||||||
|
// $event->channel
|
||||||
|
// $event->notifiable
|
||||||
|
// $event->notification
|
||||||
|
// $event->response
|
||||||
|
|
||||||
|
Log::info(sprintf('Notification of type %s sent via channel %s to user ID #%s', get_class($event->notification), $event->channel, $event->notifiable->id));
|
||||||
|
}
|
||||||
|
}
|
@ -131,8 +131,6 @@ public function resetPassword()
|
|||||||
public function sendPasswordResetNotification($token)
|
public function sendPasswordResetNotification($token)
|
||||||
{
|
{
|
||||||
$this->notify(new ResetPassword($token));
|
$this->notify(new ResetPassword($token));
|
||||||
|
|
||||||
Log::info(sprintf('Password reset token sent to user id "%s', $this->id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
use App\Events\TwoFAccountDeleted;
|
use App\Events\TwoFAccountDeleted;
|
||||||
use App\Listeners\CleanIconStorage;
|
use App\Listeners\CleanIconStorage;
|
||||||
use App\Listeners\DissociateTwofaccountFromGroup;
|
use App\Listeners\DissociateTwofaccountFromGroup;
|
||||||
|
use App\Listeners\LogNotification;
|
||||||
use App\Listeners\RegisterOpenId;
|
use App\Listeners\RegisterOpenId;
|
||||||
use App\Listeners\ReleaseRadar;
|
use App\Listeners\ReleaseRadar;
|
||||||
use App\Listeners\ResetUsersPreference;
|
use App\Listeners\ResetUsersPreference;
|
||||||
@ -16,6 +17,7 @@
|
|||||||
use Illuminate\Auth\Events\Registered;
|
use Illuminate\Auth\Events\Registered;
|
||||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||||
|
use Illuminate\Notifications\Events\NotificationSent;
|
||||||
use SocialiteProviders\Manager\SocialiteWasCalled;
|
use SocialiteProviders\Manager\SocialiteWasCalled;
|
||||||
|
|
||||||
class EventServiceProvider extends ServiceProvider
|
class EventServiceProvider extends ServiceProvider
|
||||||
@ -44,6 +46,9 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
SocialiteWasCalled::class => [
|
SocialiteWasCalled::class => [
|
||||||
RegisterOpenId::class,
|
RegisterOpenId::class,
|
||||||
],
|
],
|
||||||
|
NotificationSent::class => [
|
||||||
|
LogNotification::class,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
29
tests/Unit/Listeners/LogNotificationTest.php
Normal file
29
tests/Unit/Listeners/LogNotificationTest.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Listeners;
|
||||||
|
|
||||||
|
use App\Listeners\LogNotification;
|
||||||
|
use Illuminate\Notifications\Events\NotificationSent;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ResetUsersPreferenceTest test class
|
||||||
|
*/
|
||||||
|
#[CoversClass(LogNotification::class)]
|
||||||
|
class LogNotificationTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function test_LogNotificationTest_listen_to_NotificationSent_event()
|
||||||
|
{
|
||||||
|
Event::fake();
|
||||||
|
|
||||||
|
Event::assertListening(
|
||||||
|
NotificationSent::class,
|
||||||
|
LogNotification::class
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user