MeteorJS useraccounts:核心和流星角色 [英] MeteorJS useraccounts:core and meteor-roles

查看:197
本文介绍了MeteorJS useraccounts:核心和流星角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MeteorJS开发简单的Web应用程序。我是新的MeteorJs。通过alanning核心包和流星的角色:我用useraccounts。难道是更多钞票,同时注册一个新用户角色分配给用户?

修改1

我一直在使用onCreateUser钩尝试,但东西是行不通的。

  Accounts.onCreateUser(功能(选件,用户){
  变种角色= ['未选择];
  Roles.addUsersToRoles(用户,角色);
  返回用户;
});


解决方案

我有这样的方法来创建用户(如果我understond你比如你alredy有一些用户,只需要一些角色,因此与当前用户只创建此) ,还使用一些登录按钮自定义或类似的东西。

Server.js

  Meteor.methods({
     调用createUsers:功能(电子邮件,密码,角色名字){
       VAR用户= [{名:姓名,电子邮件:电子邮件,角色:[角色]},
                   ];
。每个(用户,功能(用户){
   VAR ID;
ID = Accounts.createUser({
     电子邮件:user.email,
     密码:密码,
     简介:{名称:user.name}
     });
 如果(user.roles.length大于0){
          Roles.addUsersToRoles(ID,user.roles);
         }
        });
},
       deleteUser:函数(ID){///一些删除方法(如果需要的不要忽略)
      返回Meteor.users.remove(ID);
      },
    });

发布方法

  //发布角色
      Meteor.publish(NULL,函数(){
       返回Meteor.roles.find({})
       })
Meteor.publish(超级管理员功能(){
        VAR用户= Meteor.users.findOne({_ ID:this.userId});         如果(Roles.userIsInRole(用户,[超级管理员])){
        返回Meteor.users.find({},{字段:{电子邮件:1,概况:1,作用:1}});
         }
   this.stop();
          返回;
       });          Meteor.publish(管理,功能(){
       VAR用户= Meteor.users.findOne({_ ID:this.userId});        如果(Roles.userIsInRole(用户,[管理员])){
        返回Meteor.users.find({},{字段:{电子邮件:1,概况:1,作用:1}});
       }
this.stop();
     返回;
      });
      Meteor.publish(NULL,函数(){
     返回Meteor.roles.find({})
     })

所以客户端客户端/ register.html

 <模板名称=注册>
      <表ID =注册表单行动=行动>
        <输入类型=电子邮件ID =注册的电子邮件占位=农布雷NUEVO Usuario>
        <输入类型=密码ID =注册密码占位=密码>
          <选择ID =注册-ROL级=表格控>
             <期权价值=管理中选择>联系< /选项>
            超级管理员<<选择&GT期权价值=超级管理员/选项>
            <期权价值=正常选择>正常< /选项>
         < /选择>
       <输入类型=提交值=注册>
      < /表及GT;
    < / tempalate>

register.js 调用服务器方法

  Template.registrar.events({
  提交#寄存器的形式':功能(E,T){
    亦即preventDefault();
     VAR电子邮件= t.find('#寄存器的电子邮件')。值,
     密码= t.find('#寄存器密码)。价值,
      角色= $(#寄存器ROL).VAL();
    Meteor.call(调用createUsers,电子邮件,密码,角色);
     返回false;
      },
    点击#deleteUser':函数(事件,模板){
     VAR idUsuario = this._id;
     Meteor.call('deleteUser',{_ ID:idUsuario})
     }
    });

删除部分(HTML)这是可选的太看,如果帐户创建正确

  {{#每一个用户}}
      <李ID =用户>< H6> {{EMAIL}}< / H6>< H6> {{角色}}< / H6>< /李>
        <按钮ID =deleteUser级=BTN BTN-危险BTN-XS> Borrar Usuario {{EMAIL}}< /按钮>
     {{/每}}

客户端/ registerList.js

  Template.registrar.helpers({
         用户:函数(){
         返回Meteor.users.find();
       },
        电子邮件:功能(){
        返回this.emails [0]。地址;
        },
       作用:功能(){
       如果(!this.roles)返回'<没有>';
    返回this.roles.join(,);
      }
      });

记得订阅

  Meteor.subscribe(管理员);
  Meteor.subscribe(超级管理员);

希望这有助于为凌乱code对不起

I am using MeteorJS to develop simple web app. I am new at MeteorJs. I use useraccounts:core package and meteor-roles by alanning. Is it posible to assign role to user while registering a new user ?

EDIT 1

I have tried using onCreateUser hook but something is not working.

Accounts.onCreateUser(function(options, user){
  var role = ['unselected'];
  Roles.addUsersToRoles(user, role);
  return user;
});

解决方案

I have this way to create users (if i understond you example you alredy have some users, just need some roles, so with the current user just create this), also use some Login buttons customized or something like that

Server.js

Meteor.methods({
     createUsers: function(email,password,roles,name){
       var users = [{name:name,email:email,roles:[roles]},
                   ];
.each(users, function (user) {
   var id;
id = Accounts.createUser({
     email: user.email,
     password: password,
     profile: { name: user.name }
     });
 if (user.roles.length > 0) {
          Roles.addUsersToRoles(id, user.roles);
         }
        });
},
       deleteUser : function(id){       ///Some Delete Method (ignore if dont needed)
      return Meteor.users.remove(id);
      },
    });

Publish Methods

//publish roles
      Meteor.publish(null, function (){ 
       return Meteor.roles.find({})
       })
Meteor.publish("Super-Admin", function () {
        var user = Meteor.users.findOne({_id:this.userId});

         if (Roles.userIsInRole(user, ["Super-Admin"])) {
        return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
         } 
   this.stop();
          return;
       });

          Meteor.publish("Admin", function () {
       var user = Meteor.users.findOne({_id:this.userId});

        if (Roles.userIsInRole(user, ["Admin"])) {
        return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
       } 
this.stop();
     return;
      });
      Meteor.publish(null, function (){ 
     return Meteor.roles.find({})
     })

So on the Client side client/register.html

<template name="register">
      <form id="register-form" action="action" >
        <input type="email" id="register-email" placeholder="Nombre Nuevo Usuario">
        <input type="password" id="register-password" placeholder="Password">        
          <select id="register-rol" class="form-control">
             <option value="Admin" selected>Admin</option>
            <option value="Super-Admin" selected>Super Admin</option>
            <option value="Normal" selected>Normal</option>
         </select>
       <input type="submit" value="Register">
      </form>
    </tempalate>

and on register.js call the server methods

 Template.registrar.events({
  'submit #register-form' : function(e, t) {
    e.preventDefault();    
     var email = t.find('#register-email').value,
     password = t.find('#register-password').value,
      roles = $( "#register-rol" ).val();
    Meteor.call("createUsers", email, password,roles);
     return false;
      },
    'click #deleteUser' : function(event,template){
     var idUsuario= this._id;
     Meteor.call('deleteUser',{_id:idUsuario})
     }
    });

Delete Part(html) this is optional just too look if accounts are creating correctly

{{#each users}}
      <li id="user"><h6>{{email}}</h6><h6>{{roles}}</h6></li>
        <button id="deleteUser" class="btn btn-danger btn-xs" > Borrar Usuario        {{email}}       </button>
     {{/each}}

client/registerList.js

   Template.registrar.helpers({
         users: function () {
         return Meteor.users.find();
       },
        email: function () {
        return this.emails[0].address;
        },
       roles: function () {
       if (!this.roles) return '<none>';
    return this.roles.join(',');
      }
      });

Remember Subscribe

  Meteor.subscribe('Admin');
  Meteor.subscribe('Super-Admin');

Hope this help sorry for the messy code

这篇关于MeteorJS useraccounts:核心和流星角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆