簡體   English   中英

僅在特定條件下使用流星和鐵流星限制視圖

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM