是否可以确定服务器是否调用了Meteor方法

Is it possible to determine if a Meteor method was called by the server

本文关键字:是否 Meteor 方法 调用 服务器      更新时间:2023-09-26

我有一些Meteor方法,我想保护它们的安全,这样只有特定的用户才能从客户端调用它们。然而,服务器也使用这些方法。我收到了这个.userid中的userid,这样我就可以检查用户是否登录,以及是否允许他们拨打电话,没问题。但是,当我还需要从服务器端调用该方法时,我如何确定它是一个服务器调用,以便允许该方法执行。检查是否没有this.userid以确定其服务器调用是否允许未经身份验证的用户也调用该方法。我正在寻找一种方法来确定该方法是否由服务器调用,这样我就可以允许它,并且仍然可以防止未经身份验证的用户调用该方法。

Meteor.methods({
  makeCoffee: function (time) {
    check(time, Number);
    if(calledByServer || (Meteor.user() && Meteor.user().profile.usertype === 'coffee dude')){
          //Makin' Coffee
    }
    else
      throw new Meteor.Error(404, "Can't find my pants");
    return "Coffee will be made at " + time;
  }

this.connection将是服务器端方法内的null,如果该方法不是从客户端调用的

请参阅this.connection文档。

看起来Meteor.call现在也可以从服务器端调用:http://docs.meteor.com/#meteor_call

原始答案:

这样做:

makeCoffee = function (time) { //code here }
Meteor.methods({
  makeCoffeeMethod: function (time) {
    if (calledByAllowedUser())
      return makeCoffee(time);
    else
      throw new Meteor.Error(403, 'Forbidden');
  }
});

现在您可以绕过身份验证在服务器上调用它。