From 3144addb85e1d34cfd71b6015b28bf45984a947b Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 25 Dec 2024 09:39:42 +0100 Subject: [PATCH] Optionally force not showing the user menu item (#74) * Force not showing the user menu item * Fix styling --------- Co-authored-by: Baspa --- README.md | 10 ++++++++++ src/TwoFactorAuthPlugin.php | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 208ad9d..99de3a3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/TwoFactorAuthPlugin.php b/src/TwoFactorAuthPlugin.php index 8db2d15..247faa4 100644 --- a/src/TwoFactorAuthPlugin.php +++ b/src/TwoFactorAuthPlugin.php @@ -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'; @@ -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') @@ -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); + } }