Skip to content

Commit

Permalink
hasRole check swaped for canAccessPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Aug 6, 2023
1 parent 9a6a9d4 commit fef0885
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function boot()
}
```

##### Visibility
By default, the package checks whether you have `Spatie permissions` plugin installed and checks for a role called `super_admin`. You can further customize whether the panel switch should be shown.
#### Visibility
By default, the package checks whether the user can access the panel if so the switch will be visible. You can further customize whether the panel switch should be shown.

```php
PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) {
Expand All @@ -61,7 +61,7 @@ PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) {
});
```

##### Who Can Switch Panels?
#### Who Can Switch Panels?
You might want an option in a situation where you want a group of your users to see the panel but not be able to switch panels. You can do that by using the `canSwitchPanels()` method.

```php
Expand All @@ -82,7 +82,7 @@ PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) {
});
```

##### Placement
#### Placement
You can choose where the panel switch menu should be placed. By default panel switch menu is rendered via 'panels::topbar.start' `Hook`. But you can change it to anyone of the other available hooks.
```php
PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) {
Expand Down
23 changes: 12 additions & 11 deletions src/PanelSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,31 @@ class PanelSwitch
public static function make(): static
{
$static = app(static::class);
$static->configure();

return $static;
}

public static function boot(): void
{
$static = static::make();

$static->visible(function () {
if (($user = auth()->user()) === null) {
return false;
}

if (method_exists($user, 'hasRole')) {
return $user->hasRole('super_admin');
if (method_exists($user, 'canAccessPanel')) {
return $user->canAccessPanel(filament()->getCurrentPanel() ?? filament()->getDefaultPanel());
}

return true;
});

$static->configure();

return $static;
}

public static function boot(): void
{
$static = static::make();

FilamentView::registerRenderHook(
name: $static->getRenderHook(),
hook: function () use ($static) {
hook: function () use($static){
if (! $static->isVisible()) {
return '';
}
Expand Down

0 comments on commit fef0885

Please sign in to comment.