使用 Meteor 设置管理员帐户以查看所有任务
Set admin account using Meteor to view all tasks
我是 Meteor 的新手,刚刚浏览了此处提供的待办事项列表教程 (https://www.meteor.com/tutorials/blaze/creating-an-app)。我删除了 autopublish 并将显示功能设置为所有任务都是私有的(即用户只能看到自己的任务。)
现在,我想更改它并将一个帐户设置为管理员帐户。管理员可以查看每个人的任务,但其他人看不到任何东西(甚至他们自己的任务也看不到)。我正在尝试使用我已经在应用程序文件夹中下载的 alanning-roles 包来执行此操作。
在我的 tasks.js 文件中,我插入了以下行:
const mod = 'E9Y4qtFXK2qQGAGq3'; // this is the userId of the account that I wish to make admin
Roles.addUsersToRoles(mod, 'moderator');
然后,我不只是显示所有任务,而是将显示所有任务的命令附在一个 if 语句中:
if (Meteor.isServer) {
if (Roles.userIsInRole(this.userId,'moderator')) {
Meteor.publish('tasks', function tasksPublication() {
return Tasks.find();
});
}
}
如果您以 moderator/admin 身份登录,这应该会显示所有任务,否则不会显示任何内容。但是当我 运行 这段代码时,即使我以管理员身份登录,也没有任务显示。我确定我设置的userId是正确的,集合中有任务。有人知道问题出在哪里吗?
(或者,关于如何执行此操作的任何其他建议?不必使用 alanning-roles——我只是认为那将是最简单的)
非常感谢
-C
编辑:如果我在行中将 "this.userId" 替换为 "mod":
if (Roles.userIsInRole(this.userId,'moderator')){...}
然后出现所有任务。所以看来问题出在 this.userId.
的输出上
您应该使用 Meteor.userId() 而不是 this.userId:
if (Meteor.isServer) {
if (Roles.userIsInRole(Meteor.userId(),'moderator')) {
Meteor.publish('tasks', function tasksPublication() {
return Tasks.find();
});
}
}
根据经验,始终使用 Meteor.userId(),除非在出版物中,您应该使用 this.userId
您需要将检查当前用户是否 'moderator' 的位置移动到发布函数内部:
目前在您的代码中,当您访问 this.userId
服务器时,服务器正在启动,this.userId
将是 undefined
。所以你的 if
语句中的代码没有被执行,所以 publish
函数没有被创建,并且没有客户端能够订阅这个数据。
试试这个:
if (Meteor.isServer) {
Meteor.publish('tasks', function tasksPublication() {
if (Roles.userIsInRole(this.userId, 'moderator')) {
return Tasks.find({});
}
});
}
现在,Meteor.isServer
块在启动时运行,创建 tasks
发布,其中包含检查角色的代码。现在每次客户端订阅时都会调用此函数,在此上下文中 this.userId
将是当前客户端的用户 ID。
此外,不要将 alanning-roles 包的源代码放在应用程序的文件夹中 - 通过 运行 meteor add alanning:roles
或通过 npm npm install @alanning/roles --save
[= 包含包21=]
我是 Meteor 的新手,刚刚浏览了此处提供的待办事项列表教程 (https://www.meteor.com/tutorials/blaze/creating-an-app)。我删除了 autopublish 并将显示功能设置为所有任务都是私有的(即用户只能看到自己的任务。)
现在,我想更改它并将一个帐户设置为管理员帐户。管理员可以查看每个人的任务,但其他人看不到任何东西(甚至他们自己的任务也看不到)。我正在尝试使用我已经在应用程序文件夹中下载的 alanning-roles 包来执行此操作。
在我的 tasks.js 文件中,我插入了以下行:
const mod = 'E9Y4qtFXK2qQGAGq3'; // this is the userId of the account that I wish to make admin
Roles.addUsersToRoles(mod, 'moderator');
然后,我不只是显示所有任务,而是将显示所有任务的命令附在一个 if 语句中:
if (Meteor.isServer) {
if (Roles.userIsInRole(this.userId,'moderator')) {
Meteor.publish('tasks', function tasksPublication() {
return Tasks.find();
});
}
}
如果您以 moderator/admin 身份登录,这应该会显示所有任务,否则不会显示任何内容。但是当我 运行 这段代码时,即使我以管理员身份登录,也没有任务显示。我确定我设置的userId是正确的,集合中有任务。有人知道问题出在哪里吗?
(或者,关于如何执行此操作的任何其他建议?不必使用 alanning-roles——我只是认为那将是最简单的)
非常感谢 -C
编辑:如果我在行中将 "this.userId" 替换为 "mod":
if (Roles.userIsInRole(this.userId,'moderator')){...}
然后出现所有任务。所以看来问题出在 this.userId.
的输出上您应该使用 Meteor.userId() 而不是 this.userId:
if (Meteor.isServer) {
if (Roles.userIsInRole(Meteor.userId(),'moderator')) {
Meteor.publish('tasks', function tasksPublication() {
return Tasks.find();
});
}
}
根据经验,始终使用 Meteor.userId(),除非在出版物中,您应该使用 this.userId
您需要将检查当前用户是否 'moderator' 的位置移动到发布函数内部:
目前在您的代码中,当您访问 this.userId
服务器时,服务器正在启动,this.userId
将是 undefined
。所以你的 if
语句中的代码没有被执行,所以 publish
函数没有被创建,并且没有客户端能够订阅这个数据。
试试这个:
if (Meteor.isServer) {
Meteor.publish('tasks', function tasksPublication() {
if (Roles.userIsInRole(this.userId, 'moderator')) {
return Tasks.find({});
}
});
}
现在,Meteor.isServer
块在启动时运行,创建 tasks
发布,其中包含检查角色的代码。现在每次客户端订阅时都会调用此函数,在此上下文中 this.userId
将是当前客户端的用户 ID。
此外,不要将 alanning-roles 包的源代码放在应用程序的文件夹中 - 通过 运行 meteor add alanning:roles
或通过 npm npm install @alanning/roles --save
[= 包含包21=]