Integrate Spatie Laravel Permission to replace the legacy role system. This includes: - Adding `spatie/laravel-permission` dependency. - Implementing `Role` and `User` model updates with `HasRoles` trait. - Adding migrations for permission and role tables. - Creating `RoleResource` and `UserResource` for Filament administration. - Adding a `RoleAndPermissionSeeder` for initial setup. - Updating `User` model helper methods to utilize role checks.
162 lines
5.4 KiB
PHP
162 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\RoleResource\Pages;
|
|
use App\Models\Role;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class RoleResource extends Resource
|
|
{
|
|
protected static ?string $model = Role::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-shield-check';
|
|
protected static ?string $navigationLabel = 'نقشها';
|
|
protected static ?string $modelLabel = 'نقش';
|
|
protected static ?string $pluralModelLabel = 'نقشها';
|
|
protected static ?string $navigationGroup = 'تنظیمات سیستم';
|
|
protected static ?int $navigationSort = 2;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Forms\Components\Section::make('اطلاعات نقش')
|
|
->schema([
|
|
Forms\Components\TextInput::make('name')
|
|
->label('نام نقش (انگلیسی)')
|
|
->required()
|
|
->unique(ignoreRecord: true)
|
|
->maxLength(255),
|
|
|
|
Forms\Components\Select::make('guard_name')
|
|
->label('نوع Guard')
|
|
->options([
|
|
'web' => 'Web',
|
|
'api' => 'API',
|
|
])
|
|
->default('web')
|
|
->required(),
|
|
])->columns(2),
|
|
|
|
Forms\Components\Section::make('دسترسیها')
|
|
->schema([
|
|
Forms\Components\CheckboxList::make('permissions')
|
|
->label('دسترسیهای این نقش')
|
|
->relationship('permissions', 'name')
|
|
->columns(3)
|
|
->bulkToggleable(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('id')
|
|
->label('شناسه')
|
|
->sortable(),
|
|
|
|
Tables\Columns\TextColumn::make('name')
|
|
->label('نام')
|
|
->searchable()
|
|
->sortable()
|
|
->badge()
|
|
->color(fn (string $state): string => match ($state) {
|
|
'super_admin' => 'danger',
|
|
'admin' => 'warning',
|
|
'staff' => 'info',
|
|
'customer' => 'success',
|
|
default => 'gray',
|
|
}),
|
|
|
|
Tables\Columns\TextColumn::make('guard_name')
|
|
->label('Guard')
|
|
->badge()
|
|
->color('gray'),
|
|
|
|
Tables\Columns\TextColumn::make('users_count')
|
|
->label('تعداد کاربران')
|
|
->counts('users')
|
|
->badge()
|
|
->color('info'),
|
|
|
|
Tables\Columns\TextColumn::make('permissions_count')
|
|
->label('تعداد دسترسیها')
|
|
->counts('permissions')
|
|
->badge()
|
|
->color('success'),
|
|
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->label('تاریخ ایجاد')
|
|
->dateTime('Y/m/d')
|
|
->sortable(),
|
|
])
|
|
->filters([])
|
|
->actions([
|
|
Tables\Actions\EditAction::make()->label('ویرایش'),
|
|
Tables\Actions\DeleteAction::make()->label('حذف'),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
]),
|
|
])
|
|
->defaultSort('created_at', 'desc');
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListRoles::route('/'),
|
|
'create' => Pages\CreateRole::route('/create'),
|
|
'edit' => Pages\EditRole::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
// ... بقیه کد فعلی (form, table, getPages) ...
|
|
|
|
// ─── Access Control (فقط super_admin) ───
|
|
|
|
protected static function getCurrentUser(): ?\App\Models\User
|
|
{
|
|
$user = Auth::user();
|
|
return $user instanceof \App\Models\User ? $user : null;
|
|
}
|
|
|
|
public static function canViewAny(): bool
|
|
{
|
|
$user = static::getCurrentUser();
|
|
return $user !== null && $user->hasRole('super_admin');
|
|
}
|
|
|
|
public static function canCreate(): bool
|
|
{
|
|
return static::canViewAny();
|
|
}
|
|
|
|
public static function canEdit($record): bool
|
|
{
|
|
return static::canViewAny();
|
|
}
|
|
|
|
public static function canDelete($record): bool
|
|
{
|
|
// جلوگیری از حذف نقش super_admin حتی توسط super_admin
|
|
if ($record->name === 'super_admin') {
|
|
return false;
|
|
}
|
|
return static::canViewAny();
|
|
}
|
|
} |