是否可以通过alanning为角色添加角色:来自模板事件的流星角色?

问题描述 投票:0回答:1

我对Meteor相当新,并且在这个问题上遇到了麻烦。

我希望有一个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());

    }
  });

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

mongodb select meteor roles alanning-roles
1个回答
0
投票

您尝试在客户端中添加角色。但是,客户端仅反映来自服务器的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;
  }
});

此方法假定,用户可以使用特殊的角色/组组合来更改角色。这应该只是很少的人,比如管理员。

另请注意,此方法使用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是一个立即订阅的集合。变化被反映出来。如果您没有立即看到更改

© www.soinside.com 2019 - 2024. All rights reserved.