meteor - 允许更新特定集合属性

标签 meteor

我正在尝试向集合添加“评级”属性,并希望使任何用户(不仅仅是所有者)能够向集合中设置的评级添加评级。我的问题是我设置了允许/拒绝规则,以便只有所有者才能对他们拥有的集合执行更新。有没有办法允许任何用户仅在更新特定属性(“评级”集)时才更新集合,并在他们尝试更新任何其他属性时拒绝他们更新访问权限。

我在服务器上的允许/拒绝规则如下...

Playlists.allow({
  insert: function(userId, doc) {
    return (userId && doc.owner === userId);
  },
  update: function (userId, docs, fields, modifier) {
    return _.all(docs, function(doc) {
      return doc.owner === userId;
    });
  },
  remove: function (userId, docs) {
    return _.all(docs, function(doc) {
      return doc.owner === userId;
    });
  }
});

Playlists.deny({
  update: function (userId, docs, fields, modifier) {
    return _.contains(fields, 'owner');
  },
  remove: function (userId, docs) {
    return _.any(docs, function (doc) {
      return doc.locked;
    });
  },
  fetch: ['locked']
});

最佳答案

Playlists.deny.update ,您可以更改逻辑,以便它首先检查是否有人试图修改 ratings 属性(例如使用 $addToSet )和 return false如果是这样的话。所以你最终会得到这样的代码:

 Playlists.deny({
    update: function(userId, docs, fields, modifier) {
      if (fields.ratings && modifier["$addToSet"] && modifier["$addToSet"].ratings) {
        return false; // don't deny this
      }
      else {
        return _.contains(fields, 'owner');
      }
    }
  });

关于meteor - 允许更新特定集合属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15469216/

相关文章:

javascript - 在 Meteor 中缩短收集字段以进入

meteor - 在服务器运行时使用 tinytest 测试 Meteor 客户端

javascript - Meteor:在所有浏览器上更新

testing - 如何测试服务器端 debugOnly 包

javascript - 父作用域中函数的参数

jquery - Meteor:将 jQuery ajax 帖子从 NodeJs 发送到 php

javascript - 显示来自 Meteor 1.0 的数据

javascript - Meteor因ID错误删除文档

javascript - 如何将 Ionic 输入类型文件设置为 Button

javascript - meteor AngularJS "Node.replaceChild does not implement interface Node"错误