Display only users with a specific role in meteor-tabular table

二次信任 提交于 2020-01-07 02:39:52

问题


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.


回答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} ) }
    }
},



回答2:


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.




回答3:


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']}};
     },


来源:https://stackoverflow.com/questions/36375535/display-only-users-with-a-specific-role-in-meteor-tabular-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!