我正在从自定义用户权限管理系统迁移到 Alanning:roles v2.0 .我有一个非常基本的结构:
- 基本用户
- 用户组,每个用户组都有特定的设置。我将它们存储在一个“组”集合中。
- 管理群组的用户的群组管理员身份(每个群组都有其群组管理员)。
我之前将组成员和管理员 mongo _id
存储在“组”文档中。这样,我可以被动地发布组:我只需要检查 userId
是否在组文档中的“成员”或“管理员”字段中。
现在我切换到由 Alanning:roles 强制执行的权限管理,我在我的出版物中做了这样的事情:
const userGroupsAsAdmin = Roles.getPartitionsForUser (this.userId, ['group_admin'])
const userGroupsAsMember = Roles.getPartitionsForUser (this.userId, ['member'])
const selector = {$or:[{'_id':{$in: userGroupsAsMember}},{'_id':{$in: userGroupsAsAdmin}}]}
const options = {}
const response = Groups.find(selector, options)
return response
请注意,Roles.getPartitionsForUser ()
只是 Roles.getGroupsForUser ()
的新函数名称。
这里的问题是发布不关注 role
集合中的更改,因此当用户成为成员时,发布不会更新。我知道this is a common issue我知道 3 种方法来解决这个问题,但没有一种让人满意:
最佳候选者:反规范化和复制。我将我的
members
和admins
字段保留在组文档中。让我烦恼的是,我会保留同一事物的 2 个版本,这可能会导致出现不一致。向发布添加参数并使用此参数重新运行它(例如
userGroupsAsMember
),但它依赖于客户端并使其发送不必要的信息。使用低级发布 api,直接或使用 package .过去我已经直接这样做了,但我不想再依赖
<Cursor.observe()
,因为它无法有效扩展并产生不必要的服务器负载。
我是否缺少一个选项?如果不是,让我的出版物保持 react 的最佳方式是什么?
最佳答案
使用 reywood:publish-composite
创建响应式(Reactive)连接。
Meteor.publishComposite("my-groups", {
find: function() {
// findOne won't work, it's not a cursor
return Meteor.users.find(
{_id: this.userId},
{fields: {roles: 1}}
);
},
children: [{
find: function(user) {
// getPartitionsForUser allows the first parameter to be the actual user object.
const userGroupsAsAdmin = Roles.getPartitionsForUser (user, ['group_admin']);
const userGroupsAsMember = Roles.getPartitionsForUser (user, ['member']);
const selector = {$or:[{'_id':{$in: userGroupsAsMember}},{'_id':{$in: userGroupsAsAdmin}}]};
const options = {};
const response = Groups.find(selector, options);
return response;
}
}]
});
Roles.getPartitionsForUser
不返回游标,因此它不能是响应式(Reactive)的。这就是为什么您还需要发布 Meteor.users.find
调用的原因。
关于javascript - 我应该如何保持我的出版物的 react 性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861894/