簡體   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