如何将Meteor帐户创建限制为管理员?

时间:2013-12-08 02:03:54

标签: meteor meteorite

这可以分为两部分:

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

这就是我现在所做的事情,它不起作用。

Meteor.startup(function () {
    if (Meteor.users.find().count() === 0) {
        console.log("Adding fake data");
        Accounts.createUser({username:"admin", email:"admin@admin.com", 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 个答案:

答案 0 :(得分: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@email.com", "password123", "Bob Bob");

答案 1 :(得分:2)

查看authorization Atmosphere插件。它处理基于角色的授权,并有一个限制新用户创建给授权用户的示例。

相关问题