-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add models and helpers for multi tenancy #45
Conversation
This is intended to be a replacement for Apartment using row level security. It has helpers for enabling row level security, and methods for switching tenants that can replace the existing apartment methods fairly seamlessly. It also has a module that can be used to set whether a model is tenanted or not, and a helper to check for whether a model is tenanted.
This was from this code originally being in ApplicationRecord and then being copied out.
We removed the tenant_id thread variable in favor of just storing the entire tenant object
Also use the relationship on ApplicationInstance to get the tenant
Make the user name configurable, as well as what table is used for the tenants.
It's shorter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! There's a lot of complicated rails internals here that look okay but I'm also not an expert. We really need some security tests related to switching tenants and trying to do things that should raise.
end | ||
|
||
def self.switch_tenant_legacy(tenant_key, &block) | ||
tenant = AtomicTenant.tenant_model.find_by(key: tenant_key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check it exists, otherwise I think we will get switched to public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
lib/atomic_tenant/tenantable.rb
Outdated
super | ||
|
||
if subclass <= ActiveRecord::Base && !subclass.abstract_class? | ||
AtomicTenant::Tenantable.register_private_tenanted_model(subclass) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever. There are a lot of things in this function I've never seen before
schema.rb doesn't have the row level security policies, so if the database is loaded from it in CI or locally then it won't have them either.
These weren't real vulnerabilities, but it's easy enough to make Brakeman happy.
This will preserve RLS policies
This was creating 'rls."tenant_id"' which is not what we want.
This method is meant to be used from within the class, not externally
This is intended to be a replacement for Apartment using row level security. It has helpers for enabling row level security, and methods for switching tenants that can replace the existing apartment methods fairly seamlessly. It also has a module that can be used to set whether a model is tenanted or not, and a helper to check for whether a model is tenanted.