bezhanSalleh/filament-shield

Resoures in the sub folders accessable by everyone

wikihadi opened this issue · 5 comments

Resoures in the sub folders accessable by everyone
Screenshot 2024-05-30 010932
داشبوردا

I agree, it's a nice Filament feature to have possibility to use sub folders for Models / Resources, but they won't work with Filament Shield when using subfolders... Any idea how to fix this ? thks

I also agree with you in filament docus they mentionds for subdirectories path used AuthServiceProvider but in laravel 11 there's not AuthServiceProvider class.
Screenshot 2024-06-13 at 1 13 17 AM

Hi
According to the changes of Laravel 11 (https://laravel.com/docs/11.x/authorization#manually-registering-policies) and also the documentation of this package (https://github.com/bezhanSalleh/filament-shield?tab=readme-ov-file#custom-folder-structure-for-models-or-third-party-plugins)
, to fix the problem:
Should manually register policies and their corresponding models within the boot method of your application's AppServiceProvider:

use App\Models\Order;
use App\Policies\OrderPolicy;
use Illuminate\Support\Facades\Gate;
 
/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    Gate::policy(Order::class, OrderPolicy::class);
}

it's already mentioned in the docs.

the problem isn't filament sub-folders resources, it's only models's sub-folders, according with the Laravel doc.
If you would like to define your own policy discovery logic, you may register a custom policy discovery callback using the Gate::guessPolicyNamesUsing method. Typically, this method should be called from the boot method of your application's AppServiceProvider:

Gate::guessPolicyNamesUsing(function (string $modelClass) {
    return str($modelClass)
        ->after('Models\\')
        ->prepend('App\\Policies\\')
        ->append('Policy')
        ->toString();
});