【问题标题】:Restricting views using meteor-roles and iron-meteor works only in certain conditions使用流星角色和铁流星限制视图仅在某些条件下有效
【发布时间】:2016-02-03 06:01:09
【问题描述】:

所有这些都来自routes.js 文件...

首先,new 操作只有三个路由,然后是两个视图,根据角色以不同的方式列出这些项目;一个用于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 仍会拒绝我访问。

我对此束手无策,我已经查看了有关此问题的所有可能问题,并尝试阅读铁路由器文档和流星角色文档以了解发生了什么,但我无法弄清楚这一点。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript meteor iron-router roles


    【解决方案1】:

    在您的onBeforeAction 挂钩中使用this.render(routeName) 代替Router.go(routeName)。我不确定为什么会出现这种情况,但在 onBeforeAction 中使用 Router.go() 会导致您描述的行为类型。

    【讨论】:

      【解决方案2】:

      您可以尝试以下模板,而不是限制对路由的访问和重定向:

      <template name="newAudition">
          {{#isInRole "director"}}
              You cannot view this page. Go to Profile.
          {{/if}}
          {{#isInRole "actor"}}
              How the New Audition Content
          {{/if}}
      </template>
      

      此外,根据用户角色显示/隐藏导航链接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2016-05-06
        • 2016-01-22
        • 1970-01-01
        • 2015-08-11
        相关资源
        最近更新 更多