Search code examples
meteorcollectionsmeteor-autoformsubscriptions

Meteor Yogiben:Admin Filtering by Logged in User


I am using the yogiben:admin package for meteor and I would like the currently logged in user to be able to see only their own objects from each collection.

How do I integrate subscriptions into the admin? Is it part of autoform?


Solution

  • So I am starting to figure it out.

    I cloned the package using the technique described here: https://stackoverflow.com/a/30864393/55124

    Now I am looking into the lib/server/publish.coffee file to edit the publication.

    Meteor.publishComposite 'adminCollectionDoc', (collection, id) ->
        check collection, String
        check id, Match.OneOf(String, Mongo.ObjectID)
        if Roles.userIsInRole this.userId, ['admin']
            find: ->
                adminCollectionObject(collection).find(id)
            children: AdminConfig?.collections?[collection]?.children or []
        else
            @ready()
    
    Meteor.publish 'adminUsers', ->
        if Roles.userIsInRole @userId, ['admin']
            Meteor.users.find()
        else
            @ready()
    
    Meteor.publish 'adminUser', ->
        Meteor.users.find @userId
    
    Meteor.publish 'adminCollectionsCount', ->
        handles = []
        self = @
    
    _.each AdminTables, (table, name) ->
        id = new Mongo.ObjectID
        count = 0
        table = AdminTables[name]
        ready = false
        selector = if table.selector then table.selector(self.userId) else {}
        handles.push table.collection.find().observeChanges
            added: ->
                count += 1
                ready and self.changed 'adminCollectionsCount', id, {count: count}
            removed: ->
                count -= 1
                ready and self.changed 'adminCollectionsCount', id, {count: count}
        ready = true
    
        self.added 'adminCollectionsCount', id, {collection: name, count: count}
    
    self.onStop ->
        _.each handles, (handle) -> handle.stop()
    self.ready()
    
    Meteor.publish null, ->
        Meteor.roles.find({})
    

    But it turns out, it is as simple as changing the a line in lib/both/startup.js

    return AdminTables[name] = new Tabular.Table({
      name: name,
      collection: adminCollectionObject(name),
      pub: collection.children && adminTablePubName(name),
      sub: collection.sub,
      columns: columns,
      extraFields: collection.extraFields,
      dom: adminTablesDom,
      selector: collection.selector || function( userId ) {
        return { owner: userId };
      }
    

    There was an empty selector function and I've added return owner: userId.