Skip to content

Commit

Permalink
Merge pull request #198 from jeffgreco13/2.x-dev
Browse files Browse the repository at this point in the history
Multi-tenancy support
  • Loading branch information
jeffgreco13 authored Aug 9, 2023
2 parents 749a7e2 + 45efbeb commit 49bf937
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
5 changes: 3 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
foreach (Filament::getPanels() as $panel) {
$panelId = $panel->getId();
$domains = $panel->getDomains();
$hasTenancy = $panel->hasTenancy();
foreach ((empty($domains) ? [null] : $domains) as $domain) {
Route::domain($domain)
->middleware($panel->getMiddleware())
->name("{$panelId}.")
->prefix($panel->getPath())
->group(function () use ($panel) {
Route::get('/two-factor-authentication', TwoFactorPage::class)->name('auth.two-factor');
->group(function () use ($panel, $hasTenancy) {
Route::get($hasTenancy ? '/{tenant}/two-factor-authentication' : '/two-factor-authentication', TwoFactorPage::class)->name('auth.two-factor');
});
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/BreezyCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ public function boot(Panel $panel): void
}

if ($this->myProfile['shouldRegisterUserMenu']) {
$panel->userMenuItems([
'account' => MenuItem::make()->url(Pages\MyProfilePage::getUrl()),
]);
if ($panel->hasTenancy()) {
$tenantId = request()->route()->parameter('tenant');
if ($tenant = app($panel->getTenantModel())::where($panel->getTenantSlugAttribute() ?? 'id', $tenantId)->first()){
$panel->userMenuItems([
'account' => MenuItem::make()->url(Pages\MyProfilePage::getUrl(panel:$panel->getId(),tenant: $tenant)),
]);
}
} else {
$panel->userMenuItems([
'account' => MenuItem::make()->url(Pages\MyProfilePage::getUrl()),
]);
}
}
}
}
Expand Down
29 changes: 19 additions & 10 deletions src/Middleware/MustTwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Closure;
use Illuminate\Http\Request;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Hash;
use Jeffgreco13\FilamentBreezy\Facades\FilamentBreezy;
use Symfony\Component\HttpFoundation\Response;

class MustTwoFactor
Expand All @@ -18,18 +16,29 @@ class MustTwoFactor
*/
public function handle(Request $request, Closure $next): Response
{
$breezy = filament('filament-breezy');
if($breezy->auth()->check() && !$request->routeIs('filament.admin.auth.logout')){
// Logged in.
$myProfileRouteName = 'filament.' . filament('filament-breezy')->getCurrentPanel()->getId() . '.pages.my-profile';
if (
filament()->auth()->check() &&
!str($request->route()->getName())->contains('logout')
){
$breezy = filament('filament-breezy');
$myProfileRouteName = 'filament.' . filament()->getCurrentPanel()->getPath() . '.pages.my-profile';

if (filament()->hasTenancy()){
if (!$tenantId = request()->route()->parameter('tenant')){
return $next($request);
}
$twoFactorRoute = route('filament.' . filament()->getCurrentPanel()->getId() . '.auth.two-factor',['tenant'=>$tenantId]);
} else {
$twoFactorRoute = route('filament.' . filament()->getCurrentPanel()->getId() . '.auth.two-factor');
}

if ($breezy->shouldForceTwoFactor() && !$request->routeIs($myProfileRouteName)){
return redirect()->route($myProfileRouteName);
} else if ($breezy->auth()->user()->hasConfirmedTwoFactor() && !$breezy->auth()->user()->hasValidTwoFactorSession()){
return redirect()->route('filament.' . $breezy->getCurrentPanel()->getId() . '.auth.two-factor');
} else if (filament()->auth()->user()->hasConfirmedTwoFactor() && !filament()->auth()->user()->hasValidTwoFactorSession()) {
return redirect($twoFactorRoute);
}


}
// Fall through
return $next($request);
}
}

0 comments on commit 49bf937

Please sign in to comment.