Globals shared between tenants
DaniloArantesF opened this issue · 6 comments
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.
Please let me know if there are other details I can provide.
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?
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.
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'.
What about the solution I proposed here: #payloadcms/payload/discussions/3268
- Create a globals collection with the same structure as your global
- Add a field to this collection for the scope (your domain, for example)
- Add in the global a new hook beforeChange
- In this hook store the values via the internal payload API to the globals collection (with scope)
- Add a new hook beforeRead or afterRead
- 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.
@joas8211 Maybe something to include into this plugin? Looks very clean and easy payloadcms/payload#3268 (reply in thread)