bezhanSalleh / filament-shield

The easiest and most intuitive way to add access management to your Filament Admin Resources, Pages & Widgets through `spatie/laravel-permission`
MIT License
1.62k stars 181 forks source link

Bug on Custom Permission #408

Closed teguh02 closed 1 month ago

teguh02 commented 2 months ago

Hi, i was try to implement a custom role in my blank new resources, and the panel are not appears on the panel

Here my new resources

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EmployeeTaskManagementResource\Pages;
use App\Filament\Resources\EmployeeTaskManagementResource\RelationManagers;
use App\Models\Employee;
use App\Models\MasterPoint;
use App\Models\Task;
use App\Traits\AttributeTranslation;
use App\Traits\PanelMenuTranslations;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
use Illuminate\Support\Facades\Auth;

class EmployeeTaskManagementResource extends Resource implements HasShieldPermissions
{

    protected static ?string $model = Task::class;

    protected static ?string $navigationIcon = 'heroicon-o-rocket-launch';

    protected static ?string $navigationGroup = 'Employee';

    public static function getPermissionPrefixes(): array
    {
        return [
            'view',
            'view_any',
            'create',
            'update',
            'restore',
            'restore_any',
            'replicate',
            'reorder',
            'delete',
            'delete_any',
            'force_delete',
            'force_delete_any',
            'submission' // custom permission
        ];
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListEmployeeTaskManagement::route('/'),
            'create' => Pages\CreateEmployeeTaskManagement::route('/create'),
            'edit' => Pages\EditEmployeeTaskManagement::route('/{record}/edit'),
        ];
    }
}

My Task Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class Task extends Model
{
    use HasFactory, SoftDeletes, LogsActivity;

    protected $fillable = [
        'employee_id',
        'point_id',
        'assigned_by',
        'reviewed_by',
        'title',
        'description',
        'status',
        'due_date',
    ];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['employee_id', 'point_id', 'assigned_by', 'reviewed_by', 'title', 'description', 'status', 'due_date']);
    }

    public function employee()
    {
        return $this->belongsTo(Employee::class);
    }

    public function point()
    {
        return $this->belongsTo(MasterPoint::class);
    }

    public function assignedBy()
    {
        return $this->belongsTo(User::class, 'assigned_by');
    }

    public function reviewedBy()
    {
        return $this->belongsTo(User::class, 'reviewed_by');
    }

    public function taskSubmissions()
    {
        return $this->hasMany(TaskSubmission::class);
    }
}

My Task Policy

<?php

namespace App\Policies;

use App\Models\User;
use App\Models\Task;
use Illuminate\Auth\Access\HandlesAuthorization;

class TaskPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any models.
     */
    public function viewAny(User $user): bool
    {
        return $user->can('view_any_employee::task::management');
    }

    /**
     * Determine whether the user can view the model.
     */
    public function view(User $user, Task $task): bool
    {
        return $user->can('view_employee::task::management');
    }

    /**
     * Determine whether the user can create models.
     */
    public function create(User $user): bool
    {
        return $user->can('create_employee::task::management');
    }

    /**
     * Determine whether the user can update the model.
     */
    public function update(User $user, Task $task): bool
    {
        return $user->can('update_employee::task::management');
    }

    /**
     * Determine whether the user can delete the model.
     */
    public function delete(User $user, Task $task): bool
    {
        return $user->can('delete_employee::task::management');
    }

    /**
     * Determine whether the user can bulk delete.
     */
    public function deleteAny(User $user): bool
    {
        return $user->can('delete_any_employee::task::management');
    }

    /**
     * Determine whether the user can permanently delete.
     */
    public function forceDelete(User $user, Task $task): bool
    {
        return $user->can('force_delete_employee::task::management');
    }

    /**
     * Determine whether the user can permanently bulk delete.
     */
    public function forceDeleteAny(User $user): bool
    {
        return $user->can('force_delete_any_employee::task::management');
    }

    /**
     * Determine whether the user can restore.
     */
    public function restore(User $user, Task $task): bool
    {
        return $user->can('restore_employee::task::management');
    }

    /**
     * Determine whether the user can bulk restore.
     */
    public function restoreAny(User $user): bool
    {
        return $user->can('restore_any_employee::task::management');
    }

    /**
     * Determine whether the user can replicate.
     */
    public function replicate(User $user, Task $task): bool
    {
        return $user->can('replicate_employee::task::management');
    }

    /**
     * Determine whether the user can reorder.
     */
    public function reorder(User $user): bool
    {
        return $user->can('reorder_employee::task::management');
    }

/**
     * Determine whether the user can submit
     */
    public function submission(User $user, Task $task): bool
    {
        return $user->can('submission_employee::task::management');
    }
}

My Filament Providers

<?php

namespace App\Providers\Filament;

use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

// Plugins
use Swis\Filament\Backgrounds\FilamentBackgroundsPlugin;
use BezhanSalleh\FilamentShield\FilamentShieldPlugin;
use Hasnayeen\Themes\ThemesPlugin;

// Middleware
use Hasnayeen\Themes\Http\Middleware\SetTheme;

class SipegawaiPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('sipegawai')
            ->path('sipegawai')
            ->databaseNotifications(true)
            ->login()
            ->colors([
                'primary' => Color::Amber,
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
            ->pages([
                Pages\Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
                Widgets\AccountWidget::class,
                Widgets\FilamentInfoWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
                SetTheme::class,
            ])
            ->plugins([
                FilamentBackgroundsPlugin::make()
                    ->showAttribution(false),

                FilamentShieldPlugin::make(),
                ThemesPlugin::make()
                    ->canViewThemesPage(fn () => true),
            ]);
    }
}

My Super Admin Custom Permission Tabs (my custom permission was appears) Screen Shot 2024-08-13 at 09 51 24

What happen with my code?

bezhanSalleh commented 1 month ago

If you wanna get rid of those extra permissions in your custom permissions tab either do shield:install --fresh or remove them from your db. If that's not the case, could you rephrase your question?

bezhanSalleh commented 1 month ago

Closing this for now. No Activity!