Python MongoDB和Meteor中隐私设置的理想模式设计

Python MongoDB和Meteor中隐私设置的理想模式设计,python,mongodb,meteor,Python,Mongodb,Meteor,我有一套文档,周围可以有各种阅读隐私设置: 它们可以完成,公众(任何注册用户)都可以查看它们 只有跟随您的人(此“跟随者”)才能看到它们 数组存储在每个用户的文档中) 它们也可以是发布文档的人的隐私 它们可以具有自定义隐私,允许您命名可以查看文档的单个用户。此外,您还可以允许用户组查看文档(例如,可能有一个名为“示例组”的组,其中有20个用户。您可以允许该组查看互动程序。) 我不知道如何在MongoDB中高效地实现该模式,我希望能够深入了解实现该模式的最佳实践。我们已经完成了多访问级别和mong

我有一套文档,周围可以有各种阅读隐私设置:

  • 它们可以完成,公众(任何注册用户)都可以查看它们
  • 只有跟随您的人(此“跟随者”)才能看到它们 数组存储在每个用户的文档中)
  • 它们也可以是发布文档的人的隐私
  • 它们可以具有自定义隐私,允许您命名可以查看文档的单个用户。此外,您还可以允许用户组查看文档(例如,可能有一个名为“示例组”的组,其中有20个用户。您可以允许该组查看互动程序。)

  • 我不知道如何在MongoDB中高效地实现该模式,我希望能够深入了解实现该模式的最佳实践。

    我们已经完成了多访问级别和mongoose的几个项目,到目前为止,这是我们最喜欢的方法:

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

    因为您还没有指定正在使用的驱动程序(虽然标记了Javascript,所以您可能正在使用mongoose?),所以我将尝试使用伪代码/结构来回答这个问题

    您的
    文档
    收藏我认为可能是这样的:

    {
        _id,
        title,
    
        owner, //ref to User collection?
    
        access,  //'public', 'followers' etc...
    
        permissions[] 
    }
    
    权限
    可能类似于:

    {
        // 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中的查询将显著减少。

    您最好也使用聚合框架进行此操作。

    这些权限是否仅用于读取操作-即:您允许组中的成员仅读取文档,而不是编辑文档?-或者,像谷歌文档一样,你可以设置一些用户阅读,一些用户编辑等等。这里的问题是,每次用户获得一个新的关注者,每个文档关注者集合都需要更新…你不是说这种情况已经发生了吗?“它们只能由跟随您的人查看(此“追随者”数组存储在每个用户的文档中)”-OPOh我明白您的意思-这是项目架构,追随者存储在用户上。这很简单,1s…现在,当你检查访问级别时,你
    populate()
    项目所有者检查访问级别。参见我的答案。其中一个大问题是一个获取每个用户可以查看的文档列表的函数。例如,在谷歌文档中,您有一个您创建的或与您共享的所有文档的列表等等。。。。我在回答中使用了伪代码,因为我们不确定他在Meteor中使用的是什么驱动程序/平台。