#if with Roles.userIsInRole not working

176 views Asked by At

I am creating a modal to manage the permissions of a user. But when the modal is loaded. The console returns true from the server, if a users has the role. While {{#if hasRole}} is still showing the wrong label.

editUserPermissionsModal.html

<tbody>
    {{#each roles}}
    <tr>
        <td>{{this.name}}</td>
        <td>
            {{#if hasRole}}
            <span class="label label-success">Toegang</span> 
            {{else}}
            <span class="label label-danger">Geweigerd</span> 
            {{/if}}
        </td>
        <td>
            <select class="roleSelect">
                {{#if hasRole}}
                <option value="allow">Toestaan</option>
                <option value="deny">Weigeren</option>
                {{else}}
                <option value="deny">Weigeren</option>
                <option value="allow">Toestaan</option>
                {{/if}}
            </select>
        </td>
    </tr>
    {{/each}}
</tbody>

editUserPermissionsModal.js

Template.editUserPermissionsModal.helpers({
  roles: function() {
    return Roles.getAllRoles();
  },

  hasRole: function(){
    var userId = Session.get("editing_user");
    var role = this.name;
    Meteor.call("checkRole", userId, role, function(error, result){
      if(error){
        console.log("error", error);
      }
      if(result){
        console.log(result);
        return result;


      }
    });
  }

});

Template.editUserPermissionsModal.events({
  "change .roleSelect": function(event, template){

    var addRole = event.target.value;

    if(addRole == 'allow') {
      var user = Session.get("editing_user");
      var role = this.name;

      Meteor.call('addRoleToUser', user, role)

    }

  }
});

server.js

checkRole:function(userId, role) {
    return Roles.userIsInRole(userId, role);
  }
1

There are 1 answers

0
SylvainB On

This is a typical "method call within a helper" issue. You can read about it here.

Given that you need variable arguments to your method, the easiest solution in your case would probably be using David Weldon's answer and install Sashko's meteor-reactive-method package:

$ meteor add simple:reactive-method

And then:

Template.editUserPermissionsModal.helpers({
  roles: function() {
    return Roles.getAllRoles();
  },

  hasRole: function(){
    var userId = Session.get("editing_user");
    var role = this.name;
    var result = ReactiveMethod.call("checkRole", userId, role);
    console.log(result);
    return result;
  }
});