流星页面重新加载角色
Meteor page reload with Roles
当我重新加载页面时,我的应用程序中的角色出现问题。如果我使用按钮等并像您在网站上那样导航,问题就不存在了。
但是问题是什么?在我的 meteor 模板中,我应该为管理员和普通用户显示不同的视图,所以在我的模板 onRendered 函数中,我检查角色并对此做出反应。下面是代码:
/* Predefined changes to current html are done in this function!*/
Template.Planning.onRendered(function () {
var userid = Meteor.userId();
if (Roles.userIsInRole(userid,'admin', Roles.GLOBAL_GROUP)){
//do something
}
});
由于特定原因,当我以管理员身份登录但重新加载页面时,if 块中的代码未执行。当我使用导航栏访问它时它确实有效,所以我猜想在调用 onRendered 函数时角色尚未加载。我该如何解决这个问题?
包 alanning:roles
的工作方式是 "autopublishes" 客户端的角色定义,因此您可以使用它们 "immediately"。
在这种情况下,立即意味着他们的订阅准备就绪。在那之前,函数 Roles.userIsInRole
不会 return 任何真实的东西。
为了检查它们是否可用,请使用反应式 Roles.subscription.ready()
方法。
如果您在 onRendered
中遇到反应问题,您可以检查 onCreated
的 autorun
中的角色。
例如:
Template.Planning.onCreated(function () {
const instance = this
instance.state = new ReactiveDict()
instance.autorun(() => {
if (Roles.subscription.ready()) {
var userid = Meteor.userId()
if (Roles.userIsInRole(userid,'admin', Roles.GLOBAL_GROUP)){
instance.state.set('isAdmin', true) // reactive data source
}
}
})
})
在路由器中使用
您甚至可以在路由器级别使用此方法,以便 "wait" 加载所有角色。这很有意义,尤其是当您的客户端路由逻辑大量使用角色来管理访问时。
安全注意事项
别忘了,这只是 UI 糖果。可以绕过客户端上的角色(以及路由)。使用 Roles.userIsInRole
.
在方法/发布中重新检查每个敏感的方法调用和订阅两次
当我重新加载页面时,我的应用程序中的角色出现问题。如果我使用按钮等并像您在网站上那样导航,问题就不存在了。
但是问题是什么?在我的 meteor 模板中,我应该为管理员和普通用户显示不同的视图,所以在我的模板 onRendered 函数中,我检查角色并对此做出反应。下面是代码:
/* Predefined changes to current html are done in this function!*/
Template.Planning.onRendered(function () {
var userid = Meteor.userId();
if (Roles.userIsInRole(userid,'admin', Roles.GLOBAL_GROUP)){
//do something
}
});
由于特定原因,当我以管理员身份登录但重新加载页面时,if 块中的代码未执行。当我使用导航栏访问它时它确实有效,所以我猜想在调用 onRendered 函数时角色尚未加载。我该如何解决这个问题?
包 alanning:roles
的工作方式是 "autopublishes" 客户端的角色定义,因此您可以使用它们 "immediately"。
在这种情况下,立即意味着他们的订阅准备就绪。在那之前,函数 Roles.userIsInRole
不会 return 任何真实的东西。
为了检查它们是否可用,请使用反应式 Roles.subscription.ready()
方法。
如果您在 onRendered
中遇到反应问题,您可以检查 onCreated
的 autorun
中的角色。
例如:
Template.Planning.onCreated(function () {
const instance = this
instance.state = new ReactiveDict()
instance.autorun(() => {
if (Roles.subscription.ready()) {
var userid = Meteor.userId()
if (Roles.userIsInRole(userid,'admin', Roles.GLOBAL_GROUP)){
instance.state.set('isAdmin', true) // reactive data source
}
}
})
})
在路由器中使用
您甚至可以在路由器级别使用此方法,以便 "wait" 加载所有角色。这很有意义,尤其是当您的客户端路由逻辑大量使用角色来管理访问时。
安全注意事项
别忘了,这只是 UI 糖果。可以绕过客户端上的角色(以及路由)。使用 Roles.userIsInRole
.