Skip to content
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

Add config option to bypass setting User on Events. #2006

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
10 changes: 10 additions & 0 deletions config/livewire-tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,14 @@
'defaultConfig' => [],
],

/**
* Configuration options for Events
*/
'events' => [

Check warning on line 120 in config/livewire-tables.php

View check run for this annotation

Codecov / codecov/patch

config/livewire-tables.php#L120

Added line #L120 was not covered by tests
/**
* Enable or disable passing the user from Laravel's Auth service to events
*/
'enableUserForEvent' => true,
],

Check warning on line 125 in config/livewire-tables.php

View check run for this annotation

Codecov / codecov/patch

config/livewire-tables.php#L124-L125

Added lines #L124 - L125 were not covered by tests

];
2 changes: 2 additions & 0 deletions docs/datatable/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions docs/start/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,22 @@ You must also make sure you have this Alpine style available globally. Note that
[x-cloak] { display: none !important; }
</style>
```

## 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,
],
];
```
2 changes: 1 addition & 1 deletion src/Events/LaravelLivewireTablesEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Events/ColumnsSelectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
19 changes: 19 additions & 0 deletions tests/Events/FilterAppliedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
19 changes: 19 additions & 0 deletions tests/Events/SearchAppliedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
Loading