Meteor.users.find () - how to filter by groups in alanning: role package

I am trying to create an admin section on my website. The administrator can go to the administrator page and see the table of users in his group. I only want to publish users who are in this group of administrators. For instance. he is in the ['soccer'] group, but I would not want him to see all the users in the ['soccer'] group. A user can be in several groups, but I should be able to understand this as soon as I understand how to request it.

Anyway, here is what I still have:

Meteor.publish('users', function() {

  groups = Roles.getGroupsForUser(this.userId)
  group = groups[0] //Just get the first group for now

  // If the user is an admin of any group (the first one in the array)
  if (Roles.userIsInRole(this.userId, ['admin'], group)) {
    return Meteor.users.find({roles: {$in: groups}}, 
                             {fields: {emails: 1,
                                       createdAt: 1,
                                       roles: 1
                                      }
    });
  } else {
    console.log('null')
    return null;
  }
});

My subscription on my router:

Meteor.subscribe("users")

Now when I replace:

{roles: {$in: groups}} 

using only:

{}

, , . , ? .

+4
3

5% - , , , , , .map , -.

/server/methods.js

Meteor.methods({

  returnAdminUsers: function(){
    var results = [];

    var results = Roles.getUsersInRole(['admin']).map(function(user, index, originalCursor){
      var result = {
        _id: user._id,
        emails: user.emails,
        createdAt: user.createdAt,
        roles: user.roles
      };
      console.log("result: ", result);
      return result;
    });

    console.log("all results - this needs to get returned: ", results);

    return results;
  }

})

/client/somethingsomething.js

Meteor.call("returnAdminUsers", function(error, result){
  if(error){
    console.log("error from returnAdminUsers: ", error);
  } else {
    Session.set("adminUsers", result);
  }
});

:

Template.somethingsomething.helpers({
  adminUsers: function(){ return Session.get("adminUsers") }
});

/client/somethingsomething.html

{{#each adminUsers}}
  The ID is {{_id}}
{{/each}}
+2

, , , -)

Meteor.users.find({'roles.__global_roles__':'admin'}).fetch()

__ global_roles - . .

http://docs.mongodb.org/manual/tutorial/query-documents/#match-an-array-element

+2

If you are using meteor-tabular / TabularTables, this answer may help:

Display only users with a specific role in the meteor table table

If you want to check all the rules of such a group:

  Meteor.users.find({
    'roles.myrole': {$exists : true}
  });

Or just a few:

  Meteor.users.find({
    'roles.myrole': {$in: ['viewer', 'editor']}
  });
+1
source

All Articles