Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
920 stars 167 forks source link

_roles publication doesn't publish Meteor.roles in v3 #346

Open jrkinclus opened 3 years ago

jrkinclus commented 3 years ago

Calling, say, Roles.addUsersToRoles on client-side in a Meteor method doesn't work due to roles not being published. Causing this kind of error (trying to add user to a role called 'admin'):

Error: Role 'admin' does not exist.
    at Object._addUserToRole (roles_common.js:473)
    at roles_common.js:380

If it's intended that this should work client-side too and you don't have to wrap all such calls inside a if (Meteor.isServer) { block, then this is an issue. Server-side it works of course.

The problem seems to be that the automatically subscribed _roles publication doesn't seem to do doing what it should. In v3.4.0 it's still publishing what looks like v1 format roles for the current user: https://github.com/Meteor-Community-Packages/meteor-roles/blob/v3/roles/roles_server.js#L16

Meteor.publish('_roles', function () {
  var loggedInUserId = this.userId
  var fields = { roles: 1 }

  if (!loggedInUserId) {
    this.ready()
    return
  }

  return Meteor.users.find(
    { _id: loggedInUserId },
    { fields: fields }
  )
})