Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What is the syntax for scoping all models and relationships to a given tenant in Bouncer


What is the syntax for scoping all models and relationships to a given tenant in Bouncer


the syntax for scoping all models and relationships to a given tenant in Bouncer is:

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

This will scope all models and all relationships to the given tenant ID.

The key points are:

1. Use the `Bouncer::scope()->to($tenantId)` syntax to scope to the current tenant.
2. Then call the `->everything()` method to scope all models and relationships.

This ensures that each tenant has its own set of roles and abilities, and users can only access the resources associated with their tenant.

The search results also mention an alternative syntax for scoping only the relationships, while keeping the roles and abilities global:

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

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

Citations:
[1] https://github.com/JosephSilber/bouncer/issues/217
[2] https://josephsilber.com/posts/2022/05/17/releasing-bouncer-laravel-roles-and-permissions
[3] https://laravelpodcast.com/episodes/bouncer-with-joseph-silber/transcript
[4] https://stackoverflow.com/questions/61803817/tweaking-scoping-rules-as-part-of-acts-as-tenant
[5] https://github.com/JosephSilber/bouncer/actions