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.6k stars 180 forks source link

Policy method is not getting generated as said in readme #398

Closed hardiklakhalani closed 3 months ago

hardiklakhalani commented 3 months ago

This is what I did as per readme.md

Out of the box Shield handles the predefined permissions for Filament resources. So if that's all that you need you are all set. If you need to add a single permission (for instance lock) and have it available for all your resources just append it to the following config key:

    permission_prefixes' => [
        'resource' => [
            'view',
            'view_any',
            'view_own',   //<---- Want to have this additional Policy method by default generated for All. same as other permissions in this list
            'create',
            'update',
            'restore',
            'restore_any',
            'replicate',
            'reorder',
            'delete',
            'delete_any',
            'force_delete',
            'force_delete_any',
            'lock',   <--- So as per readme.md, I added this for testing this feature, but not working
        ],
        ...
    ],

But on running php artisan shield:generate --all it's not adding lock() method inside this file \app\Policies\FooPolicy.php but it's adding view_own permissions in the Database for all the resource as expected though.

<?php

namespace App\Policies;

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

class FooPolicy
{
    use HandlesAuthorization;

     ...
     ...

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

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

    /**
     * Determine whether the user can view own foo.
     */
    public function viewOwn(User $user)  //<-------- this is the requirement for all the resources
    {
        return $user->can('view_own_foo');
    }
}

I added this question/bug (not sure) here because dedicated category seems 404 https://github.com/bezhanSalleh/filament-shield/discussions/new?category=q-a image

hardiklakhalani commented 3 months ago

I explored the vendor code, probably this function is executing the task https://github.com/bezhanSalleh/filament-shield/blob/df7ccdc630ceef5358a7c094e3578004fe93e3f5/src/Commands/Concerns/CanGeneratePolicy.php#L47

bezhanSalleh commented 3 months ago

shield only generates the default policy methods and any permissions you define. but doesn't create custom policy methods.