【问题标题】:meteor allow rules流星允许规则
【发布时间】:2013-01-24 08:50:58
【问题描述】:

我有一个关于流星派对示例的问题。

如果我调用此代码:

Parties.allow({
insert: function () {
    return true;
},

remove: function (){
    return true;    
},

update: function() {
    return true;    
}

});

每个人都可以插入、删除和更新。 示例中的代码是

Parties.allow({
 insert: function (userId, party) {
    return false; // no cowboy inserts -- use createPage method
 },
 update: function (userId, parties, fields, modifier) {
    return _.all(parties, function (party) {
    if (userId !== party.owner)
       return false; // not the owner

  var allowed = ["title", "description", "x", "y"];
  if (_.difference(fields, allowed).length)
    return false; // tried to write to forbidden field

  // A good improvement would be to validate the type of the new
  // value of the field (and if a string, the length.) In the
  // future Meteor will have a schema system to makes that easier.
     return true;
   });
 },
 remove: function (userId, parties) {
   return ! _.any(parties, function (party) {
     // deny if not the owner, or if other people are going
     return party.owner !== userId || attending(party) > 0;
   });
 }
});

所以我的问题是例如变量 useriD 和 party 在这一行的位置

 insert: function (userId, party) {

有定义吗? 这些是我在方法中调用的变量吗

 Meteor.call("createParty", variable1, variable2)

?但这没有意义,因为客户端调用

 Meteor.call('createParty', {
    title: title,
    description: description,
    x: coords.x,
    y: coords.y,
    public: public
  }

我希望有人可以向我解释允许功能吗?谢谢!

【问题讨论】:

    标签: meteor rules


    【解决方案1】:

    要了解允许/拒绝,您需要了解userIddoc 参数的来源。 (就像在任何函数定义中一样,实际的参数名称并不重要。)只看当事人插入示例:

    Parties.allow({
    
      insert: function (userId, party) {
         return false; // no cowboy inserts -- use createPage method
      }
    
    });
    

    party 参数是要插入的文档:

    Parties.insert(doc);
    

    userId 参数会在您使用 Meteor Accounts 身份验证系统时自动设置。否则,您必须在服务器上自行设置。你是怎么做到的?

    一般来说,您使用Meteor.call() 从客户端调用服务器上的代码。由于没有内置 API 来设置 userId(除了 Accounts),您必须自己编写(在您的服务器代码中):

    Meteor.methods({
    
       setUserId: function(userId) {
           this.setUserId(userId);
       }
    
    });
    

    然后您可以在客户端代码中的任何位置这样调用它:

    Meteor.call('setUserId', userId);
    

    【讨论】:

    • 你能详细说明一下“牛仔插页”吗?你是说一个人永远不应该允许在收藏中插入毛毯?
    • 完全没有——这取决于您应用的业务逻辑。那只是从问题中复制/粘贴。
    • @PhilMitchell 我正要问 op 的问题,直到我找到你的好答案。我正在关注 Discover Meteor,他们展示了很多代码,但很少解释它的正确功能,在示例中,他们使用 userID 定义了插入函数而没有解释它,然后传递了 doc 而没有解释它。这非常有帮助。
    【解决方案2】:

    1) 变量 useriD 和 party 是在哪里定义的?无处!目的是没有用户可以调用此函数。

    这是为了保护数据库免受可能使用控制台手动插入新方的用户的影响。请记住,Meteor 在客户端和服务器中复制数据库。

    任何用户都可以通过控制台手动插入新方。这可以。但是服务器会拒绝插入,因为它是不允许的。

    2) 这些是我在方法Meteor.call("createParty", variable1, variable2) 中调用的变量吗?是的,变量是可用的,但是这段代码没有使用正确的定义,即:

     Meteor.methods({
        createParty: function (options) {
    

    然后它被用作

     Meteor.call('createParty', 
                { title: title, public: public, ... }, // options array!!
                function (error, party) { ... }  // function executed after the call
     );
    

    对你有帮助吗?

    【讨论】:

    • 感谢您的回答!它有所帮助,但还有一些问题:所以如果我正在编写 Party.allow({ insert: function (userId, party) { return false; }});没有人可以通过控制台创建新的派对。但我不明白 userId 和 party 在这里代表什么?我也可以在没有这些变量的情况下编写相同的行,不是吗?所以没有客户端可以通过控制台写一个新的聚会,但是如果客户端调用方法“createParty”它可以吗?
    • 我同意这两个变量令人不安。我知道根本不允许插入。您应该尝试一些测试并检查会发生什么。 createParty 在插入派对之前会进行一些检查,所以如果他们从控制台使用此功能是可以的,因为这些检查将完成。
    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多