nicolaslopezj / roles

The most advanced roles package for meteor
MIT License
87 stars 13 forks source link

Does checking of roles can be used to limit the subscription? #8

Closed Ajaxsoap closed 8 years ago

Ajaxsoap commented 8 years ago

Hi @nicolaslopezj ,

I have a dashboard with all of the data on my collection and i have a widget which displays the document count, if admin, all documents count, then if non-admin the document count will be based on his profile (company and branch), the guys from publish-count package help me create a publish function.

Here's the publish function:

Meteor.publish("enrollments", function (limit) {
    Counts.publish(this, 'enrollmentsCount', Enrollments.find({}), {noReady: true});
    if (limit) {
        return Enrollments.find({});
    } else {
        return Enrollments.find({}, {limit: limit}) ;
    }
});

Meteor.publish("enrollmentsByCompany", function(company, limit) {
    Counts.publish(this, 'enrollmentCompany', Enrollments.find({company: company}), {noReady: true});
    if (limit) {
        return Enrollments.find({company: company});
    } else {
        return Enrollments.find({company: company}, {limit: limit}) ;
    }
});

Meteor.publish("enrollmentsByBranch", function(branch, limit) {
    Counts.publish(this, 'enrollmentBranch', Enrollments.find({branch: branch}), {noReady: true});
    if (limit) {
        return Enrollments.find({branch: branch});
    } else {
        return Enrollments.find({branch: branch}, {limit: limit}) ;
    }
});

My question is does checking of roles will run on client or only on server? Because when i tried to subscribed by checking the user roles, it seems that it didn't run the subscription. I have this subscription on the client:

var user = Meteor.users.findOne({"_id": this.userId},{fields:{profile: 1}});
  var adminRole = Roles.userHasRole(this.userId, "admin");
  var hqRole = Roles.userHasRole(this.userId, "HQ");
  var branchRole = Roles.userHasRole(this.userId, "Branch");
  if (adminRole)
    Meteor.subscribe("enrollments", 5);
  else if (hqRole)
    Meteor.subscribe("enrollmentsByCompany", user.profile.company, 5);
  else if (branchRole)
    Meteor.subscribe("enrollmentsByBranch", user.profile.branch, 5);

I've console log the count output and gives 0 count.

Is this is something on how do i check the roles or am I missing something or misused?

Ajaxsoap commented 8 years ago

I will now close this issue as i resolved it by running the code on the server.