Meteor 让一个用户阅读和更新所有文档
Meteor make one user to read and update all docs
我编写了一个发布函数,它获取当前用户 ID 并查找与该用户相关的所有文档。现在所有用户只能读取、更新和删除他们创建的内容。
我想添加一个基本上是管理员用户的用户,可以访问以阅读、更新或删除所有文档。
有没有简单的方法可以实现?请看下面我的推送功能代码,如何添加一个管理员用户到发布功能?
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");
}
});
我编写了一个发布函数,它获取当前用户 ID 并查找与该用户相关的所有文档。现在所有用户只能读取、更新和删除他们创建的内容。
我想添加一个基本上是管理员用户的用户,可以访问以阅读、更新或删除所有文档。
有没有简单的方法可以实现?请看下面我的推送功能代码,如何添加一个管理员用户到发布功能?
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");
}
});