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

Get roles for user #137

Closed ErnestClose closed 9 years ago

ErnestClose commented 9 years ago

getRolesForUser only works for GLOBAL_GROUP if the group is not specified. According to documentation the group is optional, but if not specified does not return all roles in all groups.

alanning commented 9 years ago

The group param is optional because some apps do not use groups at all:

Roles.addUsersToRoles(userId, ['view', 'edit'])
Roles.getRolesForUser(userId)  // => ['view', 'edit']

For apps that use groups, the groups are meant to be independent partitions that have no relation to each other. So a user may have 'edit' permission in one group but only 'view' permission in another.

The intent of getRolesForUser is to show the user's roles that would return true when checked with Roles.userIsInRole. This is why the roles in the Global group are included since they are always checked.

If you want to get all the user's roles across all groups, you'll need to iterate over the roles field yourself.

Something like,

var user = Meteor.user(),
    groupName,
    roles = [];

for (groupName in user.roles) {
  // note: doesn't strip duplicates
  roles.concat(user.roles[groupName])
}

console.log(roles);

I'm curious why you'd want to do that though?