Israel Z Kollie
Israel Z Kollie

Reputation: 273

Meteor Admin User not accessing all users collection

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?

Upvotes: 0

Views: 65

Answers (1)

fool-dev
fool-dev

Reputation: 7777

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

Upvotes: 0

Related Questions