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

橙三吉。 提交于 2019-12-19 02:03:50

问题


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.


回答1:


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])
    }
});



回答2:


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:

  1. 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.
  2. The roles package seems to grab the user from the collection and in onCreateUser it's not there yet.



回答3:


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;
});



回答4:


Thanks for your response. I tried but it doesn't work for me.

I used Accounts.onLogin hook to to manage this. Below code works for me:

Accounts.onLogin(function (info) {
    var user = info.user;
    if(user.profile.type === "doctor"){
      Roles.addUsersToRoles(user, [ROLES.Doctor])
    }
    else 
      if(user.profile.type === "advisor"){
        Roles.addUsersToRoles(user, [ROLES.Advisor])
        }
    return user;
  });


来源:https://stackoverflow.com/questions/30482814/meteor-how-to-assign-different-roles-to-users-during-sign-up-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!