【问题标题】:How to restrict Meteor account creation to an Administrator?如何将 Meteor 帐户创建限制为管理员?
【发布时间】:2013-12-25 06:51:53
【问题描述】:

这可以分为两部分:

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

【问题讨论】:

    标签: meteor meteorite


    【解决方案1】:

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

    【讨论】:

    • meteor-roles 包也可能很有趣。 @Cuberto 引用的授权包是流星角色的一个分支。不同之处在于授权允许拆分角色和权限,而角色平等对待一切(基本上只是标签)。因此,角色往往更简单,而授权允许在角色中嵌套权限。流星角色 - github.com/alanning/meteor-roles
    • 顺便说一句,这是限制新用户创建的示例:github.com/alanning/meteor-roles/blob/master/examples/…
    【解决方案2】:

    你可以这样做:

    服务器端代码:

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

    【讨论】:

    • 它应该可以工作。您不会在客户端看到它,因为该字段未发布。它只是作为一个例子来展示如何使用类似的东西作为更新用户的条件。如果你发现它更好,你应该使用你以前做的事情
    • 很抱歉触及一个过时的答案,但@Akshat 这会以纯文本形式通过网络发送密码吗?
    • @Shaded 是的,但如果你使用 https/wss 就不会
    猜你喜欢
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多