meteor - 发布用户所在组的项目(规划角色和发布)

标签 meteor meteor-publications meteor-collections

我正在使用Alanning Roles为我的应用程序的用户维护一组组/角色。当用户创建“应用程序”时,我为他们生成一个新角色 app_name + UUID,然后将其作为具有 Admin 角色的组添加到用户创造了它。然后,我可以使用生成的组名称加上 AdminViewer 角色的组合来确定用户有权查看和查看哪些Applications/或编辑。

我遇到的问题是我找不到一个好方法来让出版物只发布用户应该看到的内容。我知道,至少在默认情况下,发布并不像客户端那样“响应式(Reactive)”,并且它们仅对它们返回的游标使用react。但是,在我的代码中,我首先创建组/角色,将其添加到用户,然后保存“应用程序”,我认为这会重新运行我的出版物,但事实并非如此:

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;
  }
});

然后我只是在我的路线的 waitOn 中调用像 Meteor.subscribe('myApplications', Roles.getGroupsForUser(Meteor.userId())) 这样的发布,但是这意味着任何客户都可以调用同一出版物并传入他们喜欢的任何组,并且可能会看到他们不希望看到的文档。这似乎是一个相当大的安全漏洞。

是否有更好的方法来实现这一点,以便客户无法哄骗他们看到不属于他们的东西?我认为唯一真正的方法是聚集出版方的团体,但这会破坏 react 性。

最佳答案

在筛选了一堆文档和一些非常有用的堆栈帖子之后,这是我想出的替代方案。效果就像一个魅力!

我的目标是将“ guest ”用户的信息发布给群组管理员,以供批准/拒绝增强权限。

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?

关于meteor - 发布用户所在组的项目(规划角色和发布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28675356/

相关文章:

javascript - 从html点击事件中提取数据

meteor - 如何动态呈现模板内容

javascript - 为什么我的助手集合查询没有反射(reflect)在 html 模板中?

javascript - 我的 meteor 插入无法与点击事件一起工作,但没有错误可供我调试

Meteor Publish-Composite 嵌套问题

meteor linkedin帐户,redirect_uri错误

meteor - Meteor.js 模板 block 中的“或”条件

Meteor.publish 不是一个函数

javascript - 使用发布为订阅创建进度条

javascript - 如何验证meteor helper中的查询结果并重定向到404?