joas8211 / payload-tenancy

Multi-tenancy plugin for Payload CMS
MIT License
122 stars 6 forks source link

Globals shared between tenants #19

Closed DaniloArantesF closed 10 months ago

DaniloArantesF commented 10 months ago

First off, thank you for creating this!

The plugin works seamlessly for collections, however, it seems that globals are shared between all tenants. I have two users and three tenants and no matter which tenant or user I use, the globals update for all of them. I am not sure if this is by design or an issue with my setup.

Here is my payload config NVIDIA_Share_A9M5Gk1R9b

Please let me know if there are other details I can provide.

joas8211 commented 10 months ago

Isolating globals per tenant is not supported because I haven't figured out how that could be accomplished. If you know a way it can be done, please tell and I will add support for it. Collections have been isolated using access rules that restrict actions to documents that have matching value in tenant field (hidden field added by the plugin). But globals seems to only have one document which is the global, so having a global document for each tenant wouldn't work logically.

Now that I'm writing this, I can think one way this could be maybe done. What if the plugin adds a collection for globals that cannot be accessed by any users. That collection holds data for all globals and correct document is returned when global is read or written to. I'll look into this if it's possible. How does that sound to you? Any comments?

DaniloArantesF commented 10 months ago

Yes, that makes sense to me. Both the issue and your solution. I'm not too sure how that would work since I'm not too familiar with Payload integrations, unfortunately. If that's possible, I think it would be a great improvement. Thank you.

silveltman commented 10 months ago

Now that I'm writing this, I can think one way this could be maybe done. What if the plugin adds a collection for globals that cannot be accessed by any users. That collection holds data for all globals and correct document is returned when global is read or written to. I'll look into this if it's possible. How does that sound to you? Any comments?

I think your idea makes sense. Having a 'globals' collection in which the data is set, and then have the global file act as some kind of middleware, mapping the request to the right doc in the collection.

But wouldn't this make the UI look different than regular globals? Showing a 'collection of globals' to the /admin user. If so, al alternative approach could be:

Auto-generating globals with slugs like 'globalname-tenantname' for each tenant. Then have the base 'globalname' map to the correct 'globalname-tenantname'.

matthijs166 commented 10 months ago

What about the solution I proposed here: #payloadcms/payload/discussions/3268

  1. Create a globals collection with the same structure as your global
  2. Add a field to this collection for the scope (your domain, for example)
  3. Add in the global a new hook beforeChange
  4. In this hook store the values via the internal payload API to the globals collection (with scope)
  5. Add a new hook beforeRead or afterRead
  6. Fetch based on the scope the value from the globals collection and return the data within this hook

It does not seem hard to implement and users can use the singleton focused UI instead of having an extra step where only one item is in the collection.

matthijs166 commented 10 months ago

@joas8211 Maybe something to include into this plugin? Looks very clean and easy https://github.com/payloadcms/payload/discussions/3268#discussioncomment-6953744

joas8211 commented 10 months ago

I have published update (v1.2.0) that adds support for globals. In fact globals are now tenant isolated by default, so take that into account when updating. More information can be found from readme.