我有一个带有一些拒绝更新规则的用户集合:
// 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
选项返回有关更新(或插入或设置或upsert)操作的完整对象。以下是参数:
isSet: true
unset: [Function]
value: true
operator: '$set'
field: [Function]
siblingField: [Function]
isInsert: false
isUpdate: true
isUpsert: false
userId: null
isFromTrustedCode: true
因此,可以对写作权规则进行非常精细的管理。
答案 0 :(得分:1)
根据官方documentation的规定,您可以使用一个简单的选项绕过验证:
要跳过验证,请在致电
validate: false
或insert
时使用update
选项。在客户端(不受信任的代码)上,这将仅跳过客户端验证。在服务器(可信代码)上,它将跳过所有验证。
但是,如果您想要更精细的控制,而不是使用denyUpdate
,则可以使用custom
验证类型,其this
上下文与isFromTrustedCode
在服务器上调用时true
的属性。