根据 Meteor 中的用户角色过滤订阅
Filter subscription bases on user roles in Meteor
我正在使用 meteor-roles 包向应用程序添加角色功能。现在,我正在尝试根据具有查看该产品类别的角色的用户来过滤产品订阅。
我以为我可以使用 mongo $where in meteor 但我只能将 $where 函数指定为无法访问角色包的序列化字符串。
将用户添加到角色以查看特定类别的示例。
Roles.addUsersToRoles(userIdA, 'view-product-category', 'product-category-a');
当前代码无效。当函数在 mongo 引擎中反序列化时,userId
不存在。
Meteor.publish('products', function () {
let userId = this.userId;
let query = {
$where: function () {
return Roles.userIsInRole(userId, 'view-product-category', `product-catogory-${this.category}`);
}.toString();
};
return Products.find(query);
});
如何根据不同集合的值过滤集合,这是我在这里尝试做的?
目前的方向是自己序列化函数,把需要的值放在那里。我想知道是否有更通用和更强大的方法来对订阅进行这种 acl 访问过滤。
$where
没有索引而且很慢。我相信您要做的是过滤具有类别键的产品。用户有许多角色,允许他们查看各种类别。
您想将查询设置为:
Products.find({category: {$in: userCategories}});
幸运的是,roles api 有一个 getRolesForUser
功能,可以为您获取所有用户的角色。
也一样:
const myRoles = getRolesForUser(Meteor.userId());
const userCategories = f(myRoles); // whatever you need to do to format these roles as categories
Products.find({category: {$in: userCategories}});
我不清楚您的角色 groups 如何发挥作用,因此是伪答案。但基本上您希望 userCategories
成为用户有权访问的类别名称的 array。只要 category
字段被索引,这个查询就会很快。
我正在使用 meteor-roles 包向应用程序添加角色功能。现在,我正在尝试根据具有查看该产品类别的角色的用户来过滤产品订阅。
我以为我可以使用 mongo $where in meteor 但我只能将 $where 函数指定为无法访问角色包的序列化字符串。
将用户添加到角色以查看特定类别的示例。
Roles.addUsersToRoles(userIdA, 'view-product-category', 'product-category-a');
当前代码无效。当函数在 mongo 引擎中反序列化时,userId
不存在。
Meteor.publish('products', function () {
let userId = this.userId;
let query = {
$where: function () {
return Roles.userIsInRole(userId, 'view-product-category', `product-catogory-${this.category}`);
}.toString();
};
return Products.find(query);
});
如何根据不同集合的值过滤集合,这是我在这里尝试做的?
目前的方向是自己序列化函数,把需要的值放在那里。我想知道是否有更通用和更强大的方法来对订阅进行这种 acl 访问过滤。
$where
没有索引而且很慢。我相信您要做的是过滤具有类别键的产品。用户有许多角色,允许他们查看各种类别。
您想将查询设置为:
Products.find({category: {$in: userCategories}});
幸运的是,roles api 有一个 getRolesForUser
功能,可以为您获取所有用户的角色。
也一样:
const myRoles = getRolesForUser(Meteor.userId());
const userCategories = f(myRoles); // whatever you need to do to format these roles as categories
Products.find({category: {$in: userCategories}});
我不清楚您的角色 groups 如何发挥作用,因此是伪答案。但基本上您希望 userCategories
成为用户有权访问的类别名称的 array。只要 category
字段被索引,这个查询就会很快。