Meteor Roles包-userIsInRole始终返回false

Meteor Roles package - userIsInRole always returns false

本文关键字:返回 false -userIsInRole Roles Meteor      更新时间:2023-09-26

我想在路由级别上有一个过滤器,检查用户是否处于特定角色。

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()函数来检查用户角色的日志。