【问题标题】:Publication of items where User is in group (Alanning Roles and Publications)发布用户所在组的项目(Alanning Roles and Publications)
【发布时间】:2015-02-23 13:53:39
【问题描述】:

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


    【解决方案1】:

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

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

    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?

    【讨论】:

      猜你喜欢
      • 2015-03-11
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      相关资源
      最近更新 更多