Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Bouncer handle tenant-specific data in a polymorphic relationship


How does Bouncer handle tenant-specific data in a polymorphic relationship


Bouncer provides two main approaches for handling tenant-specific data in a polymorphic relationship:

Everything Scoped

The first approach is to scope everything to the current tenant:

php
Bouncer::scope()->to($tenantId)->everything();

This will scope all models and all relationships to the given tenant ID. This ensures that each tenant has its own set of roles and abilities, and users can only access the resources associated with their tenant.

Scoped Relationships

The second approach is to only scope the relationships, while keeping the roles and abilities global:

php
Bouncer::scope()->to($tenantId)->onlyRelations();

This will only scope the relationships to the given tenant, but the actual roles and abilities will be shared across tenants. This can be useful if you want to have a common set of roles and abilities, but restrict access to the associated resources based on the tenant.

The search results also mention that Bouncer provides a way to configure this behavior, potentially allowing you to exclude roles from being scoped, or make the scoping behavior more customizable.

Overall, Bouncer provides flexible options for handling multi-tenancy, allowing you to choose between fully scoping everything to the tenant, or only scoping the relationships while keeping roles global. The specific approach you choose will depend on the requirements of your application.

Citations:
[1] https://github.com/JosephSilber/bouncer/issues/217
[2] https://laracasts.com/discuss/channels/eloquent/polymorphic-relation-for-users
[3] https://laracasts.com/discuss/channels/eloquent/get-the-data-from-2-tables-using-polymorphic-relationships
[4] https://www.youtube.com/watch?v=zK6wsIAlMdI
[5] https://josephsilber.com/posts/2022/05/17/releasing-bouncer-laravel-roles-and-permissions