【问题标题】:Meteor - Publish all Users to Client just for adminMeteor - 将所有用户发布到客户端,仅供管理员使用
【发布时间】:2014-01-06 18:01:20
【问题描述】:

Meteor 有以下问题: 我有一个管理员,需要查看所有注册用户。 但是所有其他用户不应该有看到其他用户的能力。 因此,我在服务器端发布了以下代码

Meteor.publish("adminUsers", function(){

   var result; 
   if (Roles.userIsInRole(this.userId, ["admin"]))
   {
        result = Meteor.users.find();
   }
   //console.log(result);
   return result;
});

在客户端我订阅了这个

Meteor.subscribe("adminUsers");

然后做

AllUsers = new Meteor.Collection("adminUsers");

现在我想使用以下代码获取模板中的所有用户:

Template.adminUserverwaltung.AllUsers = function(){
console.log(AllUsers.find());
return AllUsers.find();
}

并使用以下代码在模板中显示结果:

    <template name="adminUserverwaltung">
  {{#each AllUsers}}
    {{this.username}}
  {{/each}}
</template>

但不幸的是,它不起作用...有人可以帮我吗?

【问题讨论】:

    标签: mongodb templates meteor


    【解决方案1】:

    当您订阅集合时,订阅名称是 adminUsers,但用户集合仍然是 Meteor.users,这已经定义了。

    所以只需更改它以使用它来代替

    Template.adminUserverwaltung.AllUsers = function(){
        return Meteor.users.find();
    }
    

    【讨论】:

      【解决方案2】:

      你快到了......这个答案使用meteor-roles包作为问题中提出的OP。

      Meteor.publish("adminUsers", function(){
         var result = [];
         if (Roles.userIsInRole(this.userId, ["admin"])) {
              result = Meteor.users.find();
         } else {
              this.stop();
              // YOUUU SHALL NOT.... PASS!!!  ~Gandalf
         }
         return result;
      });
      

      在客户端,订阅这个:

      Meteor.subscribe("adminUsers");
      

      不要这样做。省略它。 Meteor.users 是你的收藏。

      // NOO!! AllUsers = new Meteor.Collection("adminUsers");
      

      现在我想使用以下代码获取模板中的所有用户:

      Template.adminUserverwaltung.helpers = {
          AllUsers: function(){
              return Meteor.users.find();
          }
      };
      

      并使用以下代码在模板中显示结果:

      <template name="adminUserverwaltung">
      {{#each AllUsers}}
          {{this.username}}
      {{/each}}
      </template>
      

      它现在应该可以工作了。 :)

      【讨论】:

      • 多年后我现在看到了你的答案,我喜欢你的代码和你的 cmets。它让我很开心,让我今天在工作中微笑??
      【解决方案3】:

      您可以这样做:

      if (Meteor.isClient) {
        Meteor.subscribe("allUsers");
        Template.hello.users = function() {
          return Meteor.users.find();
        }
      }
      
      if (Meteor.isServer) {
        Meteor.startup(function () {
          Meteor.publish("allUsers", function(){
            var user = Meteor.user();
            if (user && user.emails[0].address === 'root@localhost'){
              return Meteor.users.find();
            }
            return null;
          });
        });
      }
      

      这样只有用户root@localhost 才能看到所有用户。其他人只会看到自己。

      meteor docs 中有一个自定义用户集合发布示例。

      【讨论】:

      • 这似乎不鼓励meteor.com: Meteor.user() 记录不使用Meteor.user()Meteor.publish()
      • 自 2013 年以来可能已经改变。publish 文档建议使用 this.userId
      • 是的,我发现我需要为管理员收集_id 并在服务器端填充一个全局数组以与this.userId 进行比较。尝试在发布中探测用户记录的属性对我不起作用。
      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 2016-10-01
      • 2016-04-06
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多