2015-09-07 26 views
0

我試圖在Meteor的文檔級別創建訪問控制系統,並且我似乎錯過了如何防止用戶獲取文檔。如何拒絕獲取操作的訪問

我讀過collection.allowcollection.deny的文檔。通過這些物體我們可以控制「誰」可以update,removeinsert。問題是流星似乎沒有爲fetch操作提供類似的功能。 拒絕未經授權的用戶閱讀文檔的正確方法是什麼?

額外要求:這需要發生服務器端,所以我們不會泄漏文件給未經授權的用戶通過網絡。

回答

1

沒有辦法拒絕讀取到收集數據一旦到達客戶端上。理論上,在客戶端上無法實際執行任何東西,因爲用戶可以修改代碼。但是,您可以強制執行發佈的文檔。

發佈函數可以處理arbirtary複雜性的授權規則。這裏有一個簡單的例子,我們要發佈來自Messages集合只有誰是給定組成員的用戶文檔:

Meteor.publish('messagesForGroup', function(groupId) { 
    check(groupId, String); 

    var group = Groups.findOne(groupId); 

    // make sure we have a valid group 
    if (!group) 
    throw new Meteor.Error(404, 'Group not found'); 

    // make sure the user is a member 
    if (!_.contains(group.members, this.userId)) 
    throw new Meteor.Error(403, 'You are not a member of the group'); 

    return Messages.find({groupId: groupId}); 
}); 
1

1)從您的應用程序中刪除autopublish包。

2)創建自己的發佈和拒絕訪問未經授權的用戶

Meteor.publish("userData", function() { 
    if (this.userId) { // Check user authorized 
     return MyCollection.find(); // Share data 
    } else { 
     this.ready(); // Share nothing 
    } 
}); 
+0

是否'Meteor.publish'隻影響'Meteor.subscribe'或者它也影響' MyCollection.findOne()'在客戶端的操作呢? – Parris

+1

它將限制從服務器發送數據到客戶端。客戶端上的MyCollection.findOne()和MyCollection.find()會搜索客戶端數據庫(minimongo)中的數據,而不是在服務器數據庫中搜索數據。但是如果客戶端上沒有數據,這個函數就不能訪問'MyCollection'數據。 – Deadly