Meteor: How to assign different roles to users during sign up process

I am using the meteor package ian:accounts-ui-bootstrap-3 for accounts and alanning:roles for assigning roles.

On the sign up form I have two options one for Doctor and one for Advisor . I want to assign the selected option as a role to that user. Can someone let me know how to do this?

I have just started learning meteor and don't know much about its flow. I can assign roles to a user if I create the user manually like this:

var adminUser = Meteor.users.findOne({roles:{$in:["admin"]}});

if(!adminUser){

    adminUser = Accounts.createUser({
      email: "mohsin.rafi@mail.com",
      password: "admin",
      profile: { name: "admin" }
    });
    Roles.addUsersToRoles(adminUser, [ROLES.Admin]);
 }

But I want to assign a roll automatically as a user signs up and select one of the options and that option should be assigned as his role.


You shouldn't need a hack for this. Instead of using Accounts.onCreateUser you can do it with the following hook on the Meteor.users collection. Something along the lines of the following should work:

Meteor.users.after.insert(function (userId, doc) {
    if (doc.profile.type === "doctor") {
        Roles.addUsersToRoles(doc._id, [ROLES.Doctor])
    } else if (doc.profile.type === "advisor") {
        Roles.addUsersToRoles(doc._id, [ROLES.Advisor])
    }
});

To get around having to check on login every time it's possible to directly set the roles on the user object instead of using the Roles API.

A hack? Yep, you probably need to make sure the roles have already been added to roles... not sure if there's anything else yet.

if(Meteor.isServer){
  Accounts.onCreateUser(function(options, user){
    if(options.roles){
      _.set(user, 'roles.__global_roles__', ['coach', options.roles]);
    }
    return user;
  });
}

Note: _.set is a lodash method not in underscorejs.

There's no pretty solution because:

  • There's no server side meteor callback post complete account creation.
  • In onCreateUser the user hasn't been added to the collection.
  • Accounts.createUser 's callback is currently only for use on the client. A method could then be used from that callback but it would be insecure to rely on it.
  • The roles package seems to grab the user from the collection and in onCreateUser it's not there yet.

  • you can use the Accounts.onCreateUser hook to manage that.

    Please keep in mind the code below is fairly insecure and you would probably want to do more checking beforehand, otherwise anyone can assign themselves admin. (from docs):

    options may come from an untrusted client so make sure to validate any values you read from it.

    Accounts.onCreateUser(function (options, user) {
      user.profile = options.profile || {};
      if (_.has(options, 'role')) {
        Roles.addUserToRoles(user._id, options.role);
      }
      return user;
    });
    
    链接地址: http://www.djcxy.com/p/85502.html

    上一篇: 在Java中加载类时如何管理内存?

    下一篇: 流星:如何在注册过程中为用户分配不同的角色