流星将alanning角色和aldeed collection2一起使用
问题描述:
我在使用meteor-collection2的项目中设置角色的问题。我认为这是collection2文档中记录的角色包。 我使用accounts-password和ian:accounts-ui-bootstrap-3作为我的帐户解决方案。下面是我的配置吧:流星将alanning角色和aldeed collection2一起使用
Accounts.ui.config({
requestPermissions: {},
extraSignupFields: [{
fieldName: 'first-name',
fieldLabel: 'First name',
inputType: 'text',
visible: true,
validate: function(value, errorFunction) {
if (!value) {
errorFunction("Please write your first name");
return false;
} else {
return true;
}
}
}, {
fieldName: 'last-name',
fieldLabel: 'Last name',
inputType: 'text',
visible: true,
}, {
fieldName: 'terms',
fieldLabel: 'I accept the terms and conditions',
inputType: 'checkbox',
visible: true,
saveToProfile: false,
validate: function(value, errorFunction) {
if (value) {
return true;
} else {
errorFunction('You must accept the terms and conditions.');
return false;
}
}
}]
});
我加入了角色字段我的用户模式:
Schemas.User = new SimpleSchema({
username: {
type: String,
// For accounts-password, either emails or username is required, but not both. It is OK to make this
// optional here because the accounts-password package does its own validation.
// Third-party login packages may not require either. Adjust this schema as necessary for your usage.
optional: true
},
emails: {
type: [Object],
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
services: {
type: Object,
optional: true,
blackbox: true
},
profile: {
type: Object,
optional: true,
blackbox: true
},
"first-name": {
type: String
},
"last-name": {
type: String
},
// Add `roles` to your schema if you use the meteor-roles package.
// Option 1: Object type
// If you specify that type as Object, you must also specify the
// `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
// Example:
// Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
// You can't mix and match adding with and without a group since
// you will fail validation in some cases.
roles: {
type: Object,
optional: true,
blackbox: true
}
});
现在我想立刻在我第一次运行我的项目,管理员创建一个用户角色并阻止任何其他人被创建后:
/*----------------------------------------------- #2 Create admin user ----------------------------------------------*/
/*Notes: Create an admin-type user if no users exist yet.*/
if (Meteor.users.find().count() === 0) { /*------------------------------------ If there are no users created yet*/
var users = [{
username: "admin",
name: "admin",
email: "[email protected]",
roles: ['admin']
}];
_.each(users, function(user) {
var id = Accounts.createUser({
username: user.username,
email: user.email,
password: "mypassword123",
profile: {
name: user.name
},
first-name: Me,
last-name: MeName
});
if (user.roles.length > 0) {
// Need _id of existing user record so this call must come
// after `Accounts.createUser` or `Accounts.onCreate`
Roles.addUsersToRoles(id, user.roles);
}
});
}
/*-------------------------------------------------------------------------------------------------------------------*/
/*Prevent non-authorized users from creating new users*/
Accounts.validateNewUser(function(user) {
var loggedInUser = Meteor.user();
if (Roles.userIsInRole(loggedInUser, ['admin'])) {
return true;
}
throw new Meteor.Error(403, "Not authorized to create new users");
});
到目前为止显然非常好:我得到新用户。
问题是,当我使用spacebars隐藏在HTML中创建的用户不被识别为一个管理员,他们都向我隐瞒管理功能...
{{#if isInRole 'admin'}}
<p>Exclusive to admin stuff</p>
{{/if}}
答
如果使用角色作为一个对象(选项#1),你必须为所有用户指定一个组和许可(我相信Roles 2.0即将推出,这将不再是这种情况),因此对于像管理员用户那样的用户,您可以使用Roles.GLOBAL_GROUP跨所有组应用一揽子权限。
对于这一点,你需要做如下改变:
Roles.addUsersToRoles(id, user.roles);
要这样:
Roles.addUsersToRoles(id, user.roles, Roles.GLOBAL_GROUP);
您还需要指定isInRole助手里面的组,这里有一个例子如何看起来如下:
Roles.addUsersToRoles(joesUserId, ['manage-team'], 'manchester-united.com')
//manchester-united.com is the group
对于你的客户端isInRole帮手,你可以使用这个:
{{#if isInRole 'manage-team' 'manchester-united.com'}}
<h3>Manage Team things go here!</h3>
{{/if}}
您当前正在使用它作为字符串(选项#2,没有组)。如果您计划为任何用户使用组,那么您需要进行上述解释(您也可以删除选项#2),但是如果您不打算为任何用户使用组,则可以删除选项#1,并将其用作字符串。
如果你创建一个如下面的模板助手并使用它,它会工作吗? Template.myTemplate.helpers({ isAdmin(){ 返回Roles.userIsInRole(Meteor.userId(), '管理员');} }); 对不起,格式不好,不知道发生了什么。 –
编号仍然隐藏它为管理员用户以及。 – mesosteros
我刚刚在控制台上运行了以下两个测试:'''Meteor.roles.findOne({}) Object {_id:“yDn27e2z4GeHwpdRe”,name:“admin”}''' 和'''Meteor。userId() “FQzPJE8Q68ZzwnBqh”'''显然我的流星userId没有被正确分配给角色 – mesosteros