【问题标题】:Meteor (0.9.0.1): Issue assigning admin role using a roles packageMeteor (0.9.0.1):使用角色包分配管理员角色的问题
【发布时间】:2014-10-26 02:19:39
【问题描述】:

我正在使用这个roles meteor package,但我无法进入我的管理面板。

我目前的行为是登录管理员用户被重定向到主页而不是显示在管理区域。

如果我运行以下代码,我会返回“false”,表明该用户不是管理员用户:

Roles.userIsInRole(Meteor.user(), ['admin']);

如果我尝试在控制台上添加角色,我会得到:

Roles.addUsersToRoles("Nvu2wZRg3K2wd4j4u", ['admin']);

未定义

insert failed: MongoError: E11000 duplicate key error index: meteor.roles.$name_1  dup key: { : "admin" }


update failed: Access denied

Roles.addUsersToRoles("Meteor.user()", ['admin']);
undefined

insert failed: MongoError: E11000 duplicate key error index: meteor.roles.$name_1  dup key: { : "admin" }

这似乎表明该用户已经拥有分配给它的角色。我期望的权限错误归结为未发布的角色。如果出现重复错误,那么似乎应该为该用户分配管理员角色。

这是相关代码。 模板:

<template name="adminTemplate">
{{#if isAdminUser}}
    {{> accountsAdmin}}
{{else}}
    Must be admin to see this...
{{/if}}
</template>

<template name="accountsAdmin">
 accountsAdmin template
</template>

助手:

Template.adminTemplate.helpers({
  // check if user is an admin
  isAdminUser: function() {
      return Roles.userIsInRole(Meteor.user(), ['admin']);
  }
})

我的 router.js 文件

Router.configure({
 layoutTemplate: 'layout'
});


Router.map( function () {
 this.route('home', {path: '/'});
 this.route('about', {path: '/about'});
 this.route('faq', {path: '/faq'});
 this.route('myaccount', {path: '/myaccount'});

 this.route('admin', {
    layoutTemplate: 'adminlayout',
    path:'/admin',
    template: 'accountsAdmin',
    onBeforeAction: function() {
        if (Meteor.loggingIn()) {
            this.render(this.loadingTemplate);
        } else if(!Roles.userIsInRole(Meteor.user(), ['admin'])) {
            console.log('redirecting');
            this.redirect('/');
        }
    }
  });
});

我的 startup.js 中有以下内容

  Meteor.startup(function () {
    // This is temporary
    if (Meteor.users.findOne("Nvu2wZRg3K2wd4j4u"))
        Roles.addUsersToRoles("Nvu2wZRg3K2wd4j4u", ['admin']);
  });

有人对此有任何想法吗?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    在客户端设置用户角色是不可能的,你应该在服务器端做这些事情,在代码中硬编码 UUID 从来都不是一个好主意(即使是临时的)。

    出于测试目的,您可以使用以下代码:

    server/collections/users.js

    insertUsers=function(){
      var adminId=Accounts.createUser({
        username:"admin",
        password:"password"
      });
      //
      Roles.addUsersToRoles(adminId,"admin");
    };
    

    server/startup.js

    // always start from scratch (you will want to comment this line at some point !)
    Meteor.users.remove({});
    if(Meteor.users.find().count()===0){
      insertUsers();
    }
    

    你可以摆脱你的isAdminUser助手,Roles提供了一个专门为此目的设计的助手,它叫做{{isInRole "list of comma separated roles"}}

    {{#if isInRole "admin"}}
      ...
    {{else}}
    

    当然,您必须登录,因为助手正在针对 Meteor.user() 进行测试。

    我猜您的管理员用户从未有机会受到管理员角色的影响...

    您的代码的其余部分看起来不错,因此希望在解决这些细节后它应该可以工作。

    【讨论】:

    • 太好了,谢谢 :) 我只需要在 insertUsers 函数中将“用户名”更改为“电子邮件”。现在开始工作了:)
    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 2017-10-16
    • 2011-01-05
    • 2016-11-28
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多