feat: Add frontend menu management system with permission filtering
This commit is contained in:
146
src/app/Filament/Resources/MenuResource.php
Normal file
146
src/app/Filament/Resources/MenuResource.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\Filament\Resources\MenuResource\Pages;
|
||||
use App\Filament\Resources\MenuResource\RelationManagers;
|
||||
use App\Models\Menu;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class MenuResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Menu::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-bars-3';
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?int $navigationSort = 10;
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.view');
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.create');
|
||||
}
|
||||
|
||||
public static function canEdit($record): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.edit');
|
||||
}
|
||||
|
||||
public static function canDelete($record): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.delete');
|
||||
}
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Section::make('Menu Details')
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('name')
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->live(onBlur: true)
|
||||
->afterStateUpdated(fn ($state, Forms\Set $set) =>
|
||||
$set('slug', Str::slug($state))
|
||||
),
|
||||
Forms\Components\TextInput::make('slug')
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->unique(ignoreRecord: true),
|
||||
Forms\Components\Select::make('location')
|
||||
->options([
|
||||
'header' => 'Header Navigation',
|
||||
'footer' => 'Footer Navigation',
|
||||
'sidebar' => 'Sidebar Navigation',
|
||||
])
|
||||
->placeholder('Select a location'),
|
||||
Forms\Components\Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true),
|
||||
])->columns(2),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('slug')
|
||||
->searchable()
|
||||
->badge()
|
||||
->color('gray'),
|
||||
Tables\Columns\TextColumn::make('location')
|
||||
->badge()
|
||||
->color(fn (?string $state) => match ($state) {
|
||||
'header' => 'success',
|
||||
'footer' => 'warning',
|
||||
'sidebar' => 'info',
|
||||
default => 'gray',
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('allItems_count')
|
||||
->label('Items')
|
||||
->counts('allItems')
|
||||
->badge(),
|
||||
Tables\Columns\IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean(),
|
||||
Tables\Columns\TextColumn::make('updated_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('location')
|
||||
->options([
|
||||
'header' => 'Header',
|
||||
'footer' => 'Footer',
|
||||
'sidebar' => 'Sidebar',
|
||||
]),
|
||||
Tables\Filters\TernaryFilter::make('is_active')
|
||||
->label('Active'),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
RelationManagers\ItemsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListMenus::route('/'),
|
||||
'create' => Pages\CreateMenu::route('/create'),
|
||||
'edit' => Pages\EditMenu::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
11
src/app/Filament/Resources/MenuResource/Pages/CreateMenu.php
Normal file
11
src/app/Filament/Resources/MenuResource/Pages/CreateMenu.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\Pages;
|
||||
|
||||
use App\Filament\Resources\MenuResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateMenu extends CreateRecord
|
||||
{
|
||||
protected static string $resource = MenuResource::class;
|
||||
}
|
||||
19
src/app/Filament/Resources/MenuResource/Pages/EditMenu.php
Normal file
19
src/app/Filament/Resources/MenuResource/Pages/EditMenu.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\Pages;
|
||||
|
||||
use App\Filament\Resources\MenuResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditMenu extends EditRecord
|
||||
{
|
||||
protected static string $resource = MenuResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
src/app/Filament/Resources/MenuResource/Pages/ListMenus.php
Normal file
19
src/app/Filament/Resources/MenuResource/Pages/ListMenus.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\Pages;
|
||||
|
||||
use App\Filament\Resources\MenuResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListMenus extends ListRecords
|
||||
{
|
||||
protected static string $resource = MenuResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\RelationManagers;
|
||||
|
||||
use App\Models\MenuItem;
|
||||
use App\Services\MenuService;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class ItemsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'allItems';
|
||||
|
||||
protected static ?string $title = 'Menu Items';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
$menuService = app(MenuService::class);
|
||||
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Section::make('Item Details')
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('title')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Forms\Components\Select::make('type')
|
||||
->options([
|
||||
'link' => 'External Link',
|
||||
'route' => 'Named Route',
|
||||
'module' => 'Module',
|
||||
])
|
||||
->default('link')
|
||||
->required()
|
||||
->live(),
|
||||
Forms\Components\TextInput::make('url')
|
||||
->label('URL')
|
||||
->url()
|
||||
->visible(fn (Forms\Get $get) => $get('type') === 'link')
|
||||
->required(fn (Forms\Get $get) => $get('type') === 'link'),
|
||||
Forms\Components\Select::make('route')
|
||||
->label('Route Name')
|
||||
->options(fn () => $menuService->getAvailableRoutes())
|
||||
->searchable()
|
||||
->visible(fn (Forms\Get $get) => $get('type') === 'route')
|
||||
->required(fn (Forms\Get $get) => $get('type') === 'route'),
|
||||
Forms\Components\Select::make('module')
|
||||
->label('Module')
|
||||
->options(fn () => $menuService->getAvailableModules())
|
||||
->searchable()
|
||||
->visible(fn (Forms\Get $get) => $get('type') === 'module')
|
||||
->required(fn (Forms\Get $get) => $get('type') === 'module')
|
||||
->helperText('Users need view permission for this module to see this item'),
|
||||
])->columns(2),
|
||||
|
||||
Forms\Components\Section::make('Permissions & Display')
|
||||
->schema([
|
||||
Forms\Components\Select::make('parent_id')
|
||||
->label('Parent Item')
|
||||
->options(fn () => MenuItem::where('menu_id', $this->ownerRecord->id)
|
||||
->whereNull('parent_id')
|
||||
->pluck('title', 'id'))
|
||||
->placeholder('None (Top Level)')
|
||||
->searchable(),
|
||||
Forms\Components\Select::make('permission')
|
||||
->label('Required Permission')
|
||||
->options(fn () => Permission::pluck('name', 'name'))
|
||||
->searchable()
|
||||
->placeholder('No specific permission required')
|
||||
->helperText('If set, user must have this permission to see this item'),
|
||||
Forms\Components\TextInput::make('icon')
|
||||
->placeholder('heroicon-o-home')
|
||||
->helperText('Heroicon name or custom icon class'),
|
||||
Forms\Components\Select::make('target')
|
||||
->options([
|
||||
'_self' => 'Same Window',
|
||||
'_blank' => 'New Tab',
|
||||
])
|
||||
->default('_self'),
|
||||
Forms\Components\TextInput::make('order')
|
||||
->numeric()
|
||||
->default(0)
|
||||
->helperText('Lower numbers appear first'),
|
||||
Forms\Components\Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true),
|
||||
])->columns(2),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('title')
|
||||
->reorderable('order')
|
||||
->defaultSort('order')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('title')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('parent.title')
|
||||
->label('Parent')
|
||||
->placeholder('—')
|
||||
->badge()
|
||||
->color('gray'),
|
||||
Tables\Columns\TextColumn::make('type')
|
||||
->badge()
|
||||
->color(fn (string $state) => match ($state) {
|
||||
'link' => 'info',
|
||||
'route' => 'success',
|
||||
'module' => 'warning',
|
||||
default => 'gray',
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('module')
|
||||
->placeholder('—')
|
||||
->toggleable(),
|
||||
Tables\Columns\TextColumn::make('permission')
|
||||
->placeholder('—')
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('order')
|
||||
->sortable(),
|
||||
Tables\Columns\IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('type')
|
||||
->options([
|
||||
'link' => 'External Link',
|
||||
'route' => 'Named Route',
|
||||
'module' => 'Module',
|
||||
]),
|
||||
Tables\Filters\TernaryFilter::make('is_active')
|
||||
->label('Active'),
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make(),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
40
src/app/Models/Menu.php
Normal file
40
src/app/Models/Menu.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Menu extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'location',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(MenuItem::class)->whereNull('parent_id')->orderBy('order');
|
||||
}
|
||||
|
||||
public function allItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(MenuItem::class)->orderBy('order');
|
||||
}
|
||||
|
||||
public static function findBySlug(string $slug): ?self
|
||||
{
|
||||
return static::where('slug', $slug)->where('is_active', true)->first();
|
||||
}
|
||||
|
||||
public static function findByLocation(string $location): ?self
|
||||
{
|
||||
return static::where('location', $location)->where('is_active', true)->first();
|
||||
}
|
||||
}
|
||||
90
src/app/Models/MenuItem.php
Normal file
90
src/app/Models/MenuItem.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class MenuItem extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'menu_id',
|
||||
'parent_id',
|
||||
'title',
|
||||
'type',
|
||||
'url',
|
||||
'route',
|
||||
'route_params',
|
||||
'module',
|
||||
'permission',
|
||||
'icon',
|
||||
'target',
|
||||
'order',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'route_params' => 'array',
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function menu(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Menu::class);
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MenuItem::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(MenuItem::class, 'parent_id')->orderBy('order');
|
||||
}
|
||||
|
||||
public function getUrlAttribute(): ?string
|
||||
{
|
||||
return match ($this->type) {
|
||||
'link' => $this->attributes['url'],
|
||||
'route' => $this->route ? route($this->route, $this->route_params ?? []) : null,
|
||||
'module' => $this->module ? route("{$this->module}.index") : null,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public function isVisibleToUser(?User $user = null): bool
|
||||
{
|
||||
if (!$this->is_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$user = $user ?? Auth::user();
|
||||
|
||||
if (!$user) {
|
||||
return !$this->permission && !$this->module;
|
||||
}
|
||||
|
||||
if ($user->hasRole('admin')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->permission) {
|
||||
return $user->can($this->permission);
|
||||
}
|
||||
|
||||
if ($this->module) {
|
||||
return $user->can("{$this->module}.view");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getVisibleChildren(?User $user = null): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->children->filter(fn (MenuItem $item) => $item->isVisibleToUser($user));
|
||||
}
|
||||
}
|
||||
117
src/app/Services/MenuService.php
Normal file
117
src/app/Services/MenuService.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Menu;
|
||||
use App\Models\MenuItem;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class MenuService
|
||||
{
|
||||
public function getMenu(string $slugOrLocation, ?User $user = null): ?array
|
||||
{
|
||||
$menu = Menu::findBySlug($slugOrLocation) ?? Menu::findByLocation($slugOrLocation);
|
||||
|
||||
if (!$menu) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->buildMenuTree($menu, $user);
|
||||
}
|
||||
|
||||
public function buildMenuTree(Menu $menu, ?User $user = null): array
|
||||
{
|
||||
$user = $user ?? Auth::user();
|
||||
|
||||
$items = $menu->items()
|
||||
->with(['children' => fn ($q) => $q->orderBy('order')])
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
|
||||
return $this->filterAndMapItems($items, $user);
|
||||
}
|
||||
|
||||
protected function filterAndMapItems(Collection $items, ?User $user): array
|
||||
{
|
||||
return $items
|
||||
->filter(fn (MenuItem $item) => $item->isVisibleToUser($user))
|
||||
->map(fn (MenuItem $item) => $this->mapItem($item, $user))
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
protected function mapItem(MenuItem $item, ?User $user): array
|
||||
{
|
||||
$children = $item->children->count() > 0
|
||||
? $this->filterAndMapItems($item->children, $user)
|
||||
: [];
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'title' => $item->title,
|
||||
'url' => $item->url,
|
||||
'icon' => $item->icon,
|
||||
'target' => $item->target,
|
||||
'is_active' => $this->isActiveUrl($item->url),
|
||||
'children' => $children,
|
||||
'has_children' => count($children) > 0,
|
||||
];
|
||||
}
|
||||
|
||||
protected function isActiveUrl(?string $url): bool
|
||||
{
|
||||
if (!$url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentUrl = request()->url();
|
||||
$currentPath = request()->path();
|
||||
|
||||
if ($url === $currentUrl) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$urlPath = parse_url($url, PHP_URL_PATH) ?? '';
|
||||
|
||||
return $urlPath && str_starts_with('/' . ltrim($currentPath, '/'), $urlPath);
|
||||
}
|
||||
|
||||
public function getAvailableModules(): array
|
||||
{
|
||||
$modules = [];
|
||||
$modulesPath = app_path('Modules');
|
||||
|
||||
if (!is_dir($modulesPath)) {
|
||||
return $modules;
|
||||
}
|
||||
|
||||
foreach (scandir($modulesPath) as $module) {
|
||||
if ($module === '.' || $module === '..' || !is_dir($modulesPath . '/' . $module)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$slug = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $module));
|
||||
$modules[$slug] = $module;
|
||||
}
|
||||
|
||||
return $modules;
|
||||
}
|
||||
|
||||
public function getAvailableRoutes(): array
|
||||
{
|
||||
$routes = [];
|
||||
|
||||
foreach (app('router')->getRoutes() as $route) {
|
||||
$name = $route->getName();
|
||||
if ($name && !str_starts_with($name, 'filament.') && !str_starts_with($name, 'livewire.')) {
|
||||
$routes[$name] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($routes);
|
||||
return $routes;
|
||||
}
|
||||
}
|
||||
30
src/app/View/Components/FrontendMenu.php
Normal file
30
src/app/View/Components/FrontendMenu.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use App\Services\MenuService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class FrontendMenu extends Component
|
||||
{
|
||||
public array $items = [];
|
||||
|
||||
public function __construct(
|
||||
public string $menu = 'header',
|
||||
public string $class = '',
|
||||
) {
|
||||
$menuService = app(MenuService::class);
|
||||
$this->items = $menuService->getMenu($menu) ?? [];
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('components.frontend-menu');
|
||||
}
|
||||
|
||||
public function hasItems(): bool
|
||||
{
|
||||
return count($this->items) > 0;
|
||||
}
|
||||
}
|
||||
30
src/app/View/Components/FrontendMenuResponsive.php
Normal file
30
src/app/View/Components/FrontendMenuResponsive.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use App\Services\MenuService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class FrontendMenuResponsive extends Component
|
||||
{
|
||||
public array $items = [];
|
||||
|
||||
public function __construct(
|
||||
public string $menu = 'header',
|
||||
public string $class = '',
|
||||
) {
|
||||
$menuService = app(MenuService::class);
|
||||
$this->items = $menuService->getMenu($menu) ?? [];
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('components.frontend-menu-responsive');
|
||||
}
|
||||
|
||||
public function hasItems(): bool
|
||||
{
|
||||
return count($this->items) > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user