2013-12-08 37 views
3

这可以分解为两部分:如何限制Meteor帐户创建给管理员?

1)如何以管理员身份指定帐户?

这是我现在正在进行的,它不起作用。

Meteor.startup(function() { 
    if (Meteor.users.find().count() === 0) { 
     console.log("Adding fake data"); 
     Accounts.createUser({username:"admin", email:"[email protected]", password:"1234", admin: true, profile:{name:"Administrator"}}); 
    } 

用户的“admin”属性不起作用。我不确定把它放在配置文件中是正确的做法...这里的任何建议?

2)我如何限制用户创建只有管理员?

这是我去,它也不起作用

Meteor.users.allow({ 
    insert: function(userId, doc) { 
     // only admin and create 
     return (userId && Meteor.users(userId).admin); 
    }, 

回答

2

退房的authorization大气插件。它处理基于角色的授权,并且有一个限制授权用户创建新用户的例子。

+0

我会仔细研究这个问题并回复你。看起来不错 – Chet

+0

流星角色包也许是有趣的。引用的授权包@Cuberto是流星角色的分支。不同之处在于,授权允许角色和权限分开,而角色同等对待所有内容(基本上只是标签)。所以角色往往更简单一些,而授权允许在角色中嵌套权限。 流星角色 - https://github.com/alanning/meteor-roles – alanning

+0

btw,这里是限制新用户创建的例子:https://github.com/alanning/meteor-roles/blob/master/examples/ mini-pages/server/server.js#L77-L85 – alanning

3

你可以做这样的事情:

服务器端代码:

Meteor.methods({ 
    createUser:function(username, email, password, name) { 
     if(Meteor.user() && Meteor.user().admin === true) { //You'll have to customize this to how you want it 

      return Accounts.createUser({ 
         username: username, 
         email: email, 
         password: password, 
         profile: { 
          name: name 
         } 
        }); 
     }else{ 
      console.log("not logged in or not an admin"); 
     } 
    }, 
    makeMeAdmin: function() { 
     //You can customize this to have a password or something this is just an example 
     Meteor.users.update({_id: this.userId}, {$set:{admin:true}}); 
    } 
}); 

客户端代码:

让自己管理:

Meteor.call("makeMeAdmin"); 

创建一个用户:

Meteor.call("createUser", "username", "[email protected]", "password123", "Bob Bob"); 
+0

我不确定{$ set:{admin:true}}是否有效。 – Chet

+0

它应该工作。虽然您没有发布该字段,但您不会看到它的客户端。它只是作为一个例子来展示如何使用类似的东西作为更新用户的条件。如果你发现它更好,你应该使用你之前做的事 – Akshat

+0

对不起,触摸一个很长的日期答案,但@Akshat会发送密码在纯文本的电线? – Shaded

相关问题