Meteor 同时使用 alanning 角色和 aldeed collection2
Meteor using alanning roles and aldeed collection2 together
我在使用 meteor-collection2 的项目中设置角色时遇到问题。我假设这是 collection2 文档中提到的角色包。
我正在使用帐户密码和 ian:accounts-ui-bootstrap-3 作为我的帐户解决方案。这是我的配置:
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: "test@test.com",
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");
});
到目前为止一切顺利:我得到了新用户。
问题是当我使用空格键隐藏管理功能时 html 创建的用户不被识别为管理员并且他们对我隐藏了...
{{#if isInRole 'admin'}}
<p>Exclusive to admin stuff</p>
{{/if}}
如果使用 Roles 作为对象(选项 #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 并简单地将其用作字符串。
我在使用 meteor-collection2 的项目中设置角色时遇到问题。我假设这是 collection2 文档中提到的角色包。 我正在使用帐户密码和 ian:accounts-ui-bootstrap-3 作为我的帐户解决方案。这是我的配置:
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: "test@test.com",
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");
});
到目前为止一切顺利:我得到了新用户。
问题是当我使用空格键隐藏管理功能时 html 创建的用户不被识别为管理员并且他们对我隐藏了...
{{#if isInRole 'admin'}}
<p>Exclusive to admin stuff</p>
{{/if}}
如果使用 Roles 作为对象(选项 #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 并简单地将其用作字符串。