2014-09-01 54 views
2

我使用這個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?

回答

2

設置在客戶端的用戶角色是不可能的,你應該做這個東西的服務器端,並在代碼中硬編碼的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()進行測試。

我猜您的管理員用戶從來沒有被影響了管理角色的機會......

你的代碼的其餘部分看起來不錯,所以希望它應該解決這些細節後工作。

+0

這非常謝謝:)我只需要改變「用戶名」在insertUsers功能「電子郵件」。現在工作:) – user1532669 2014-09-02 15:30:36