就像已经推荐过的人一样,角色是管理用户权限的最佳方式。组权限只是查看用户角色的另一种方式。
的人有些复制粘贴代码谁上台希望通过
if Roles.userIsInRole(userId, [
'llama'
'admin'
], Roles.GLOBAL_GROUP)
// Do something with privileges
else
// No privileges
安全发布到用户只能在管理员的角色:
if (Roles.userIsInRole(this.userId, ['admin'], Roles.GLOBAL_GROUP)) {
return SomeCollection.find({}); // Publish all fields
} else {
return SomeCollection.find({}, {fields: { // Only return public fields
'name': true
}
}
}
限制角色集合自身仅用户管理员角色公布:
Meteor.publish("roles", function() {
if (Roles.userIsInRole(this.userId, 'admin', Roles.GLOBAL_GROUP)) {
return Meteor.roles.find({});
} else {
this.ready();
}
});
向/从角色添加/删除用户:
Meteor.methods({
// add user to role. Roles can be an array
addRoleToUser: function(userId, roles) {
if(Roles.userIsInRole(this.userId, 'admin', Roles.GLOBAL_GROUP)) {
Roles.addUsersToRoles(this.userId, roles, Roles.GLOBAL_GROUP);
},
// Remove user from a role:
removeRoleFromUser: function(userId, roles) {
if(Roles.userIsInRole(this.userId, 'admin', Roles.GLOBAL_GROUP)) {
Roles.removeUsersFromRoles(userId, roles, Roles.GLOBAL_GROUP);
}
}
}
这很不寻常,为什么不直接使用权限/角色呢? https://atmospherejs.com/alanning/roles –
谢谢,这看起来很有趣,我一定会更多地关注它。但是,让用户登录两次并不是那么容易?他们再次登录(并验证)到一个特定的组(也可以创建一个新组)。在该帐户中,他们可以用他们的凭证登录/注册。这有点奇怪,但至少在理论上实现起来似乎很简单。我只需要帮助弄清楚如何... –
但是,为什么不只是有'组'作为角色,那么你只有一个登录,并且用户有一个他们可以交互的'组'的列表。只需检查用户是否登录,然后检查他们是否属于该“组”的一部分。简单。 –