2017-10-09 3 views
0

アクセスを制限したいMeteorJsアプリケーションにダウンロードルートがあります。ルートコードは次のとおりです。Meteor JS(Iron Router) - サーバールートへのアクセスを制限する

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" } 
); 

ルートは自動的にファイルをダウンロードします。これは現在作業中ですが、私はルートへのアクセスを制限したいと思います。私は管理者がダウンロードできるようにしたい。

私は

Router.onBeforeAction(
    function() { 
    //using alanning:roles 
    if(Roles.userIsInRole(this.userId, "admin"){ 
    console.log('message') //testing 
    } 
    }, 
    { 
    only: ["downloadData"] 
    } 
); 

以下のようにonBeforeActionフックを作成し、onBeforeAcionフックは何の効果

を負いません

//code above 
this.response.writeHead(200, headers); 
this.response.end(file, "binary"); 
}, 
{ where: "server", name: "downloadData" } 
); 

以下のようにまた、私はどちらもthis.userIdに気づいた私のルートの名前を変更したもMeteor.userIdは経路上で動作します

答えて

1

サーバ側のフックについては、あなたのルートと同じように、{where: "server"}部分を持つためにonBeforeActionが必要であると確信しています。

また、私はiron:routerがこれまでルーティングでサーバー側のユーザー認証を実装していなかったと考えています。認証されたルートにアクセスできるmhagmajer:server-routerなどのより大きな機能を使用して、サーバールーティングに関するパッケージをチェックすることができます。

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

関連する問題