【发布时间】:2015-02-23 13:53:39
【问题描述】:
我正在使用Alanning Roles 为我的应用程序的用户维护一组组/角色。当用户创建“应用程序”时,我为他们生成一个新角色app_name + UUID,然后将其作为具有Admin 角色的组添加到创建它的用户。然后我可以使用生成的组名加上Admin 或Viewer 角色的组合来确定用户有权查看和/或编辑哪个Applications。
我遇到的问题是我想不出一个好方法来让出版物只发布用户应该看到的东西。我知道,至少在默认情况下,发布不像客户端那样“反应性”,它们只对返回的游标是反应性的。但是,在我的代码中,我首先创建了组/角色,将其添加到用户,然后保存“应用程序”,我认为它会重新运行我的出版物,但它没有:
Meteor.publish('myApplications', function(groups) {
if (this.userId) {
console.log('Running myApplications publication...');
console.log('Found roles for user ' + this.userId + ': ', Roles.getGroupsForUser(this.userId));
return Applications.find({group: {$in: Roles.getGroupsForUser(this.userId)}});
} else {
//console.log("Skipping null user");
return null;
}
});
但是,与我想象的相反(整个发布方法将重新运行),我猜真正发生的是只有光标是更新的。因此,对于我的下一次尝试,我添加了 mrt:reactive-publications 包,并简单地将光标移至用户的 Meteor.users 集合,认为这会“触发”当用户使用新的更新时重新运行发布组/角色,但这不起作用。
我最终通过简单地为用户传递组来工作:
Meteor.publish('myApplications', function(groups) {
if (this.userId) {
if (!groups || groups.length === 0) {
groups = Roles.getGroupsForUser(this.userId);
}
console.log('Running myApplications publication...');
console.log('Found roles for user ' + this.userId + ': ', Roles.getGroupsForUser(this.userId));
return Applications.find({group: {$in: groups}});
} else {
//console.log("Skipping null user");
return null;
}
});
然后我只是在我的路由的waitOn 中调用像Meteor.subscribe('myApplications', Roles.getGroupsForUser(Meteor.userId())) 这样的出版物,但这意味着任何客户都可以调用相同的出版物并传入他们喜欢的任何组,并且可能会看到他们不打算看到的文档看。这似乎是一个相当大的安全漏洞。
有没有更好的方法来实现这一点,这样客户就无法哄骗他们看到不属于他们的东西?我认为唯一真正的方法是在发布方面聚集小组,但这会破坏反应。
【问题讨论】:
标签: meteor meteor-publications meteor-collections