3

I am currently experimenting with adopting PostgreSQL row-level security for a server-side application in Laravel. My goal is to move multi-tenancy access rights into the DB, so as to completely remove them from application logic. This requires me to add a SET app.tenant = 'current_tenant_id' statement to the beginning of all database sessions. This needs to happen for both Eloquent ORM queries and raw DB queries.

So far, I've experimented with two approaches:

Modifying the query in a listener

This did not work because DB::listen callbacks trigger after the query has been executed. I could not find any other callbacks or Laravel Events that seemed appropriate.

Using HTTP middleware

DB::statement("SET app.tenant = '<current_tenant_id>'");

This seems to work, but it is less than ideal for a few reasons:

  1. It will break if I ever switch to separate read/write connections, because this code will choose a connection to operate on before performing any other DB operations.
  2. It's an incomplete solution, as it does not address, for example, background jobs. I could implement this in a background job base class, but that solution then suffers from the same problem as 1.

It also seems that I may be swimming upstream here, as I've yet to find any libraries, tutorials, or write-ups by anybody else attempting to do this. All of the many Laravel tenancy libraries I have looked at seem to rely entirely on either connection switching (multi DB) or WHERE clause (single DB) solutions.

Is there a way I can take advantage of row-level security from within Laravel, in a way that is mostly transparent to the development team?

1 Answer 1

-1

You should use something like spatie/laravel-multitenancy package to handle all tenancy related logic. It already solves the job tenant awareness, so all you need to do is create a "Task", that is executed right after the "currentTenant" is set, to set the tenant_id in the database connection.

Sign up to request clarification or add additional context in comments.

1 Comment

Does this solution address issue 1 ("It will break if I ever switch to separate read/write connections...")? I'm not sure it does; if not that would ideally be clarified in your answer. If you don't mind I'd like to change "should" to "could" as I don't think the opinion is not justified by your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.