我编写了一个发布函数,它获取当前的userId并查找与该用户相关的所有文档。现在,所有用户只能访问他们创建的内容。
我想添加一个用户,该用户基本上是一个可以访问读取,更新或删除所有文档的管理员用户。
有一种简单的方法可以实现吗?请参阅下面的推送功能代码,如何将一个管理员用户添加到发布功能?
Meteor.publish("docs", function() {
return Docs.find({ userId: this.userId });
});
Meteor.methods({
"docs.insert"(
name,
title,
purpose
) {
if (!this.userId) {
throw new Meteor.Error("not-authorized");
}
return Docs.insert({
name,
title,
purpose
userId: this.userId,
});
},
创建和登录用户已经在工作。我唯一需要的是让普通用户可以访问所有文档。
Meteor.publish("docs", function() {
if (this.userId === 'superuser') {
return Docs.find({});
} else {
return Docs.find({ userId: this.userId });
});
Meteor.methods({
"docs.update"(
docId,
<props to update>
) {
if (!this.userId ) {
throw new Meteor.Error("not-authorized");
}
let userId = Docs.findOne({_id: docId}).userId;
if (this.userId === userId || this.userId === 'superuser') {
// Do the update
} else {
throw new Meteor.Error("not-authorized");
}
});