Meteor.users.find() - how to filter by group in alanning:roles package

2.7k views Asked by At

I'm trying to make an admin section on my website. The admin can go to the admin page and see a table of the users in his group. I only want to publish a the users that are in that admin's group. E.g. he is in the group ['soccer'] but I wouldn't want him to see all users in the ['hockey'] group. A user can be in more than one group but I should be able to figure that out once I understand how to query it.

Anyways, here's what I have so far:

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 in my router:

Meteor.subscribe("users")

Now when I replace:

{roles: {$in: groups}} 

with just:

{}

It works, but my table contains all users instead of just the users of a group. What query should I put to make this work? I'm using the roles package by alanning.

3

There are 3 answers

17
fuzzybabybunny On BEST ANSWER

This code has about a 5% chance of working out of the box because I have no collection to test this on, I have no way of running this code, I don't have the roles package, I don't have your users database, and I've never done .map on a cursor before, haha.

/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);
  }
});

And then in your helpers:

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

/client/somethingsomething.html

{{#each adminUsers}}
  The ID is {{_id}}
{{/each}}
0
trayan.dev On

you can do a simple query and add your fields, sorting, what you want ;-)

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

__global_roles is the default group. replace it with the group you need.

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

0
Liko On

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

Display only users with a specific role in meteor-tabular table

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

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

Or with just a few:

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