简体   繁体   English

流星角色:不能混合使用分组角色和非分组角色

[英]Meteor roles: Can't mix grouped and non-grouped roles

I'm trying to update roles for users, but everytime I want to give/remove the admin role (a global role) it tells me I can't mix them. 我正在尝试为用户更新角色,但是每当我要授予/删除管理员角色(全局角色)时,它都告诉我不能混合使用。 I dont know how to solve this issue. 我不知道如何解决这个问题。

Meteor.users.update(id, {
    $set: {
        'username': username,
        'emails.0.address': email
    }
});

if(Roles.userIsInRole(id, ['mod'])) {
    Roles.removeUsersFromRoles(id, 'mod');
}

if(Roles.userIsInRole(id, ['admin'])) {
    Roles.removeUsersFromRoles(id, 'admin');
}

if(role == 'Moderator') {
    Roles.addUsersToRoles(id, 'mod');
} else if(role == 'Administrator') {
    Roles.addUsersToRoles(id, 'admin');
}

How can this be fixed? 如何解决?

tl;dr tl; dr

You must assign roles to the global group like so: 您必须像这样将角色分配给全局组:

Roles.addUsersToRoles(id, 'admin', Roles.GLOBAL_GROUP);

Detail: 详情:

That error occurs when a user has been assigned roles with a group and without a group. 当为用户分配了具有组和不具有组的角色时,将发生该错误。 As the docs say: 正如文档所说:

NOTE: If you use groups for ANY of your users, you should use groups for ALL of your users. 注意:如果您对任何用户使用组,则应对所有用户使用组。 This is due to how the roles package stores the roles internally in the database. 这是由于角色包如何将角色内部存储在数据库中。

Specifically (from the docs): 具体来说(来自文档):

Roles.addUsersToRoles(bobsUserId, ['manage-team','schedule-game'])
// internal representation - no groups
// user.roles = ['manage-team','schedule-game']

Roles.addUsersToRoles(joesUserId, ['manage-team','schedule-game'], 'manchester-united.com')
Roles.addUsersToRoles(joesUserId, ['player','goalie'], 'real-madrid.com')
// internal representation - groups
// NOTE: MongoDB uses periods to represent hierarchy so periods in group names
//   are converted to underscores.
//
// user.roles = {
//   'manchester-united_com': ['manage-team','schedule-game'],
//   'real-madrid_com': ['player','goalie']
// }

Assigning a group to a user, converts the roles property from an array to an object, future changes then must specify a group. 将组分配给用户,将角色属性从数组转换为对象,以后的更改必须指定一个组。

To assign global roles you must specify the global group like so: 要分配全局角色,您必须像这样指定全局组:

Roles.addUsersToRoles(id, 'admin', Roles.GLOBAL_GROUP);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM