I have been stuck on this now for a couple of days. I am sorry but I should have provided an example - I know these sorts of problems can be conceptually challenging.
First I have a group:
{"_id": “grp-xxx”, “type”: “group”, “tenant”: “tenant1”, “group”: “Test1”, “members”: [“userA”, “userb”]}
So in my sync function I create a channel for the tenant/group and grant the members of the group access to it. Anything I put into that group’s channel will only be seen by that group.
Then I have record that should only be seen by this group:
{"_id": “rec-xxx”, “type”: “record”, “tenant”: “tenant1”, “groups”:[“Test1”] …}
So in my sync function, I add the record to the tenant/group channel. Then I have a note on the record:
{"_id": “note-xxx”, “type”: “note”, “tenant”: “tenant1”, “rec-ref”: “rec-xxx” … }
So in my sync function, the note doesn’t know what groups the original record was assigned to, so I have no way of knowing how to add it to the appropriate group channel(s).
I had thought about creating a channel for each record itself, so that all notes could be added to the record channel, but the record document doesn’t know about the members of the group, so I have no way to assign the correct members to the channel if I create it for the record instead of the group. [This problem could be overcome if my group was an actual Sync Gateway role, because then I could just assign the role to the channel, but since groups are dynamic, and roles can only be created by administrators on the admin REST API on the server, outside the Sync function, my groups are not currently roles].
My app has a lot of different groups, access rights, types of documents, and references between documents like this.
I don’t really see that I am doing anything that isn’t common practice in NoSQL storage design, but the whole application is very dynamic: dynamic tenants, dynamic groups, dynamic permission sets, dynamic logical storage containers (with individual security permissions) etc. All in all, pretty standard stuff for an enterprise business application.
Trying to figure out how to apply a security model is becoming mental gymnastics and I am having to re-design my whole storage model over and over as I come across more and more scenarios like this I can’t figure out.
Probably the biggest underlying issue is the limitations on roles. You can’t dynamically create them in the Sync function, you can’t get the list of members of a role in the Sync Function, and you can’t assign roles to other roles.
Where I am headed now is re-factoring to make my groups actual Sync Gateway roles. This creates a lot more work for me, and introduces more user scenarios to my mobile app that won’t work offline (if I try to solve this with a REST API). I am trying to get my head around whether I can make group documents a stateful workflow with server-side agents to provision the group, but I have not gotten that far yet…