balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

[Proposal] Sails.js Multi Tenancy on Document Level #7088

Open Goostavo opened 3 years ago

Goostavo commented 3 years ago

I'm willing to implement a way of doing multi tenancy on sails, using the document level approach. As it will works on all adapters easily.

Implementation and approach

I'm willing to add a tenant_id column on every multi tenant table/collection. And also on users table/collection. O login a policy will add the tenant_id to the request and every model call will ensure that is being used.

What i need to do:

  1. Implement a policy that adds a tenant from user after authentication (Sails fully support, nothing to do here).
  2. Intercept all model calls to ensure tenant is used (See Model intercept for details)

Why intercept model and not controllers? Controller might use multiple model calls, so it's impossible to guarantee the usage. Also blueprints will not work.

Model intercept

I want to make a default implementation that enables the tenant_id verification or return error automatically on every model call.

"beforeFind"/"beforeDelete": guaranteed that where contains tenant_id, else returns error "beforeCreate"/"beforeCreateEach": Automatically overrides the tenant_id of each document with tenant_id

Any toughts on implementation (where should i edit to intercept model calls, is it possibile to make a hook for it?), or possible problems that i might find?


Node version: v14 Sails version (sails): 1.x ORM hook version (sails-hook-orm): 3.x


sailsbot commented 3 years ago

@Goostavo Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

eashaw commented 3 years ago

Hi @Goostavo, Thanks for using Sails. If you're interested in adding multi tenancy, we recommend adding an organization model and building the app around multi tenancy, instead of going the intercept route.