I am migrating from a custom user rights management system to Alanning:roles v2.0. I have a very basic structure:
- A basic user
- Groups of users, each with specific settings. I store them in a "group" collection.
- A group admin status for users who manage the groups (each group has its group admins).
I was previously storing the group members and admins mongo _id
in the "group" document. This way, I could publish the groups reactively : I just had to check if the userId
was in the group document, in the "members" or "admins" fields.
Now that I switched to a right management enforced by Alanning:roles, I do something like this in my publication :
const userGroupsAsAdmin = Roles.getPartitionsForUser (this.userId, ['group_admin'])
const userGroupsAsMember = Roles.getPartitionsForUser (this.userId, ['member'])
const selector = {$or:[{'_id':{$in: userGroupsAsMember}},{'_id':{$in: userGroupsAsAdmin}}]}
const options = {}
const response = Groups.find(selector, options)
return response
Note that Roles.getPartitionsForUser ()
is just the new function name for Roles.getGroupsForUser ()
.
The problem here is that the publication don't watch for changes in the role
collection, so when a user becomes member, the publication isn't updated. I know this is a common issue and I know 3 ways to fix this, but none of them feels satisfying:
The best candidate: denormalize and duplicate. I keep my
members
andadmins
fields in the group document. What bugs me is that I will keep 2 versions of the same thing and create a possibility for inconsistencies to appear.Add an argument to the publication and rerun it using this argument (e.g.
userGroupsAsMember
) but it relies on client and makes it send unnecessary info.Use low level publication api, either directly or using a package. I already did this directly in the past but I don't want to rely on
Cursor.observe()
anymore because it doesn't scale efficiently and create an unnecessary server load.
Am I missing an option? If not, what would be the best way to keep my publication reactive?