Create Policies for each Auth Model (Multi Panels)

I have multi panels and I am trying to use Policies for the multi panels I have Admin Model and User Model
3 Replies
Abi
Abi2mo ago
try and see if this works inside your Policy methods. I was planning to use this.
if (Filament::getCurrentPanel()->id() === 'admin') {
//Policy Condition for admin panel goes here
}
if (Filament::getCurrentPanel()->id() === 'admin') {
//Policy Condition for admin panel goes here
}
pratik
pratik2mo ago
If you are trying to achieve different polices for different panels you can do something like this in AuthServiceProvider:
use Illuminate\Support\Facades\Gate;

Gate::guessPolicyNamesUsing(function ($modelClass) {
return str($modelClass)
->replace(
search: 'App\Models\\'.str(filament()->getCurrentPanel()- >getId())->studly(),
replace: 'App\Policies\\'.str(filament()->getCurrentPanel()->getId())->studly()
)
->append('Policy')
->toString();
});

$this->registerPolicies();
use Illuminate\Support\Facades\Gate;

Gate::guessPolicyNamesUsing(function ($modelClass) {
return str($modelClass)
->replace(
search: 'App\Models\\'.str(filament()->getCurrentPanel()- >getId())->studly(),
replace: 'App\Policies\\'.str(filament()->getCurrentPanel()->getId())->studly()
)
->append('Policy')
->toString();
});

$this->registerPolicies();
Suppose you have two panel providers, AdminPanelProvider and CustomerPanelProvider, To use this implementation, you should have Admin related Models in App\Models\Admin directory and Customer related models in App\Models\Customer directory. Also, Admin related policies in App\Policies\Admin and Customer related policies in App\Policies\Customer directory. The directory name is based on Panel's id.
Dan Harrin
Dan Harrin2mo ago
you can also just check what the user is inside each policy method