【发布时间】: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.userId 和Meteor.userId 在这条路线上都不起作用
【问题讨论】:
标签: javascript meteor iron-router