0
votes

I have added roles to my user collections. I'm trying to publish all users collection to admin user only but I'm only getting admin collection being returned currently.

Publish code:

Meteor.publish("users", function() {
    let result = [];
    if (Roles.userIsInRole(this.userId, ["admin"])) {
      result = Meteor.users.find({});
    } else {
      this.stop();
    }
    return result;
  });

Subscription

Meteor.subscribe("users");

Adding admin:

 if (!Meteor.users.find().count()) {
    var users = [
      { name: "Johnson Doe", email: "[email protected]", roles: ["admin"] }
    ];

    _.each(users, function(user) {
      var id;

      id = Accounts.createUser({
        email: user.email,
        password: "password@123",
        profile: { name: user.name }
      });

      if (user.roles.length > 0) {
        Roles.addUsersToRoles(id, user.roles, "default-group");
      }
    });
  }

Is there a way that the admin user can have access to all other user collections?

1
Please add the code of Roles.addUserToRoles and the publication of your admin collection. - Jankapunkt
I'm adding Roles like this: Roles.addUsersToRoles(id, user.roles, "default-group"); And I'm subscribe like this: Meteor.subscribe("users"); Meteor.users.find().fetch() - Israel Z Kollie
So when you add "default-group" to Roles.userIsInRole, does it work then? - Jankapunkt
I did this on the server side but it's still returning only the current user and not all the users: if (Roles.userIsInRole(this.userId, ["admin"], group)) { return Meteor.users.find({ group: group }, { fields: { profile: 1 } }); } else { this.stop(); return; } My goal is for this admin user to read all other users doc. - Israel Z Kollie

1 Answers

0
votes

You should use addUsersToRoles make sure it has you admin collection, before declaring userIsInRole as like

Roles.addUsersToRoles(userId, ['admin']);

You can check carefully this Usage Examples