如何从客户端的 Meteor.users 集合中读取角色数组? [英] How to read the roles array from Meteor.users collection in Client?

查看:40
本文介绍了如何从客户端的 Meteor.users 集合中读取角色数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用包:alanning/meteor-roles

我正在为管理员构建一个简单的 UI 来管理其他用户角色.一个用户可以拥有多个角色,我使用复选框来选择角色.

I am building a simple UI for Admin to manage other users roles. A user can have more than one role, I am using checkbox to choose roles.

我面临的问题是我无法访问存储在客户端代码中 Meteor.users 集合中的角色数组.

The problem I am facing is that I am unable to access the roles array that is stored in Meteor.users collection in my Client code.

在助手中,我正在检查用户角色,以便在 UI 中将其加载为已检查...

In a helper, I am checking the user roles in order to load it in the UI as checked...

注意:现在所有用户都可以访问此 HTML,只是为了测试它.在我开始工作后,它将仅对管理员可用.所以我注意到此代码仅适用于登录用户,而不适用于所有用户.

Note: this HTML is accessible for all users now just to test it. It will be available only for Admin after I get it to work. So I noticed that this code works only for signed in user and not for all users.

我的代码:

HTML:

<label class="checkbox-inline"><input type="checkbox" class="userRole" value="r1" checked="{{isRoleChecked 'r1'}}">r1</label>
<label class="checkbox-inline"><input type="checkbox" class="userRole" value="r2" checked="{{isRoleChecked 'r2'}}">r2</label>
<label class="checkbox-inline"><input type="checkbox" class="userRole" value="r3" checked="{{isRoleChecked 'r3'}}">r3</label>
<label class="checkbox-inline"><input type="checkbox" class="userRole" value="r4" checked="{{isRoleChecked 'r4'}}">r4</label>
<label class="checkbox-inline"><input type="checkbox" class="userRole" value="r5" checked="{{isRoleChecked 'r5'}}">r5</label>
<label class="checkbox-inline"><input type="checkbox" class="userRole" value="r6" checked="{{isRoleChecked 'r6'}}">r6</label>

isRoleChecked 助手:(客户端代码)

isRoleChecked helper: (Client code)

isRoleChecked: function(value) {

  // didn't work
  roles = Roles.getRolesForUser(this._id);
  if (roles) {
    for(var i=0; i < roles.length; i++) {
      if (roles[i] == value){
        return true;
      } else {
        return false;
      }
    }
  }
},

推荐答案

默认情况下 Meteor 不会在 profile 之外为 Meteor.users 集合发布数据,除了当前用户.其基本原理是确保始终隐藏敏感数据.

By default Meteor does not publish data outside profile for Meteor.users collection, except for the current user. The rationale is to make sure sensitive data is always hidden.

如果安装了自动发布包,系统上所有用户的信息都会发布到所有客户端.这包括* usernameprofile 和服务中的任何公开字段(例如 services.facebook.idservices.twitter.screenName).此外,使用自动发布时,会为当前登录的用户发布更多信息**,包括访问令牌.

If the autopublish package is installed, information about all users on the system is published to all clients. This includes* username, profile, and any fields in services that are meant to be public (eg services.facebook.id, services.twitter.screenName). Additionally, when using autopublish more information** is published for the currently logged in user, including access tokens.

(来源:https://docs.meteor.com/api/account.html#Meteor-users)

* 表示其他字段被排除,因此 roles 不会被 autopublish 发布.

* means other fields are excluded, therefore roles is not published by autopublish.

** 这包括当前用户的 roles 字段.

** this includes roles field for the current user.

这就是为什么您的代码仅适用于当前用户的原因.

That is why your code works only for the current user.

因此,您只需要设置一个发布(并订阅它),明确发送您的用户对客户端的 roles 字段:

Therefore you just need to setup a publication (and subscribe to it) that explicitly sends the roles field of your users to the client:

Meteor.publish('usersRoles', function () {
  return Meteor.users.find(mySelector, {
    fields: {
      roles: 1
    }
  });
});

这篇关于如何从客户端的 Meteor.users 集合中读取角色数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆