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

How to remove a role group? #162

Closed spgandhi closed 8 years ago

spgandhi commented 8 years ago

Hi,

I have an meteor app which has many groups. Now a user can have different roles in a each group. So I am stroring this permissions as Roles.addUsersToRoles(userId, [permissions], group_name). Here the group_name is the group id generated by mongo db.

Now when I delete the group I want to remove the whole permission group from the users collection. What I am currently using is Roles.setUserRoles(user_id, [], group_name). But this only empties the group_name array in the user collection and does not remove the whole array. I want to be able to remove the whole group permission array when the group is deleted.

Is there a function or a way around to get this done?

Thanks.

alanning commented 8 years ago

Hi @spgandhi, with the current version of roles you can reference the group directly in MongoDB like this:

// Database record:
// user.roles = {
//   'manchester-united_com': ['manage-team', 'schedule-game'],
//   'real-madrid_com': ['player', 'goalie']
// }

var groupId = "real-madrid.com".replace(/\./g, '_');  // => "real-madrid_com"
var update = {$unset: {}};
update.$unset[groupId] = 1;

// update = {
//   $unset: {
//    "real-madrid_com": 1
//   }
// }

db.users.update({_id: userId}, update);

Note that you'll have to convert any periods ('.') in the groupId into an underscore since that's what Roles does internally to avoid the MongoDB restriction/conflict.

Version 2 of roles will probably have a built-in way to do this.