How to read the roles array from Meteor.users collection in Client?

后端 未结 2 720
终归单人心
终归单人心 2021-01-21 21:07

I use the package: alanning/meteor-roles

I am building a simple UI for Admin to manage other users roles. A user can have more than one role, I am using checkbox to choo

相关标签:
2条回答
  • 2021-01-21 21:21

    You need to publish the roles key:

    Server:

    Meteor.publish('roles',() => {
      if ( Roles.userIsInRole(this.userId,['Admin']) {
        return Meteor.users.find({},{fields: {roles: 1}});
      } else this.ready();
    });
    

    And subscribe to that on the client:

    Meteor.subscribe('roles');
    
    0 讨论(0)
  • 2021-01-21 21:32

    By default Meteor does not publish data outside profile for Meteor.users collection, except for the current user. The rationale is to make sure sensitive data is always hidden.

    If the autopublish package is installed, information about all users on the system is published to all clients. This includes* username, profile, and any fields in services that are meant to be public (eg services.facebook.id, services.twitter.screenName). Additionally, when using autopublish more information** is published for the currently logged in user, including access tokens.

    (source: https://docs.meteor.com/api/accounts.html#Meteor-users)

    * means other fields are excluded, therefore roles is not published by autopublish.

    ** this includes roles field for the current user.

    That is why your code works only for the current user.

    Therefore you just need to setup a publication (and subscribe to it) that explicitly sends the roles field of your users to the client:

    Meteor.publish('usersRoles', function () {
      return Meteor.users.find(mySelector, {
        fields: {
          roles: 1
        }
      });
    });
    
    0 讨论(0)
提交回复
热议问题