是否可以通过模板事件向 meteor 中具有 alanning:roles 的用户添加角色?

Is it possible to add a role to a user with alanning:roles in meteor from an template event?

我对 Meteor 还很陌生,在这个问题上遇到了很大的麻烦。

我想要一个 select 元素,它根据选项 selected 更新用户角色(一旦登录)。当 select 更改时,我将选项的值存储为变量,并尝试将此值作为要添加到用户的角色的名称。

当我 运行 我的应用程序并更改 select 时,该角色似乎会弹出一秒钟(在蒙古语中查看),然后再次消失。我创建了一个小测试来为用户显示角色的警报,其中显示包含角色的名称,但是一旦你确定,角色就消失了。我在这里错过了什么吗?

这是我的模板,其中包含 select 元素...

<template name="select">
  <select id="select">
        <option value="working">Looking for work</option>
        <option value="hiring">Hiring</option> 
  </select>
</template>

这里是更改事件的客户端代码

Template.select.events({

    'change #select': function (event) {

          //remove any current roles added to the user as it will be either 
          //one or the other
         Roles.removeUsersFromRoles( Meteor.userId(), 'working', 'hiring' );

         //add a role to the current user with the value from select box
         var value = $(event.target).val();
         Roles.addUsersToRoles( Meteor.user(), value );


         //each of these alerts displays correctly depending on the select 
         //value
          var test = Roles.userIsInRole( Meteor.user(), 'hiring' ); // true
          if (test===true){
            alert('in hiring role');
          } 

          var test2 = Roles.userIsInRole( Meteor.user(), 'working' ); // true
          if (test2===true){
            alert('in working role');
          }

          // either working or hiring 
          alert(Roles.getRolesForUser(Meteor.userId())); 
          // alert displays count of 1 when you select 'hiring'
          alert(Roles.getUsersInRole('hiring').count());

    }
  });

任何帮助将不胜感激,已经通过文档和在线搜索了几天无济于事。非常感谢:)

您尝试在您的客户端中添加角色。但是,客户端仅反映来自服务器 Roles 集合的数据。

因此,您需要将代码更改为服务器端方法,即

a) 检查 是否允许当前用户更改角色(此处警告,不检查权限时存在潜在的安全威胁)

b) 检查,目标用户是否存在

c) 设置 给定 userId

的角色

a good example in the documentation 如何做到这一点。这是它的一个稍微修改的版本:

Meteor.methods({
  'updateRoles'({userId, roles, group}) {
    check(userId, String);
    check(roles, [String]);
    check(group, String);

    // a) check permission
    if (!this.userId || !Meteor.users.findOne(this.userId) || !Roles.userIsInRole(this.userId, 'update-roles', 'lifted-users'))
        throw new Meteor.Error('403', 'forbidden', 'you have no permission to change roles');

    // b) check target user
    if (!Meteor.users.findOne(userId))
        throw new Meteor.Error('404', 'user not found');

    // c) update user's roles
    Roles.setUserRoles(userId, roles, group);
    return true;
  }
});

此方法假定用户有一个特殊的 role/group 组合,允许更改角色。这应该只是极少数人,比如管理员。

另请注意,此方法 通过使用 Roles.setUserRoles 设置 用户角色。如果你想扩展你需要使用的角色 Roles.addUserToRoles.

然后您可以像每个 Meteor 方法一样从您的客户端调用此方法:

Template.select.events({
  'change #select': function (event) {

    // get value from select box
    var roles = [$(event.target).val()];

    // TODO create a second select for the group
    var group = 'defaultUsers' 

    var userId = Meteor.userId();

    Meteor.call('updateRoles', { userId, roles, group }, (err, res) => {
      // handle err / res
      console.log(Roles.userIsInRole(userId, roles, group)); // should return true
    }); 
  }
});

请注意,客户端上的 Roles 是一个立即订阅的集合。变化是反应性地反映出来的。如果您没有立即看到更改