Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
921 stars 168 forks source link

Two way binding #149

Closed odesey closed 8 years ago

odesey commented 8 years ago

I am wondering if what I would like to do is possible with this package.

So using your example Roles.addUsersToRoles(joesUserId, ['manage-team','schedule-game'], 'manchester-united.com') I will be able to query the user account (joe) and see that they have 'manage-team and schedule-game "permissions" for the manchester-united.com team. Is there anyway I can query the team collection and see what users have a specified role? I guess like a DNS reverse look-up :-)

So for example, I want to do SportsTeamCollection.findOne({_id: sportsTeamId}).roles('manage-team') and I should get an array or cursor of all the user ID's that have been assigned this role.

My reason for this is simple, in my app there are far less "teams" than users, and it just makes sense from a performance standpoint to query the "team" collection and get the list of users that have a specified role (this list will be used throughout the app) as opposed to querying thousands of users to build a list of the ones that have access to a specified team.

alanning commented 8 years ago

Currently there's no good way to do this with the roles package but there are workarounds. It's due to the database structure used which makes it impossible to create an index on the user's groups. This will be changed in roles v2.0 so that indexing will be possible and then you can just do a regular mongo query.

What we do in our app is to use a separate field to indicate "membership" and index that. So for example, each user has a "channels" field and contains the groups that the user is a member of. The roles package is used to determine specific permissions inside the group but not membership in the group itself.

So to display a list of users for a group, we query on the "channels" field. Then we can filter down further by checking roles in the app code (which hopefully will be a much smaller sub-set of users).

odesey commented 8 years ago

Thanks for the response, I ended up using a separate field to store the membership data as well. Looking forward to version 2.0 of the package!