2013-01-05 35 views
2

隱私設置,我有一組文件,可以在其周圍的各種READ隱私設置:理想架構設計用於在MongoDB中和流星

  1. 他們就可以完成公共(任何註冊用戶)可以查看他們
  2. 只有跟隨你的人才能看到它們(此「跟隨者」 數組存儲在每個用戶的文檔中)
  3. 它們對於發佈文檔的人也是私人的。
  4. 他們可以擁有自定義隱私,允許您指定可以查看文檔的個人用戶。此外,您還可以允許用戶組查看文檔(例如,可能會有一個名爲「Sample Group」的組,其中有20個用戶,您可以允許該組查看該拼貼。)

關於如何在MongoDB中高效地實現一個模式,我很遺憾,並且希望深入瞭解一下實現這個模式的最佳實踐。

+0

這些權限僅用於讀取操作 - 即:您允許組中的成員只讀取文檔,而不是編輯它? - 或者,像谷歌文檔,你可以設置一些用戶閱讀,一些編輯等... – Alex

+0

只讀詳細信息。 – HGandhi

回答

0

我們已經做了多次訪問級別和貓鼬的幾個項目,這一直是我們最喜歡的方法,到目前爲止:

var ACCESS_MODES = 'public followers private explicit'.split(' '); 

var projectSchema = new Schema({ 
    access: { type: String, enum: ACCESS_MODES, required: true, default: 'public' }, 
    owner: { type: Schema.Types.ObjectId, ref: 'User' }] 
}); 

然後,我們通常執行模式上的一些自定義的接入方式,如:

projectSchema.statics.getByIdFor = function(user, id, done) { 
    this.findOne({ _id: id }).populate('owner').exec(onFound); 
    function onFound(err, project) { 
    // now check 'user' against the project's access method: 
    if (project.access === 'public') return done(undefined, project); 
    if (project.access === 'private') { 
     // ...etc, handle the logic for access at different levels 
    } 
    // finally, they didn't get access 
    done(new Error('no permission to access this project')); 
    } 
}; 

所以,你現在可以做這樣的事情,並且知道它是安全的:

ProjectModel.findByIdFor(loggedinUser, req.params.projectId, onFound);

要找到所有的項目,用戶可以訪問:

projectSchema.statics.getForUser = function(user, done) { 
    var accessible = []; 
    this.find({ access: 'public' }).exec(onPublic); 
    this.find({ access: 'followers' }).populate('owner').exec(onFollowers); 
    this.find({ access: 'private', owner: user }).exec(onPrivate); 
    this.find({ access: 'explicit' }).populate('owner').exec(onExplicit); 
    // add onPublic/Followers/Private/Explicit to accessible where user is in the correct list 
}; 
+0

這裏的麻煩是,每當用戶得到一個新的追隨者,每個文檔的追隨者收集將需要更新... – Alex

+0

你不是說這已經發生?「只有跟隨你的人才能看到他們(這個」關注者「數組存儲在每個用戶的文檔中)」 - OP – hunterloftis

+0

哦,我明白你的意思了 - 這是項目模式,關注者存儲在用戶中。這很容易,1s ... – hunterloftis

0

既然你沒有指定你使用的驅動程序(?雖然標記JavaScript,因此也許你正在使用的貓鼬)我會嘗試使用僞代碼/結構來回答這個問題。

document收集我認爲可能是這個樣子:

{ 
    _id, 
    title, 

    owner, //ref to User collection? 

    access, //'public', 'followers' etc... 

    permissions[] 
} 

Permission可能類似於:

{ 
    // one or the other 
    user_id 
    group_id 
} 

現在,棘手的部分是生產可見對於一個給定的文件清單用戶。
解決這個

function findDocumentsViewableByUser(userID){ 

    var followedBy = //populate a list of userIDs that FOLLOW the passed in userID 

    var groupIDs = //populate a list of groupIDs this user is a member of 

    // all documents where access = 'public' 

    // all documents where access = 'followers' AND owner_id is in followedBy 

    // all documents where access = 'custom' 
    // and permissions.user_id = userID OR groupIDs contains permissions.groupID 
} 

根據您的用戶和組類型文檔的結構,在上述findDocumentsViewableByUser的查詢將顯著減少。
你也許最好使用聚合框架。

+0

這是爲了在Meteor中實現。 – HGandhi