I'm using meteor-roles for permissions, and I'd like to add a particular role to the first user created. I know I can't use addUsersToRoles
in the onCreateUser
hook because it queries the database for the user ID but the user has not been added to the database yet.
I found this answer suggesting wrapping the createUser
method, but that doesn't work for me. The server complains that createUser
doesn't support callbacks yet.
You can on the OnCreateUser
, do something like this.
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;
});
Assuming you have the First-user
role. like this.
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;
});
Remember you should call the onCreateUser
at the top of the createUsers
methods.
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)
From README .
Hope this Works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.