所有这些都来自 routes.js
文件...
首先,只有三个用于new
操作的路由,然后是两个 View ,根据 Angular 色以不同的方式列出这些项目;一份用于导演
,一份用于 Actor
。
/////////////////////////////////////////////////////////
// Routes
Router.route('/:_id/new_audition', {
name: 'newAudition',
controller: 'NewAuditionController',
action: 'new',
where: 'client'
});
Router.route('/:_id/feed', {
name: ':UserFeed',
controller: 'FeedController',
action: 'view',
where: 'client'
});
Router.route('/:_id/list_auditions', {
name: 'listAuditions',
controller: 'ListAuditionsController',
action: 'view',
where: 'client'
});
然后我只是定义 onBeforeAction 行为,以便 Actor
用户可以查看list_auditions
View ,而导演可以访问其他两个 View 。
/////////////////////////////////////////////////////////
// Functions for use with Router Hooks
var forDirectorsOnly = function() {
if (!Roles.userIsInRole(Meteor.user(), 'director')) {
toastr.error("Only directors can view that page", "Invalid Permissions");
Router.go("myProfile");
}
else {
console.log("Director trying to view a page");
this.next();
}
};
var forActorsOnly = function() {
if(!Roles.userIsInRole(Meteor.user(), 'actor')) {
toastr.error("Only actors can view that page", "Invalid Permissions");
Router.go("myProfile");
}
else {
console.log("Actor trying to view a page");
this.next();
}
};
/////////////////////////////////////////////////////////
// onBeforeAction Declarations
Router.onBeforeAction(forDirectorsOnly, {only: ['UserFeed', 'newAudition']});
Router.onBeforeAction(forActorsOnly, {only: ['listAuditions']});
这就是正在发生的事情:
- 我将以主管身份登录并转到
UserFeed
路线,它显示得很好。 (但console.log
消息不会显示。) - 然后,我转到
newAudition
路线,它会重定向并显示权限错误。 - 奇怪的是,当我在浏览器中按
Back
时,它会正确显示newAudition
View (以及console.log
消息)也会显示。) - 最后,正确转到
listAuditions
View 会拒绝我的权限,甚至尝试通过浏览器返回Back
也会继续拒绝我的访问。
我对此束手无策,我已经研究了与此相关的每个可能的问题,并尝试阅读iron router 文档和meteor-roles 文档以了解发生了什么,但我无法弄清楚这一点。任何帮助将不胜感激。
最佳答案
在 onBeforeAction
Hook 中使用 this.render(routeName)
,而不是 Router.go(routeName)
。我不确定为什么会出现这种情况,但在 onBeforeAction
中使用 Router.go()
会导致您所描述的行为类型。
关于javascript - 使用 meteor 角色和铁 meteor 限制 View 仅在某些条件下有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493110/