From 45d6a938b19748e1eccf5f71c7d8039121c69cf4 Mon Sep 17 00:00:00 2001 From: julianmedwards <82068546+julianmedwards@users.noreply.github.com> Date: Tue, 22 Oct 2024 20:00:53 -0500 Subject: [PATCH] Add config option to bypass setting User on Events. --- config/livewire-tables.php | 10 ++++++++++ docs/datatable/events.md | 2 ++ docs/start/configuration.md | 19 +++++++++++++++++++ src/Events/LaravelLivewireTablesEvent.php | 2 +- tests/Events/ColumnsSelectedTest.php | 19 +++++++++++++++++++ tests/Events/FilterAppliedTest.php | 19 +++++++++++++++++++ tests/Events/SearchAppliedTest.php | 19 +++++++++++++++++++ 7 files changed, 89 insertions(+), 1 deletion(-) diff --git a/config/livewire-tables.php b/config/livewire-tables.php index 4cc3dccab..9a176a2fd 100644 --- a/config/livewire-tables.php +++ b/config/livewire-tables.php @@ -114,4 +114,14 @@ 'defaultConfig' => [], ], + /** + * Configuration options for Events + */ + 'events' => [ + /** + * Enable or disable passing the user from Laravel's Auth service to events + */ + 'enableUserForEvent' => true, + ], + ]; diff --git a/docs/datatable/events.md b/docs/datatable/events.md index 2a64763a7..dc81a5695 100644 --- a/docs/datatable/events.md +++ b/docs/datatable/events.md @@ -55,6 +55,8 @@ There are several events, all in the Rappasoft\LaravelLivewireTables\Events name | FilterApplied | Applied when a Filter is applied (not when removed) | The Table Name ($tableName), Filter Key ($key), Filter Value ($value), Logged In User ($user) | | SearchApplied | Applied when a Search is applied (not when removed) | The Table Name ($tableName), Search Term ($value), Logged In User ($user) | +Passing the user with an event is optional and [can be disabled in the config](../start/configuration.md#bypassing-laravels-auth-service). + By default, the Tables will dispatch an event when the Selected Columns is changed, you may customise this behaviour: #### enableAllEvents diff --git a/docs/start/configuration.md b/docs/start/configuration.md index 3e264c578..01725843f 100644 --- a/docs/start/configuration.md +++ b/docs/start/configuration.md @@ -81,3 +81,22 @@ You must also make sure you have this Alpine style available globally. Note that [x-cloak] { display: none !important; } ``` + +## Bypassing Laravel's Auth Service + +By default, all [events](../datatable/events#dispatched) will retrieve any currently authenticated user from Laravel's [Auth service](https://laravel.com/docs/authentication) and pass it along with the event. + +If your project doesn't include the Illuminate/Auth package, or you otherwise want to prevent this, you can set the `enableUserForEvent` config option to false. + +```php +// config/livewire-tables.php +return [ + // ... + 'events' => [ + /** + * Enable or disable passing the user from Laravel's Auth service to events + */ + 'enableUserForEvent' => false, + ], +]; +``` diff --git a/src/Events/LaravelLivewireTablesEvent.php b/src/Events/LaravelLivewireTablesEvent.php index 89026ead2..b1a034367 100644 --- a/src/Events/LaravelLivewireTablesEvent.php +++ b/src/Events/LaravelLivewireTablesEvent.php @@ -42,7 +42,7 @@ public function setTableForEvent(string $tableName): self public function setUserForEvent(): self { - if (auth()->user()) { + if (config('livewire-tables.events.enableUserForEvent', true) && auth()->user()) { $this->user = auth()->user(); } diff --git a/tests/Events/ColumnsSelectedTest.php b/tests/Events/ColumnsSelectedTest.php index 197b1450f..a6dd19580 100644 --- a/tests/Events/ColumnsSelectedTest.php +++ b/tests/Events/ColumnsSelectedTest.php @@ -80,4 +80,23 @@ public function test_an_event_is_emitted_when_a_column_selection_are_updated_and return $event->columns == [] && $event->user->id == '1234' && $event->tableName == $this->basicTable->getTableName(); }); } + + public function test_user_not_set_on_event_when_a_column_selection_is_updated_and_user_for_event_disabled() + { + Event::fake(); + + config()->set('livewire-tables.events.enableUserForEvent', false); + + $user = new \Illuminate\Foundation\Auth\User; + $user->id = '1234'; + $user->name = 'Bob'; + $this->actingAs($user); + + $this->basicTable->selectAllColumns(); + + Event::assertDispatched(ColumnsSelected::class, function ($event) { + $this->assertFalse(isset($event->user), "User set on Event when config is set to disable this behavior"); + return true; + }); + } } diff --git a/tests/Events/FilterAppliedTest.php b/tests/Events/FilterAppliedTest.php index ef59c5719..d48040b86 100644 --- a/tests/Events/FilterAppliedTest.php +++ b/tests/Events/FilterAppliedTest.php @@ -61,4 +61,23 @@ public function test_an_event_is_emitted_when_a_filter_is_applied_with_values_an return $event->value == 'test value' && $event->user->id == '1234' && $event->key = 'pet_name_filter' && $event->tableName == $this->basicTable->getTableName(); }); } + + public function test_user_not_set_on_event_when_a_filter_is_applied_and_user_for_event_disabled() + { + Event::fake(); + + config()->set('livewire-tables.events.enableUserForEvent', false); + + $user = new \Illuminate\Foundation\Auth\User; + $user->id = '1234'; + $user->name = 'Bob'; + $this->actingAs($user); + + $this->basicTable->enableFilterAppliedEvent()->setFilter('pet_name_filter', 'test value'); + + Event::assertDispatched(FilterApplied::class, function ($event) { + $this->assertFalse(isset($event->user), "User set on Event when config is set to disable this behavior"); + return true; + }); + } } diff --git a/tests/Events/SearchAppliedTest.php b/tests/Events/SearchAppliedTest.php index d7abc2bc7..d559697c6 100644 --- a/tests/Events/SearchAppliedTest.php +++ b/tests/Events/SearchAppliedTest.php @@ -71,4 +71,23 @@ public function test_an_event_is_emitted_when_a_search_is_applied_and_event_enab return $event->value == 'test search value' && $event->user->id == '1234' && $event->tableName == $this->basicTable->getTableName(); }); } + + public function test_user_not_set_on_event_when_a_search_is_applied_and_user_for_event_disabled() + { + Event::fake(); + + config()->set('livewire-tables.events.enableUserForEvent', false); + + $user = new \Illuminate\Foundation\Auth\User; + $user->id = '1234'; + $user->name = 'Bob'; + $this->actingAs($user); + + $this->basicTable->enableSearchAppliedEvent()->setSearch('test')->applySearch(); + + Event::assertDispatched(SearchApplied::class, function ($event) { + $this->assertFalse(isset($event->user), "User set on Event when config is set to disable this behavior"); + return true; + }); + } }