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
921 stars 168 forks source link

Roles.userIsInRoles is uncertain #208

Closed ansaries closed 5 years ago

ansaries commented 8 years ago

i am trying to check the roles during the insert and update operations like below:

Categories.allow({
  insert(category) {
    if (Roles.userIsInRole(this.userId, ['admin'],"admin-group")) {
      console.log("Approved role for insert");
      return true;
    }
    console.log("Non admin insert operation");
  },
})

but it always return false.

secondly i was trying to use the same method in ui-router resolve, again it return's false. then i used it in $onInit() of my component where it returns false upon url navigation but when i press back from my browser it return true. cool :)

i dont know whether it has something to do with the userIsInRoles method or using it in wrong life cycle hooks. Can someone help me on this.

jerfowler commented 8 years ago

Don't pass an array to userIsInRole.... Roles.userIsInRole(this.userId, 'admin','admin-group')

ansaries commented 8 years ago

dear @jerfowler thanks for the tip, actually I did following:

Categories.allow({
  insert(category) {
    var currentUser = Meteor.user();
    if (Roles.userIsInRole(currentUser, 'admin',"admin-group")) {
      console.log("Approved role for insert");
      return true;
    }
    console.log("Non admin insert operation");
  },
})

and it is working, may be i did not keep the allow method in the Meteor.isServer block. But i am still struggling with the routing, i want to enable and disable some routes based on roles. I am using Angular-ui-router. Any tip for routing?

Note: I tried to use Roles.userIsInRoles in resolve of my route, it did not work at all. I called it in my controller constructor, it did not work, then i used it in $onInit() of my controller it still did not work as required. Then I created a service to check roles and create menues accordingly, it does not work as expected. As I said it is uncertain. Its a great package and i am sure i am doing a mistake somewhere.

jerfowler commented 8 years ago

My only tip would be not to use Angular... yuk! Go React, for the win!

However, if you're committed to that framework, then ask around on the Meteor forums or Stack Overflow. The Meteor Chef also has a pretty active slack group, that would probably be your best bet to getting help quickly. Just an FYI, Github issues are meant for reporting bugs and asking for features, not really an appropriate place to be asking questions on how to use it.