[SOLVED] Alanning:roles show only users in my group

I am using alaning:roles and I want to publish only users that are in my group (each user only gets to be in one group). Any ideas?

I’ve tried:
Meteor.publish("directory", function() { var my_groups = Roles.getGroupsForUser(this.userId); return Meteor.users.find({'groups': {$in: my_groups}}, {fields:{emails:1, profile:1, roles:1, groups: 1}}); }

but it doesn’t work. It only gives me my current user.

Did you use meteor toys to make sure there are others in the group etc?

I currently have 4 users in the same group, and two users in a different group.

When autopublish is on, I can see all of those users and verify their groups.

I’m not sure. The query looks correct to me as long as my_groups is an array. If you but the query into a variable and log it, it is only returning your current user?

typing ‘Meteor.users.find().count()’ into the firebug console gives me ‘1’

‘console.log(my_groups)’ gives me [‘default_group’]. default_group is the correct name of the group.

This is a the code I use to create my users (sorry for the poor formatting, I don’t know how to make it nicer):

var defaultUsers = [
{name: “Supah Fly”, email: "fly@supah.com", password: “1234”, roles: [‘admin’], group: Roles.GLOBAL_GROUP},
{name: “Charlie Administrator”, email: "admin@some.com", password: “1234”, roles: [‘admin’], group: ‘default_group’},
{name: “Jonny FieldUser”, email: "field@some.com", password: “1234”, roles: [‘field’], group: ‘default_group’},
{name: “Suzie Manager”, email: "manager@some.com", password: “1234”, roles: [‘manager’], group: ‘default_group’}

if (Meteor.users.find().count() === 0) {
_.each(defaultUsers, function(user) {
var id;
id = Accounts.createUser({
email: user.email,
password: user.password,
profile: {
fullname: user.name
if(user.roles.length > 0) {
Roles.addUsersToRoles(id, user.roles, user.group);


I don’t think ‘groups’ works this way. Looking at my own users:

{ "_id" : "gJSWWLmkF7F2QiBAQ", "createdAt" : ISODate("2016-08-05T02:09:16.964Z"), "services" : { "password" : { "bcrypt" : "$2a$10$bcRi17mkB6bVZybp5DmgJuY90J4XgPKdUKLLY6IVciOUAuBRSm/7e" } }, "emails" : [ { "address" : "foo@foo.com", "verified" : false } ], "profile" : { "name" : { "first" : "someFName", "last" : "someLName" } }, "roles" : { "bTHwzLN7y4FLWRm7Z" : [ "kAvnw5HEAjnQCcLR9", "42LMA83SWLwEABmLX" ] } }

So there is no key groups. In the example above, I use groups which I create separately, so in this case the user is in group: ‘bTHwzLN7y4FLWRm7Z’. I don’t believe there are any getUsersForGroup or isUserInGroup methods, so probably best coding one up properly.

I think (from memory) the way i solved this was to come up with a array of groups. Then individually sort through users to work out an array of users who were in the specific group/specific role using userIsInRole(user, ['somerole], group]. The final return find then becomes a simpler check if id is in my user id array.

When I iterate through the all of Meteor.users.find() I access the users’ role like this:

So if I know that users will only ever have one group, I thought I’d try the publish statement like this:
Meteor.publish('usersInMyGroup', function() { var my_group = Roles.getGroupsForUser(this.userId)[0]; var field = 'roles.'+my_group; return Meteor.users.find({field: {$exists: true}}, {fields:{emails:1, profile:1, roles:1}}); });

Once again, I only get one user (my current user) returned. Sigh.

Well, I now have something that is working

Meteor.publish('usersInMyGroup', function() { var my_group = Roles.getGroupsForUser(this.userId)[0]; var query = {}; query['roles.'+my_group] = {$exists: true}; return Meteor.users.find(query, {fields:{emails:1, profile:1, roles:1}}); });

1 Like