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



我的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:可以访问已验证路由的服务器路由器。

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

最新更新