Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Postgres RLS integration #249

Closed
lukinovec opened this issue Jun 7, 2023 · 0 comments
Closed

[4.x] Postgres RLS integration #249

lukinovec opened this issue Jun 7, 2023 · 0 comments

Comments

@lukinovec
Copy link
Collaborator

Postgres RLS integration {#postgres-rls-integration}

To integrate Postgres RLS in your app with single-database tenancy:

  • Enable PostgresRLSBootstrapper in the Tenancy config
  • Uncomment the CreatePostgresUserForTenant and DeleteTenantsPostgresUser jobs in TenancyServiceProvider so that when you create a new tenant, a Postgres user gets created for it, and when you delete a tenant, its Postgres user gets deleted
  • Make sure the DB is migrated and run php artisan tenants:create-rls-policies. This will create RLS policies for tables of all models that use BelongsToTenant or BelongsToPrimaryModel and are located in the directories specified in the customizable static Tenancy::$modelDirectories property (App/Models by default)
  • To make existing tenants use RLS, you can use the tenants:create-postgres-user command to create Postgres users for the tenants.

To scope models using RLS, you can either:

  • set config('tenancy.database.rls) to true – queries of all models that use the BelongsToTenant trait will be scoped using RLS
  • or make models that belong to tenants directly (= models that use BelongsToTenant) implement the RlsModel interface – queries of all models that use the BelongsToTenant and implement the RlsModel interface will get scoped using RLS
    Note that tenants:create-rls-policies only creates policies for tables that belong to tenants (directly, or through a primary model).
@stancl stancl closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2024
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants