繁体   English   中英

Meteor JS(铁路由器)-限制对服务器路由的访问

[英]Meteor JS (Iron Router) - Restricting access to server routes

我想限制访问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挂钩,如下所示

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都不在路由上

对于服务器端钩子,我非常确定您需要onBeforeAction来像处理路由那样具有{where:“ server”}部分。

另外,我认为iron:router从未在其路由上实现服务器端用户身份验证。 您可能需要使用更大的功能(例如mhagmajer:server-router)访问服务器路由周围的程序包,该功能可以访问经过身份验证的路由。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM