【问题标题】:Meteor collection2 deny rules : grant full permissions to the serverMeteor collection2 拒绝规则:授予服务器完全权限
【发布时间】:2014-10-04 14:44:14
【问题描述】:

我有一个包含一些拒绝更新规则的用户集合:

// The roles object
Schema.roles = new SimpleSchema({
    maker: {
        type: Boolean,
        denyUpdate: true
    },
    admin: {
        type: Boolean,
        denyUpdate: true
    }
});

这些数据在用户配置文件中。显然,我不希望随机用户能够修改profile.roles.admin。但是管理员用户应该可以。

它部分工作:用户不能修改这个布尔值。但是应该可以从下面的服务器端代码修改它。

Meteor.users.update({_id: targetID'}, {$set: {'profile.roles.admin': true}});

有没有办法告诉collection2 信任来自服务器的代码?


编辑:答案


感谢下面的回答,这是我现在用于架构的代码:

admin: {
    type: Boolean,
    autoValue: function() {
        // If the code is not from the server (isFromTrustedCode)
        // unset the update
        if(!this.isFromTrustedCode)
            this.unset();
    }
}

isFromTrustedCode 布尔值告诉代码是否应该被信任。简单的。顺便说一句,autoValue 选项返回有关更新(或插入、设置或更新)操作的完整对象。以下是参数:

isSet: true
unset: [Function]
value: true
operator: '$set'
field: [Function]
siblingField: [Function]
isInsert: false
isUpdate: true
isUpsert: false
userId: null
isFromTrustedCode: true

因此可以对写作权限规则进行真正细粒度的管理。

【问题讨论】:

    标签: collections meteor meteor-collection2


    【解决方案1】:

    正如官方documentation所提供的,您可以使用一个简单的选项绕过验证:

    要跳过验证,请在调用 insertupdate 时使用 validate: false 选项。在客户端(不受信任的代码)上,这将仅跳过客户端验证。在服务器上(可信代码),它将跳过所有验证。

    但是如果您想要更细粒度的控制,而不是使用denyUpdate,您可以使用custom 验证类型,该类型具有this 上下文和isFromTrustedCode 属性,在调用时为true在服务器上。

    【讨论】:

    • 我想要细粒度的控制。 autovalue 选项的语法似乎很复杂。但这似乎是我需要的。谢谢。
    • 欢迎您。 Autovalue 也很棒,但您会发现 custom validation 更加强大和通用。
    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2021-10-20
    • 2016-03-16
    • 2011-10-17
    • 2020-03-22
    • 2016-11-27
    • 1970-01-01
    相关资源
    最近更新 更多