我正在尝试在我的网站上创建管理部分。管理员可以转到管理页面并查看其组中的用户表。我只想发布该管理员组中的用户。例如。他是小组['足球'],但我不希望他看到['曲棍球']组中的所有用户。用户可以在多个组中,但是一旦我理解了如何查询它,我就应该能够解决这个问题。
无论如何,这是我到目前为止所拥有的:
Meteor.publish('users', function() {
groups = Roles.getGroupsForUser(this.userId)
group = groups[0] //Just get the first group for now
// If the user is an admin of any group (the first one in the array)
if (Roles.userIsInRole(this.userId, ['admin'], group)) {
return Meteor.users.find({roles: {$in: groups}},
{fields: {emails: 1,
createdAt: 1,
roles: 1
}
});
} else {
console.log('null')
return null;
}
});
我的路由器订阅:
Meteor.subscribe("users")
现在当我替换:
{roles: {$in: groups}}
只是:
{}
它可以工作,但我的表包含所有用户,而不仅仅是组的用户。 我应该用什么查询来完成这项工作?我正在使用alanning的角色包。
答案 0 :(得分:2)
此代码有大约5%的机会开箱即用,因为我没有集合来测试它,我无法运行此代码,我没有角色包,我没有&# 39;没有你的用户数据库,我之前从未在光标上做过.map,哈哈。
<强> /server/methods.js 强>
Meteor.methods({
returnAdminUsers: function(){
var results = [];
var results = Roles.getUsersInRole(['admin']).map(function(user, index, originalCursor){
var result = {
_id: user._id,
emails: user.emails,
createdAt: user.createdAt,
roles: user.roles
};
console.log("result: ", result);
return result;
});
console.log("all results - this needs to get returned: ", results);
return results;
}
})
<强> /client/somethingsomething.js 强>
Meteor.call("returnAdminUsers", function(error, result){
if(error){
console.log("error from returnAdminUsers: ", error);
} else {
Session.set("adminUsers", result);
}
});
然后在你的助手中:
Template.somethingsomething.helpers({
adminUsers: function(){ return Session.get("adminUsers") }
});
<强> /client/somethingsomething.html 强>
{{#each adminUsers}}
The ID is {{_id}}
{{/each}}
答案 1 :(得分:1)
如果您使用的是meteor-tabular / TabularTables,这个答案可以提供帮助:
Display only users with a specific role in meteor-tabular table
如果你想检查这样一个群体的所有规则:
Meteor.users.find({
'roles.myrole': {$exists : true}
});
或只有几个:
Meteor.users.find({
'roles.myrole': {$in: ['viewer', 'editor']}
});