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 167 forks source link

How to get users in a group? #217

Closed luixal closed 4 years ago

luixal commented 7 years ago

The only option I see right now is going through all users and check their groups. Seems quite heavy.

Any ideas?

wiserweb commented 7 years ago

One solution might be to build your own query.

Try this in Robomongo

db.getCollection('users').find({ 'roles.__global_roles__': {$ne:null}}) You should get back all the users that have any group name.

Now just change for the group you are looking for

db.getCollection('users').find({ 'roles.__global_roles__': {$in: ['groupname']} })

Hope this helps.

muninn9 commented 7 years ago

I have a similar problem where I need to return all users with a specified role from all groups.

db.getCollection('users').find({ 'roles.__global_roles__': {$ne:null}}) just returns all users with a group name called "__globalroles_\".

It would be nice if Roles.getUsersInRole("specifiedRole") just returns all users with the "specifiedRole" in all groups when no group is specified.

The only way I could think to do this was to create a "Groups" collection and for every group call getUsersInRole using that group name:

_.each(Groups.find().fetch(), (group) => {
            users = users.concat(Roles.getUsersInRole("role", group.name).fetch())
});
alanning commented 7 years ago

Yeah, this is a known pain-point but the good news is that Roles v2 let's you do this kind of thing. So try v2! You can find it on the v2 branch in the repo. You'll need to clone the repo to your app's packages directory to get the functionality since at this time its not up on Atmosphere yet.

See the v2 readme for migration and changes between v1 and v2. Should be pretty much drop-in (but be sure to run the migration script).

FYI, the db structure of v1 roles field on the user record didn't allow for indexing so this kind of operation would be terrible to do performance-wise. But the v2 structure allows indexes on these.

kan-ashwin commented 6 years ago

Is there a plan to release v2 ? How do we fetch users in a role with v2 ?

mitar commented 4 years ago

Released.