From f453eb566385154fcbd928dba9034a96b605c65b Mon Sep 17 00:00:00 2001 From: Curtis Delicata Date: Fri, 28 Jun 2024 18:04:40 +0000 Subject: [PATCH] Configure Jetstream classes in providers and separate panels into Admin and App --- app/Providers/Filament/AdminPanelProvider.php | 104 +++++++++++++++++- app/Providers/Filament/AppPanelProvider.php | 61 +++++----- 2 files changed, 130 insertions(+), 35 deletions(-) diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index 9cfc65b9..ae5660e0 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -2,10 +2,20 @@ namespace App\Providers\Filament; +use App\Filament\Admin\Pages; +use App\Filament\Admin\Pages\EditProfile; +use App\Http\Middleware\TeamsPermission; +use App\Listeners\CreatePersonalTeam; +use App\Listeners\SwitchTeam; +use App\Models\Team; +use Filament\Events\Auth\Registered; +use Filament\Events\TenantSet; +use Filament\Facades\Filament; use Filament\Http\Middleware\Authenticate; use Filament\Http\Middleware\DisableBladeIconComponents; use Filament\Http\Middleware\DispatchServingFilamentEvent; -use Filament\Pages; +use Filament\Navigation\MenuItem; +use Filament\Pages as FilamentPage; use Filament\Panel; use Filament\PanelProvider; use Filament\Support\Colors\Color; @@ -16,24 +26,45 @@ use Illuminate\Routing\Middleware\SubstituteBindings; use Illuminate\Session\Middleware\AuthenticateSession; use Illuminate\Session\Middleware\StartSession; +use Illuminate\Support\Facades\Event; use Illuminate\View\Middleware\ShareErrorsFromSession; +use Laravel\Fortify\Fortify; +use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController; +use Laravel\Fortify\Http\Controllers\RegisteredUserController; +use Laravel\Jetstream\Features; +use Laravel\Jetstream\Jetstream; class AdminPanelProvider extends PanelProvider { public function panel(Panel $panel): Panel { - return $panel + $panel + ->default() ->id('admin') ->path('admin') + ->login([AuthenticatedSessionController::class, 'create']) + ->registration([RegisteredUserController::class, 'create']) + ->passwordReset() + ->emailVerification() + ->viteTheme('resources/css/Filament/Admin/theme.css') ->colors([ - 'primary' => Color::Amber, + 'primary' => Color::Gray, + ]) + ->userMenuItems([ + MenuItem::make() + ->label('Profile') + ->icon('heroicon-o-user-circle') + ->url(fn () => $this->shouldRegisterMenuItem() + ? url(EditProfile::getUrl()) + : url($panel->getPath())), ]) ->discoverResources(in: app_path('Filament/Admin/Resources'), for: 'App\\Filament\\Admin\\Resources') ->discoverPages(in: app_path('Filament/Admin/Pages'), for: 'App\\Filament\\Admin\\Pages') ->pages([ - Pages\Dashboard::class, + FilamentPage\Dashboard::class, + Pages\EditProfile::class, ]) - ->discoverWidgets(in: app_path('Filament/Admin/Widgets'), for: 'App\\Filament\\Admin\\Widgets') + ->discoverWidgets(in: app_path('Filament/Admin/Widgets/Home'), for: 'App\\Filament\\Admin\\Widgets\\Home') ->widgets([ Widgets\AccountWidget::class, Widgets\FilamentInfoWidget::class, @@ -51,9 +82,72 @@ public function panel(Panel $panel): Panel ]) ->authMiddleware([ Authenticate::class, + TeamsPermission::class, ]) ->plugins([ \BezhanSalleh\FilamentShield\FilamentShieldPlugin::make() ]); + + if (Features::hasApiFeatures()) { + $panel->userMenuItems([ + MenuItem::make() + ->label('API Tokens') + ->icon('heroicon-o-key') + ->url(fn () => $this->shouldRegisterMenuItem() + ? url(Pages\ApiTokenManagerPage::getUrl()) + : url($panel->getPath())), + ]); + } + + if (Features::hasTeamFeatures()) { + $panel + ->tenant(Team::class, ownershipRelationship: 'team') + ->tenantRegistration(Pages\CreateTeam::class) + ->tenantProfile(Pages\EditTeam::class) + ->userMenuItems([ + MenuItem::make() + ->label('Team Settings') + ->icon('heroicon-o-cog-6-tooth') + ->url(fn () => $this->shouldRegisterMenuItem() + ? url(Pages\EditTeam::getUrl()) + : url($panel->getPath())), + ]); + } + + return $panel; + } + + public function boot() + { + /** + * Disable Fortify routes. + */ + Fortify::$registersRoutes = false; + + /** + * Disable Jetstream routes. + */ + Jetstream::$registersRoutes = false; + + /** + * Listen and create personal team for new accounts. + */ + Event::listen( + Registered::class, + CreatePersonalTeam::class, + ); + + /** + * Listen and switch team if tenant was changed. + */ + Event::listen( + TenantSet::class, + SwitchTeam::class, + ); + } + + public function shouldRegisterMenuItem(): bool + { + return true; //auth()->user()?->hasVerifiedEmail() && Filament::hasTenancy() && Filament::getTenant(); } } diff --git a/app/Providers/Filament/AppPanelProvider.php b/app/Providers/Filament/AppPanelProvider.php index 455f9406..f59fbda4 100644 --- a/app/Providers/Filament/AppPanelProvider.php +++ b/app/Providers/Filament/AppPanelProvider.php @@ -2,10 +2,9 @@ namespace App\Providers\Filament; -use App\Filament\Pages\ApiTokens; -use App\Filament\Pages\CreateTeam; -use App\Filament\Pages\EditProfile; -use App\Filament\Pages\EditTeam; +use App\Filament\App\Pages; +use App\Filament\App\Pages\EditProfile; +use App\Http\Middleware\TeamsPermission; use App\Listeners\CreatePersonalTeam; use App\Listeners\SwitchTeam; use App\Models\Team; @@ -16,7 +15,7 @@ use Filament\Http\Middleware\DisableBladeIconComponents; use Filament\Http\Middleware\DispatchServingFilamentEvent; use Filament\Navigation\MenuItem; -use Filament\Pages; +use Filament\Pages as FilamentPage; use Filament\Panel; use Filament\PanelProvider; use Filament\Support\Colors\Color; @@ -30,10 +29,12 @@ use Illuminate\Support\Facades\Event; use Illuminate\View\Middleware\ShareErrorsFromSession; use Laravel\Fortify\Fortify; +use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController; +use Laravel\Fortify\Http\Controllers\RegisteredUserController; use Laravel\Jetstream\Features; use Laravel\Jetstream\Jetstream; -class AppPanelProvider extends PanelProvider +class AdminPanelProvider extends PanelProvider { public function panel(Panel $panel): Panel { @@ -41,11 +42,11 @@ public function panel(Panel $panel): Panel ->default() ->id('app') ->path('app') - ->login() - ->registration() + ->login([AuthenticatedSessionController::class, 'create']) + ->registration([RegisteredUserController::class, 'create']) ->passwordReset() ->emailVerification() - ->viteTheme('resources/css/app.css') + ->viteTheme('resources/css/Filament/App/admin/theme.css') ->colors([ 'primary' => Color::Gray, ]) @@ -57,14 +58,13 @@ public function panel(Panel $panel): Panel ? url(EditProfile::getUrl()) : url($panel->getPath())), ]) - ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources') - ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages') + ->discoverResources(in: app_path('Filament/App/Resources'), for: 'App\\Filament\\App\\Resources') + ->discoverPages(in: app_path('Filament/App/Pages'), for: 'App\\Filament\\App\\Pages') ->pages([ - Pages\Dashboard::class, - EditProfile::class, - ApiTokens::class, + FilamentPage\Dashboard::class, + Pages\EditProfile::class, ]) - ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') + ->discoverWidgets(in: app_path('Filament/App/Widgets/Home'), for: 'App\\Filament\\App\\Widgets\\Home') ->widgets([ Widgets\AccountWidget::class, Widgets\FilamentInfoWidget::class, @@ -82,7 +82,11 @@ public function panel(Panel $panel): Panel ]) ->authMiddleware([ Authenticate::class, - ]); + TeamsPermission::class, + ]) + ->plugins([ + \BezhanSalleh\FilamentShield\FilamentShieldPlugin::make() + ]); if (Features::hasApiFeatures()) { $panel->userMenuItems([ @@ -90,27 +94,24 @@ public function panel(Panel $panel): Panel ->label('API Tokens') ->icon('heroicon-o-key') ->url(fn () => $this->shouldRegisterMenuItem() - ? url(ApiTokens::getUrl()) + ? url(Pages\ApiTokenManagerPage::getUrl()) : url($panel->getPath())), ]); } if (Features::hasTeamFeatures()) { $panel - ->tenant(Team::class) - ->tenantRegistration(CreateTeam::class) - ->tenantProfile(EditTeam::class) + ->tenant(Team::class, ownershipRelationship: 'team') + ->tenantRegistration(Pages\CreateTeam::class) + ->tenantProfile(Pages\EditTeam::class) ->userMenuItems([ MenuItem::make() ->label('Team Settings') ->icon('heroicon-o-cog-6-tooth') ->url(fn () => $this->shouldRegisterMenuItem() - ? url(EditTeam::getUrl()) + ? url(Pages\EditTeam::getUrl()) : url($panel->getPath())), - ]) - ->plugins([ - \BezhanSalleh\FilamentShield\FilamentShieldPlugin::make() - ]); + ]); } return $panel; @@ -119,17 +120,17 @@ public function panel(Panel $panel): Panel public function boot() { /** - * Disable Fortify routes + * Disable Fortify routes. */ Fortify::$registersRoutes = false; /** - * Disable Jetstream routes + * Disable Jetstream routes. */ Jetstream::$registersRoutes = false; /** - * Listen and create personal team for new accounts + * Listen and create personal team for new accounts. */ Event::listen( Registered::class, @@ -137,7 +138,7 @@ public function boot() ); /** - * Listen and switch team if tenant was changed + * Listen and switch team if tenant was changed. */ Event::listen( TenantSet::class, @@ -147,6 +148,6 @@ public function boot() public function shouldRegisterMenuItem(): bool { - return auth()->user()?->hasVerifiedEmail() && Filament::hasTenancy() && Filament::getTenant(); + return true; //auth()->user()?->hasVerifiedEmail() && Filament::hasTenancy() && Filament::getTenant(); } }