我即将向我的学生打开一个 Meteor Web 应用程序,我显然需要将用户集合中的数据保密(每个学生都应该有权访问自己的"结果"(。但"管理员"角色需要具有更多访问权限。以下是发布信息(/server/main.js(:
Meteor.publish('userData', function() {
if (Roles.userIsInRole(this.userId, ['admin'])) {
return Meteor.users.find({}, {fields: {
_id: 1,
createdAt: 1,
username: 1,
emails: 1,
profile: 1,
lastAccess: 1,
roles: 1
}}
)};
if (this.userId) {
return Meteor.users.find({_id: this.userId}, {fields: {
results: 1
}
});
} else {
this.ready();
}
});
我的问题是:此发布设置是否足够安全,可以抵御恶意用户?
简短回答:是的。
请记住,无需发布任何集合即可检索已登录用户的数据。在客户端,您可以在没有订阅的情况下使用Meteor.user()
。
因此,仅当您想要显示有关已登录用户以外的用户的信息时,才需要订阅userData
。
另外,请记住,当您返回 Mongo 光标时,您不必调用 this.ready()
。
因此,您的出版物将变为:
Meteor.publish('userData', function() {
if (Roles.userIsInRole(this.userId, ['admin'])) {
return Meteor.users.find({}, { fields: {
_id: 1,
createdAt: 1,
username: 1,
emails: 1,
profile: 1,
lastAccess: 1,
roles: 1
}});
}
});