我正在使用meteor-roles对于权限,我想向创建的第一个用户添加特定 Angular 色。我知道我无法在 onCreateUser
Hook 中使用 addUsersToRoles
,因为它会在数据库中查询用户 ID,但用户尚未添加到数据库中。
我找到了this answer建议包装 createUser
方法,但这对我不起作用。服务器提示 createUser
尚不支持回调。
最佳答案
您可以在OnCreateUser
上执行类似的操作。
Accounts.onCreateUser(function(options, user) {
//if there is not users on the database
//we assign the First-User role
if(Meteor.users.find().count() === 0){
user.role = "First-User"
}else{
user.role = "normalUser"
}
return user;
});
假设您具有First-user
Angular 色。像这样。
Meteor.publish("First-User", function () {
var user = Meteor.users.findOne({_id:this.userId});
if (Roles.userIsInRole(user, ["First-User"])) {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
}
this.stop();
return;
});
请记住,您应该调用 createUsers
方法顶部的 onCreateUser
。
Note that the Roles.addUsersToRoles call needs to come after Accounts.createUser or Accounts.onCreate or else the roles package won't be able to find the user record (since it hasn't been created yet)
来自README .
希望这能起作用。
关于javascript - 如何为 Meteor 应用程序中的第一个用户分配特定角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28641434/