bezhanSalleh / filament-shield

The easiest and most intuitive way to add access management to your Filament Admin Resources, Pages & Widgets through `spatie/laravel-permission`

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Install is not finding relations

bamod33 opened this issue · comments

Hi,

Doing: php artisan shield:install

It doesn't add permissions for relations. Example:

class TargetsResource extends Resource (having: protected static ?string $model = Target::class;)

RelationManagers\CriteriaRelationManager::class, (having: protected static string $relationship = 'criteria';)

CriterionPolicy.php is not created unless you add explicit resource for criteria (CriteriaResource having protected static ?string $model = Criterion::class;)

This behaviour makes that the relation is not shown.

Thanks

Hi,

I don't really understand your problem...

As far as I know, permissions and policies aren't relationship-related.

Are you trying to use permissions on a relation manager?

The problem is that php artisan shield:install does not create permissions for the model that is being related in the RelationManager, so the relationship does not appear in the panel.

When I launch install, it generates permissions for all models, but it does not create permissions for models that are linked through a RelationManager (i.e. those that are used through a pivot).

Model A <- PIVOT -> MODEL B

The policy for MODEL B is not generated, and therefore does not appear in the panel.

Did you create a resource for MODEL B?

Did you create a resource for MODEL B?

No I didn't, cause I don't want to have a dedicated resource for model B

That is why Shield didn't generate your MODEL B policy.

You need the MODEL B resource if you want Shield to create the MODEL B policy, and if you do not want this resource to appear in your navigation, you can add this line:

// Add this line to your ModelbResource.php file
protected static bool $shouldRegisterNavigation = false;

Hi @micraux

That works, but I feel that way a little tricky. Maybe as a future improvement the plugin could search for this kind of relations that doesn't have a dedicated Resource

Anyway, thanks for the help :)

You're welcome!

I don't think it would be an improvement to create all policies based on models because:

  • you may want to use Filament only with certain models and not all your application models
  • you may have created your own policies for your front app and do not want Shield to replace them
  • and maybe other reasons I don't see right now...

Remember that Filament was originally created to make an admin, not to be a full app.

That is why (I suppose) Shield only generates policies for models with a resource.