[英]Restricting views using meteor-roles and iron-meteor works only in certain conditions
所有這些都來自routes.js
文件...
首先,只有3條路線可以進行new
操作,然后有2個視圖根據角色不同列出這些項目。 一個給director
,一個給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行為,以便actors
用戶可以查看list_auditions
視圖,並且導演可以訪問其他兩個。
/////////////////////////////////////////////////////////
// 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
視圖(並且還會顯示console.log
消息。) listAuditions
視圖會拒絕我權限,甚至嘗試通過瀏覽器Back
仍會拒絕我訪問。 我為此付出了很多努力,我查看了與此相關的所有可能問題,並嘗試閱讀鐵路由器文檔和流星角色文檔以了解發生了什么,但我無法弄清楚。 任何幫助,將不勝感激。
取而代之的Router.go(routeName)
在onBeforeAction
掛鈎使用this.render(routeName)
我不確定為什么會這樣,但是在onBeforeAction
使用Router.go()
onBeforeAction
導致您描述的行為種類。
您可以嘗試使用以下模板,而不是限制對路由和重定向的訪問:
<template name="newAudition">
{{#isInRole "director"}}
You cannot view this page. Go to Profile.
{{/if}}
{{#isInRole "actor"}}
How the New Audition Content
{{/if}}
</template>
另外,根據用戶角色顯示/隱藏導航鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.