发布用户在组中的项目(Alanning角色和发布)
我使用Alanning Roles为我的应用程序的用户维护一组组/角色。当用户创建一个“应用程序”时,我为它们生成一个新角色app_name + UUID
,然后将其作为一个角色为Admin
的组添加到创建它的用户。然后,我可以使用生成的组名称加上Admin
或Viewer
角色的组合来确定用户有权查看和/或编辑哪个Applications
。发布用户在组中的项目(Alanning角色和发布)
我遇到的问题是我找不到一个好方法让出版物只发布用户应该看到的东西。我知道,至少在默认情况下,出版物并不像客户那样是“被动的”,而且他们只对被返回的游标产生反应。但是,在我的代码我首先要创建组/角色,把它添加到用户,然后保存“应用程序”,而且我认为他会重新运行我的出版物,但它没有:
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;
}
});
但是,违背我认为会发生什么(整个发布方法会重新运行),我猜测真正发生的是只有Cursor是更新的。因此,对于我的下一次尝试,我添加了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;
}
});
然后我就打电话一样Meteor.subscribe('myApplications', Roles.getGroupsForUser(Meteor.userId()))
发表在我的路线的waitOn
,但是这将意味着,任何客户端可以调用相同的出版物并通过他们喜欢的任何群体,并可能看到他们不打算看到的文件。这似乎是一个相当大的安全漏洞。
有没有更好的方式来实现这样的客户端将无法哄骗他们的方式看到的东西不是他们的?我认为唯一真正的方法是在出版物方面收集这些小组,但是这会打破反应性。
在筛选了一堆文档和一些非常有用的帖子后,这是我提出的替代方案。奇迹般有效!
我的目标是向组管理员发布“访客”用户的信息,以批准/拒绝增强的权限。
Meteor.publish('groupAdmin', function(groupId) {
// only publish guest users info to group admins
if(Roles.userIsInRole(this.userId, ['group-admin'], groupId)) {
// I can't explain it but it works!
var obj = {key: {$in: ['guest']}};
var query = {};
var key = ('roles.' + groupId);
query[key] = {$in: ['guest']};
return Meteor.users.find(query, {
fields: {
createdAt: 1,
profile: 1
}
});
} else {
this.stop();
return;
}
});
参考:How to set mongo field from variable & How do I use a variable as a field name in a Mongo query in Meteor?