【发布时间】: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