【问题标题】:Meteor JS (Iron Router) - Restricting access to server routesMeteor JS (Iron Router) - 限制对服务器路由的访问
【发布时间】:2018-03-20 18:06:38
【问题描述】:

我的 MeteorJs 应用程序中有一个下载路径,我想限制访问。路由代码如下

Router.route("/download-data",  function() {
var data = Meteor.users.find({ "profile.user_type": "employee" }).fetch();
var fields = [...fields];

var title = "Employee - Users";

var file = Excel.export(title, fields, data);

var headers = {
  "Content-type": "application/vnd.openxmlformats",
  "Content-Disposition": "attachment; filename=" + title + ".xlsx"
};

 this.response.writeHead(200, headers);
 this.response.end(file, "binary");
 },
 { where: "server" }
);

路由会自动下载文件。这目前正在工作,但我想限制对路线的访问。我只希望管理员能够下载它。

我创建了一个onBeforeAction Hook,如下所示

Router.onBeforeAction(
  function() {
    //using alanning:roles
    if(Roles.userIsInRole(this.userId, "admin"){
     console.log('message') //testing
   }
  },
  {
    only: ["downloadData"]
  }
);

并将我的路线重命名如下

//code above
this.response.writeHead(200, headers);
 this.response.end(file, "binary");
 },
 { where: "server", name: "downloadData" }
);

onBeforeAcion 钩子不起作用

我还注意到this.userIdMeteor.userId 在这条路线上都不起作用

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    对于服务器端的钩子,我很确定你需要 onBeforeAction 来像你的路由一样拥有 { where: "server" } 部分。

    另外,我认为 Iron:router 从未在其路由上实施服务器端用户身份验证。您可能需要检查具有更大功能的服务器路由包,例如可以访问经过身份验证的路由的 mhagmajer:server-router。

    https://github.com/mhagmajer/server-router

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2014-03-27
      相关资源
      最近更新 更多