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 forced() method and middleware #28

Merged
merged 15 commits into from
Sep 15, 2024
3 changes: 2 additions & 1 deletion resources/views/two-factor.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<x-filament-panels::page>

<div class="space-y-10 divide-y divide-gray-900/10 ">
<div class="grid grid-cols-1 gap-x-8 gap-y-8 pt-10 md:grid-cols-3">
<div class="pr-4 sm:px-0">
<h2 class="text-base font-semibold leading-7 dark:bg-white">
{{ __('Secure your account') }}
{{ __('Secure your account') }} sdfsadfsa
</h2>

@if (!$showingRecoveryCodes && $user->two_factor_confirmed_at)
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Middleware/ForceTwoFactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Vormkracht10\TwoFactorAuth\Http\Middleware;

use Closure;
use Filament\Facades\Filament;
use Illuminate\Http\Request;

class ForceTwoFactor
{
public function handle(Request $request, Closure $next): mixed
{
$user = Filament::auth()->user();

if ($request->is('*/two-factor') || $request->is('*/logout')) {
return $next($request);
}

if ($user && ! $user->two_factor_confirmed_at) {
$currentPanel = Filament::getCurrentPanel();

if ($currentPanel) {
return redirect()->to(route('filament.' . $currentPanel->getId() . '.pages.two-factor', [
'tenant' => Filament::getTenant(),
]))->with('two_factor_redirect_message', __('Your administrator requires you to enable two-factor authentication.'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this customizable?
Example:

->forced(message: __('Some message')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes maybe it's better to let the developer choose which message to show to the user. I'm also not really satisfied with showing the message yet. Weird enough I couldn't show the message in the blade view itself using the session helper..

}
}

return $next($request);
}
}
10 changes: 9 additions & 1 deletion src/Pages/TwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public function getTitle(): string | Htmlable

public function mount(): void
{
if (session('two_factor_redirect_message')) {
Notification::make()
->title(session('two_factor_redirect_message'))
->danger()
->persistent()
->send();
}

$this->twoFactorOptionsCount = config('filament-two-factor-auth.options') ? count(config('filament-two-factor-auth.options')) : 0;

$this->user = Auth::user();
Expand Down Expand Up @@ -272,4 +280,4 @@ public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generate): voi

$this->showingRecoveryCodes = true;
}
}
}
40 changes: 38 additions & 2 deletions src/TwoFactorAuthPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Vormkracht10\TwoFactorAuth;

use Filament\Contracts\Plugin;
use Filament\Facades\Filament;
use Filament\Navigation\MenuItem;
use Filament\Panel;
use Vormkracht10\TwoFactorAuth\Http\Middleware\ForceTwoFactor;
use Vormkracht10\TwoFactorAuth\Pages\TwoFactor;

class TwoFactorAuthPlugin implements Plugin
{
private bool $forced = false;
private bool $hasTenancy = false;

public function getId(): string
{
return 'filament-two-factor-auth';
Expand All @@ -24,12 +29,19 @@ public function register(Panel $panel): void
])
->viteTheme('vendor/vormkracht10/filament-2fa/resources/dist/filament-two-factor-auth.css');

if ($this->isForced()) {
$middlewareMethod = $this->hasTenancy() ? 'tenantMiddleware' : 'middleware';
$panel->$middlewareMethod([
ForceTwoFactor::class,
]);
}

if (! config('filament-two-factor-auth.enabled_features.multi_tenancy')) {
$panel->userMenuItems([
'two-factor-authentication' => MenuItem::make()
->icon('heroicon-o-lock-closed')
->label(__('Two-Factor Authentication'))
->url(fn (): string => TwoFactor::getUrl()),
->url(fn(): string => TwoFactor::getUrl()),
]);
}

Expand All @@ -55,4 +67,28 @@ public static function get(): static

return $plugin;
}
}

public function forced(bool $forced = true): self
{
$this->forced = $forced;

return $this;
}

public function isForced(): bool
{
return $this->forced;
}

public function withTenancy(bool $hasTenancy = true): self
{
$this->hasTenancy = $hasTenancy;

return $this;
}

public function hasTenancy(): bool
{
return $this->hasTenancy;
}
}