Meteor Roles包-userIsInRole始终返回false
Meteor Roles package - userIsInRole always returns false
我想在路由级别上有一个过滤器,检查用户是否处于特定角色。
this.route('gamePage', {
path: '/game/:slug/',
onBeforeAction: teamFilter,
waitOn: function() { return […]; },
data: function() { return Games.findOne({slug: this.params.slug}); }
});
这是我的过滤器:
var teamFilter = function(pause) {
if (Meteor.user()) {
Meteor.call('checkPermission', this.params.slug, Meteor.userId(), function(error, result) {
if (error) {
throwError(error.reason, error.details);
return null;
}
console.log(result); // returns always false
if (!result) {
this.render('noAccess');
pause();
}
});
}
}
在我的收藏中:
checkPermission: function(gameSlug, userId) {
if (serverVar) { // only executed on the server
var game = Games.findOne({slug: gameSlug});
if (game) {
if (!Roles.userIsInRole(userId, game._id, ['administrator', 'team'])) {
return false;
} else {
return true;
}
}
}
}
我的第一个问题是Roles.userIsInRole(userId, game._id, ['administrator', 'team']
总是返回false。起初,我在router.js
中有这段代码,但后来我认为它不起作用,因为缺少发布/订阅,所以我确保代码只在服务器上运行。我检查了数据库,用户就是这个角色。
我的第二个问题是,在这一点上我得到了一个异常(Exception in delivering result of invoking 'checkPermission': http://localhost:3000/lib/router.js?77b3b67967715e480a1ce463f3447ec61898e7d5:14:28
):this.render('noAccess');
,我不知道为什么。
我已经读过了:meteoroles.userIsInRole()总是返回false,但它并没有解决我的问题。
如有任何帮助,我们将不胜感激。
在teamFilter
钩子中,您调用Meteor.method checkPermission
,它异步运行,并且OnBeforeAction需要同步执行(无回调)。这就是为什么你总是收到虚假信息。
另一件事是你使用Roles.userIsInRole
不正确:
应为:
Roles.userIsInRole(this.userId, ['view-secrets','admin'], group)
在这种情况下,我会检查客户端的角色:
Roles.userIsInRole(userId, ['administrator', 'team'])
您可能担心此解决方案的安全性。我认为你不应该。最重要的是数据,数据由发布功能保护,该功能应检查角色。
请注意,客户可以访问所有模板。
您只能在服务器上为用户添加角色,为此您可以使用Meteor.call({});在服务器的main.js上,从客户端到调用方法的check here方法,在该方法调用后,您可以使用meteormongo和db.users.find({}).pretty()检查角色是否添加到了用户集合中,并查看角色数组是否添加了该usedId的用户,然后您可以在客户端的任何位置使用roles.userIsInRole()函数来检查用户角色的日志。
- Array.every返回false,而不是类型error
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 为什么当我在数组上测试regex时,它会返回false
- 检查文本区域是否为空总是返回false
- PHP strtotime()为JavaScript日期字符串返回false
- onsubmit返回false,但submit仍然执行
- 这个代码会返回false吗
- Ajax向表单返回false
- 表单验证JavaScript编号总是返回false
- 使用!而in运算符在应该为true时返回false,为什么
- jQuery.is(':checked')总是返回false
- Javascript 下划线每个总是返回 false
- 提交返回 false 总是在 jquery 中
- 为什么 1.2 == true 返回 false 如果布尔值 (1.2) 实际上是真的
- 如果循环中至少有一个元素返回 false,如何将变量设置为 false
- 基本JavaScript不断返回false
- 布尔对象上的逻辑 NOT 在 Javascript 中总是返回 false
- jquery focusin没有'调用.focus()并返回false;时无效;
- Ajax-执行成功前返回false
- jQuery JSON flickr提要返回false不工作