ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.56k stars 264 forks source link

Tenancy for all objects in model, except for specific conditions #291

Open darylpdavies opened 2 years ago

darylpdavies commented 2 years ago

I have a model called Post. Posts require tenancy and are only accessible by their owners.

However - if a Post is set to "global" (or created by a site admin), it will be accessible by everyone.

acts_as_tenant :account, has_global_records: true

In my controller: @post = Post.find(params[:id])

if @post.global? || @post.account.admin?
     // Accessible by everyone
end
pruzicka commented 2 years ago

I don't believe it's possible to do something like this now. There was some work here #86, but again I don't think it's possible now to share objects between tenants.

darylpdavies commented 2 years ago

Thanks @pruzicka – that definitely points me in the right direction. My platform allows users to see only their own posts, but I wanted to add the ability for the admins to create "shared posts" that everyone can see but not edit.

pruzicka commented 2 years ago

Thanks @pruzicka – that definitely points me in the right direction. My platform allows users to see only their own posts, but I wanted to add the ability for the admins to create "shared posts" that everyone can see but not edit.

Well I would use it as well. I have libraries that are owned by tenants yet I would like to have "shared" library that each tenant could use. One option would be to always create copy for each tenant but that is "brute force" way, not smart at all. If I remember there was an option to have "nil" in tenant_id field, that record would be "shared" record. But it doesn't work anymore

darylpdavies commented 2 years ago

Thanks @pruzicka – that definitely points me in the right direction. My platform allows users to see only their own posts, but I wanted to add the ability for the admins to create "shared posts" that everyone can see but not edit.

Well I would use it as well. I have libraries that are owned by tenants yet I would like to have "shared" library that each tenant could use. One option would be to always create copy for each tenant but that is "brute force" way, not smart at all. If I remember there was an option to have "nil" in tenant_id field, that record would be "shared" record. But it doesn't work anymore

That sounds very much like my use case – I was thinking of adding a "global" setting, so that it requires a little more confirmation on the admin side. Could be a bad situation if a post were to accidentally have a "nil" tenant to suddenly be viewable by everyone.

Something like: acts_as_tenant :account, allow_global_tenant: "is_global"

"is_global" would be a customizable column with a boolean value - if true, then the object is shared for all users. Then you can use something like pundit to allow users to view these "global" posts but not edit/delete them. I'm hoping to use these objects as templates and allow users to "clone" and edit them without having to start a new post from scratch.

Would that work for your libraries?

pruzicka commented 2 years ago

Thanks @pruzicka – that definitely points me in the right direction. My platform allows users to see only their own posts, but I wanted to add the ability for the admins to create "shared posts" that everyone can see but not edit.

Well I would use it as well. I have libraries that are owned by tenants yet I would like to have "shared" library that each tenant could use. One option would be to always create copy for each tenant but that is "brute force" way, not smart at all. If I remember there was an option to have "nil" in tenant_id field, that record would be "shared" record. But it doesn't work anymore

That sounds very much like my use case – I was thinking of adding a "global" setting, so that it requires a little more confirmation on the admin side. Could be a bad situation if a post were to accidentally have a "nil" tenant to suddenly be viewable by everyone.

Something like: acts_as_tenant :account, allow_global_tenant: "is_global"

"is_global" would be a customizable column with a boolean value - if true, then the object is shared for all users. Then you can use something like pundit to allow users to view these "global" posts but not edit/delete them. I'm hoping to use these objects as templates and allow users to "clone" and edit them without having to start a new post from scratch.

Would that work for your libraries?

Yes, that would be perfect. so some records would be visible to everyone, or better say could be visibile for everyone. first would be is_global: true and second condition would be cancancan or pundit. Not all tenants could see everything, let't say it would be additional service to sell to them, if they can have global library as a start