Skip to content

Commit

Permalink
Optionally force not showing the user menu item (#74)
Browse files Browse the repository at this point in the history
* Force not showing the user menu item

* Fix styling

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Dec 25, 2024
1 parent 1de617e commit 3144add
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ If you want to force users to enable Two Factor Authentication, you can add this
])
```

### Prevent showing the Two Factor Authentication page in user menu

If you want to prevent showing the Two Factor Authentication page in the user menu, you can add this to your `PanelProvider`:

```php
->plugins([
TwoFactorAuthPlugin::make()->hideFromMenu(),
])->showInUserMenu(false)
```

> [!WARNING]
> When you're using the `forced` method, make sure to set the `multi_tenancy` option to `true` in the `filament-2fa.php` config file when you're using a multi-tenant setup. Otherwise, the forced setting will not work. We cannot check the tenant in the `PanelProvider` because the user is not authenticated yet.
Expand Down
16 changes: 15 additions & 1 deletion src/TwoFactorAuthPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TwoFactorAuthPlugin implements Plugin

private Closure | bool | null $forced = false;

private Closure | bool $showInUserMenu = true;

public function getId(): string
{
return 'filament-2fa';
Expand All @@ -37,7 +39,7 @@ public function register(Panel $panel): void
]);
}

if (! config('filament-2fa.enabled_features.multi_tenancy')) {
if (! config('filament-2fa.enabled_features.multi_tenancy') && $this->shouldShowInUserMenu()) {
$panel->userMenuItems([
'two-factor-authentication' => MenuItem::make()
->icon('heroicon-o-lock-closed')
Expand Down Expand Up @@ -80,4 +82,16 @@ public function isForced(): Closure | bool | null
{
return $this->evaluate($this->forced);
}

public function showInUserMenu(Closure | bool $showInUserMenu = true): self
{
$this->showInUserMenu = $showInUserMenu;

return $this;
}

public function shouldShowInUserMenu(): bool
{
return $this->evaluate($this->showInUserMenu);
}
}

0 comments on commit 3144add

Please sign in to comment.