如何限制文件夹,所以只有登录我的Meteor应用程序的人才能下载文件?
我研究了多种方法,但主要问题是我无法访问(我得到null.):
Meteor.user() or this.userId()
Run Code Online (Sandbox Code Playgroud)
我试过了:
__meteor_bootstrap__.app
.use(connect.query())
.use(function(req, res, next) {
Fiber(function () {
// USER HERE?
}).run();
});
Run Code Online (Sandbox Code Playgroud)
要么
__meteor_bootstrap__.app.stack.unshift({
route: "/protected/secret_document.doc", // only users can download this
handle: function(req, res) { Fiber(function() {
// CHECK USER HERE ?
// IF NOT LOGGED IN:
res.writeHead(403, {'Content-Type': 'text/html'});
var content = '<html><body>403 Forbidden</body></html>';
res.end(content, 'utf-8');
}).run() }
});
Run Code Online (Sandbox Code Playgroud)
您可以尝试将文件存储在 mongodb 中,这意味着它们将被挂接到您的收集系统中,并且可以在客户端和服务器上进行查询。然后,只需将相关数据发布到特定用户的客户端,或者使用 Meteor.methods 来公开信息。
例子:
假设文件存储在 MongoDB 中,我们首先将它们发布到客户端:
Meteor.publish("files", function(folder) {
if (!this.userId) return;
// the userHasAccessToFolder method checks whether
// this user is allowed to see files in this folder
if (userHasAccessToFolder(this.userId, folder))
// if so, return the files for that folder
// (filter the results however you need to)
return Files.find({folder: folder});
});
Run Code Online (Sandbox Code Playgroud)
然后在客户端,我们自动订阅已发布的频道,以便每当它发生变化时,它就会刷新:
Meteor.startup(function() {
Meteor.autosubscribe(function() {
// send the current folder to the server,
// which will return the files in the folder
// only if the current user is allowed to see it
Meteor.subscribe("files", Session.get("currentFolder"));
});
});
Run Code Online (Sandbox Code Playgroud)
注意。我尚未测试上面的代码,因此请将其视为伪代码,但它应该为您指明解决此问题的总体方向。困难的部分是将文件存储在 mongodb 中!
| 归档时间: |
|
| 查看次数: |
1045 次 |
| 最近记录: |