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

getRolesForUser returns undefined for users that aren't logged in #209

Closed traugdor closed 4 years ago

traugdor commented 8 years ago

I'm trying to list the roles for all users for a user management page. So far every attempt to access and print all roles for all users has yielded bad results.

'roles': function(id) {

    var roles = Roles.getRolesForUser(id, 'default-group');
    var output = "";
    output += roles[0];
    if(roles.length > 1) {
        for(var i = 1; i < roles.length; i++) {
            output += ", " + roles[i];
        }
    }

    return output;
},

Expected: Array of roles for any user submitted by _id.

Actual output: undefined for every other user except the one that is logged in.

Is there another way to access the roles for every user in the system? Or will I have to

  1. programmatically iterate through all known roles,
  2. catalog who is in each role and then
  3. rebuild the data around each user instead of each role...which may not even work if that method also returns "undefined"

Any help here would be appreciated.

stijndepestel commented 7 years ago

Hi,

The documentation for alanning:roles states: "The currently logged-in user's roles field is automatically published to the client."

This is probably the reason why only the currently logged-in user's roles are available. Have you tried publishing all the user accounts server side (including the roles field)?

bmcgonag commented 7 years ago

@stijndepestel is right. You'll need to publish the 'users' collection server side first, then subscribe to it client side, and then you can query for all user's roles.

Additionally, i would limit the publish function to users with an 'Admin' (or your equivalent) role.

If you need some guidance on doing this, there is a great set of tutorials on Youtube by Scott Tolinski for LevelUpTuts for Meteor Roles and Accounts. The Roles part doing what you want is covered in episodes 6, 7, and 8 of that series as I recall.

mitar commented 4 years ago

I think this was answered.