首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以通过模板事件在流星中向用户添加角色?

是否可以通过模板事件在流星中向用户添加角色?
EN

Stack Overflow用户
提问于 2018-03-05 18:08:05
回答 1查看 127关注 0票数 0

我对Meteor相当陌生,在这个问题上一直有真正的麻烦。

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

当我运行我的应用程序并更改选择时,这个角色似乎会弹出一秒钟(在蒙古观看),然后再次消失。我创建了一个小测试来显示用户角色的警报,它显示了包含角色名称的内容,但是一旦您确定了它,角色就消失了。我是不是漏掉了什么?

下面是包含select元素的模板..。

代码语言:javascript
复制
<template name="select">
  <select id="select">
        <option value="working">Looking for work</option>
        <option value="hiring">Hiring</option> 
  </select>
</template>

下面是更改事件的客户端代码

代码语言:javascript
复制
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());

    }
  });

任何帮助都将是非常感谢的,已经在文件和网上搜索了几天,但没有效果。非常感谢:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-05 19:31:19

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

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

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

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

( c)为给定的userId设置角色

关于如何做到这一点,还有文档中的一个好例子。这是一个稍微修改过的版本:

代码语言:javascript
复制
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.addUserToRoles

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

代码语言:javascript
复制
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是立即订阅的集合。变化是积极反映的。如果您没有立即看到更改

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49116829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档