[Solved] Alanning roles V3 how to publish users in role?

I using alanning roles v3
I creating list of users and their roles.

I try to publish it like this :

Meteor.publish('users.all', () => {
        return [
            Meteor.users.find(), // to publish all users
            Meteor.roles.find() // to publish all roles
        ];
    });

in client I tried this :

Roles.getRolesForUser(userId);

but it return [] empty array.

how to publish user-role data?

finally I found it :

Meteor.publish('users.all', () => {
        return [
            Meteor.users.find(),
            Meteor.roles.find(),
            Meteor.roleAssignment.find() //<--- add this
        ];
    });
3 Likes

For future reference to others, the full details of changes you need to make between V2 and V3 are here:

1 Like

Actually, you just have to publish the roleAssignment collection and for security reasons I recommend you to apply this validation:

Meteor.publish('roles', function () {
    return Meteor.roleAssignment.find({'user._id': this.userId});
});

In this way, you can only access to the permissions of the user logged. Also, with this way you can use all the Roles functions.

If you want to access to roles of other users I recommend you to do it through Meteor methods since if you removed the insecure package, the write methods of Roles package are disabled from the client. Although, you can revert that behavior using Meteor.roleAssignment.allow/deny methods but it isn’t recommended.

1 Like

The current example in the main page of the repo creates an unnamed publication to have this data published automatically without needing an explicit subscription (as per this doc). It also covers the need for the user to be logged in (pretty much the same way diavrank95 is very aptly doing here).

Meteor.publish(null, function () {
  if (this.userId) {
    return Meteor.roleAssignment.find({ 'user._id': this.userId });
  } else {
    this.ready()
  }
})
2 Likes

But that only returns user’s roles that has logged in. When we need all roles

You can change the query condition and option to match your requirement.