我的 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
钩子(Hook)没有任何效果
此外,我注意到 this.userId
和 Meteor.userId
在路线上都不起作用
最佳答案
对于服务器端钩子(Hook),我非常确定您需要 onBeforeAction 来拥有 { where: "server"} 部分,就像您为路线所做的那样。
此外,我认为 Iron:router 从未在其路由上实现过服务器端用户身份验证。您可能想检查一个具有更大功能的服务器路由包,例如可以访问经过身份验证的路由的 mhagmajer:server-router。
关于javascript - Meteor JS (Iron Router) - 限制对服务器路由的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46647998/