0

My site allows users to apply by connecting their google account, as soon as an account is created they're given a "pending" role (using alanning:roles package). I would like to have a table for admins to see when new applicants have applied, from there the admin can properly manage the users application (change role to accepted, declined, etc.). So, I have created my table, but it's showing all users, I'm wondering if someone knows a way to make it so only users with the "pending" role are shown in my table?

Here is what I have so far:

 TabularTables.ManageApplications = new Tabular.Table({
   name: 'ManageApplications',
   collection: Meteor.users,
   allow: function (userId) {
     return Roles.userIsInRole(userId, 'admin');
   },
   autoWidth: false,
   oLanguage: {
       "sSearch": "Search: "
   },
   columns: [
     { data: //column details here },
     { data: //column details here },
     { data: //column details here },
     { data: //column details here },
     { data: //column details here },
   ],
 });

This works, but it shows every user (not just users with the "pending" role).

I then created this to try and publish only data for pending users:

 Meteor.publish("pendingUsers", function() {
   var isAdmin = Roles.userIsInRole(this.userId, 'admin');
     if (isAdmin) {
       return Roles.getUsersInRole('pending').fetch();
     } else {
       return null;
     }
 });

and subscribed by adding pub: "pendingUsers", to my table. This somewhat works, it makes it so it only shows data in the columns for "pending" role users, but, it still lists every user and just has blank spaces where the data would be.

If anyone knows how I can achieve this it would be greatly appreciated if you could give some insight as I've been stuck on this for quite a while... I believe it may have to do with "Displaying Only Part of a Collection's Data Set" in the Tabular readme, but I'm very unsure of how to set this up. Any examples or help is extremely appreciated.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
U54
  • 122
  • 1
  • 9

3 Answers3

1

This has been solved by adding the following to my table:

selector: function(userId) {
    return {
        _id: {$in: Roles.getUsersInRole('pending')
                        .map(function(user){ return user._id} ) }
    }
},
U54
  • 122
  • 1
  • 9
0

Given the way that publications work it's more than likely that you have another publication and subscription which is giving you the rest of the users but with a different set of keys/fields. Since multiple publications can be running on the same collection at the same time you want to perform the same .find() on the client that your publication is giving you.

Go ahead and add a selector to your table definition as follows:

selector: function( userId ) {
  return Roles.getUsersInRole('pending');
}

You don't need the .fetch() in your publication btw, Roles.getUsersInRole() already returns a cursor of Meteor.users.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • I added the selector to my table definition, but now I receive this error (http://pastebin.com/W4pfVgBk) on the server console every time I refresh the page, and the table is completely empty now as well and says "No data available in table". I've also removed ".fetch()" from my publication. Any ideas? – U54 Apr 04 '16 at 21:15
  • Your error bubbles up to `collection-hooks` - what do you have in there that might be interfering? – Michel Floyd Apr 04 '16 at 21:50
  • I do have some collection hooks that get ran after a user is created, I removed these to test and I was still receiving the same error. I then removed them as well as removed the package and then received a slightly different error, I then removed kadira:debug and it shortened the error, here is the errors from the server console: http://pastebin.com/qQM1Jh1z – U54 Apr 04 '16 at 22:11
  • basically: `Exception from sub tabular_getInfo id ShD25xYqq3FzC9sgP undefined` - what is that the _id of? – Michel Floyd Apr 04 '16 at 22:42
  • I've looked and cant seem to find any match for the id, and every time I refresh my page I get a new error and the id is different each time. I read more and see there are some requirements when creating a [custom publish function](https://github.com/aldeed/meteor-tabular#using-a-custom-publish-function) I'm guessing this is whats causing the error. I see in the [Meteor Roles 2.0] (https://github.com/alanning/meteor-roles/tree/v2.0) [api docs](http://i.imgur.com/tKRxCLp.png) that I can set [queryOptions], I believe this is what I need to do I'm just a little unsure of how this would look. – U54 Apr 05 '16 at 00:40
  • As an update, I guess I didn't need the custom publish function anyways, so I removed that and only used a selector. Adding this to my table solved the problem `selector: function(userId) { return { _id: {$in: Roles.getUsersInRole('pending') .map(function(user){ return user._id} ) } } },` – U54 Apr 05 '16 at 18:11
0

Use Selector: https://github.com/aldeed/meteor-tabular#modifying-the-selector

If you want to check all the rules of such a group:

     selector: function (userId) {
       return 'roles.myrole': {$exists: true}};
     },

Or with just a few:

     selector: function (userId) {
       return 'roles.myrole': {$in: ['viewer', 'editor']}};
     },
Liko
  • 2,130
  • 19
  • 20