流星(0.9.0.1):使用角色包
问题描述:
我使用这个roles meteor package,我不能让我的管理面板发行分配管理角色。流星(0.9.0.1):使用角色包
我目前得到的行为记录在管理用户被重定向到主页,而不是被显示的管理区。
如果我运行下面的代码我得到返回“假”,建议用户不是一个管理员用户:
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']);
});
以下任一有人对此有任何想法NE?
答
设置在客户端的用户角色是不可能的,你应该做这个东西的服务器端,并在代码中硬编码的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();
}
你可以摆脱你的帮手,角色提供了一个助手专门为此目的设计的,这就是所谓的{{isInRole "list of comma separated roles"}}
{{#if isInRole "admin"}}
...
{{else}}
当然,您必须先登录,因为帮手正在针对Meteor.user()
进行测试。
我猜您的管理员用户从来没有被影响了管理角色的机会......
你的代码的其余部分看起来不错,所以希望它应该解决这些细节后工作。
这非常谢谢:)我只需要改变“用户名”在insertUsers功能“电子邮件”。现在工作:) – user1532669 2014-09-02 15:30:36