title: Publication of items where User is in group (Alanning Roles and Publications)
body: <p>I am using <a href="https://atmospherejs.com/alanning/roles" rel="nofollow">Alanning Roles</a> to maintain a set of groups/roles for the users of my application.  When a user creates an "Application", I generate a new role for them as the <code>app_name + UUID</code>, then add that as a group with the roles of <code>Admin</code> to the user that created it.  I can then use the combination of the generated group name plus either the <code>Admin</code> or <code>Viewer</code> roles to determine which <code>Applications</code> the user has rights to see and/or edit.</p>
  
  <p>The issue that I am having is that I can't figure out a good way to get the publication to only publish the things the user should see.  I know that, by default at least, publications are not "reactive" in the way the client is, and they they are only reactive for the cursors they return.  But, in my code I create the group/role first, add it to the user, then save the "Application", which I thought would rerun my publication, but it did not:</p>
  
  <pre><code>Meteor.publish('myApplications', function(groups) {
    if (this.userId) {
      console.log('Running myApplications publication...');
      console.log('Found roles for user ' + this.userId + ': ', Roles.getGroupsForUser(this.userId));
      return Applications.find({group: {$in: Roles.getGroupsForUser(this.userId)}});
    } else {
      //console.log("Skipping null user");
      return null;
    }
  });
  </code></pre>
  
  <p>But, contrary to what I thought would happen (the whole publication method would re-run), I am guessing what really happens is that only the Cursor is updates.  So for my next attempt, I added the mrt:reactive-publications package and simply got a cursor to the Meteor.users collection for the user, thinking that would "trigger" the publication to re-run when the user gets updated with the new group/role, but that didn't work.</p>
  
  <p>I have this finally working by simply passing in the groups for the user:</p>
  
  <pre><code>Meteor.publish('myApplications', function(groups) {
    if (this.userId) {
      if (!groups || groups.length === 0) {
        groups = Roles.getGroupsForUser(this.userId);
      }
      console.log('Running myApplications publication...');
      console.log('Found roles for user ' + this.userId + ': ', Roles.getGroupsForUser(this.userId));
      return Applications.find({group: {$in: groups}});
    } else {
      //console.log("Skipping null user");
      return null;
    }
  });
  </code></pre>
  
  <p>And then I just call the publication like <code>Meteor.subscribe('myApplications', Roles.getGroupsForUser(Meteor.userId()))</code> in my route's <code>waitOn</code>, but this would mean that any client could call the same publication and pass in any groups they like, and potentially see documents they were not intended to see.  That seems like a pretty large security flaw.</p>
  
  <p>Is there a better way to implement this such that the client would not be able to coax their way to seeing stuff not theirs?  I think the only real way would be to gather the groups on the publication side, but then it breaks the reactivity.</p>