Javascript MeteorJS用户帐户:核心和meteor角色
我正在使用MeteorJS开发简单的web应用程序。我是MeteorJs的新手。我使用了useraccounts:core软件包和alanning的meteor角色。注册新用户时是否可以将角色分配给用户 编辑1 我尝试过使用onCreateUser钩子,但有些东西不起作用Javascript MeteorJS用户帐户:核心和meteor角色,javascript,authentication,meteor,Javascript,Authentication,Meteor,我正在使用MeteorJS开发简单的web应用程序。我是MeteorJs的新手。我使用了useraccounts:core软件包和alanning的meteor角色。注册新用户时是否可以将角色分配给用户 编辑1 我尝试过使用onCreateUser钩子,但有些东西不起作用 Accounts.onCreateUser(function(options, user){ var role = ['unselected']; Roles.addUsersToRoles(user, role);
Accounts.onCreateUser(function(options, user){
var role = ['unselected'];
Roles.addUsersToRoles(user, role);
return user;
});
您可能想使用OnCreateUser钩子:我有这种方法来创建用户(如果我了解您的示例,您已经有一些用户,只需要一些角色,因此当前用户只需创建这个),还可以使用一些自定义的登录按钮或类似的方法 Server.js
Meteor.methods({
createUsers: function(email,password,roles,name){
var users = [{name:name,email:email,roles:[roles]},
];
.each(users, function (user) {
var id;
id = Accounts.createUser({
email: user.email,
password: password,
profile: { name: user.name }
});
if (user.roles.length > 0) {
Roles.addUsersToRoles(id, user.roles);
}
});
},
deleteUser : function(id){ ///Some Delete Method (ignore if dont needed)
return Meteor.users.remove(id);
},
});
//publish roles
Meteor.publish(null, function (){
return Meteor.roles.find({})
})
Meteor.publish("Super-Admin", function () {
var user = Meteor.users.findOne({_id:this.userId});
if (Roles.userIsInRole(user, ["Super-Admin"])) {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
}
this.stop();
return;
});
Meteor.publish("Admin", function () {
var user = Meteor.users.findOne({_id:this.userId});
if (Roles.userIsInRole(user, ["Admin"])) {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
}
this.stop();
return;
});
Meteor.publish(null, function (){
return Meteor.roles.find({})
})
Template.registrar.helpers({
users: function () {
return Meteor.users.find();
},
email: function () {
return this.emails[0].address;
},
roles: function () {
if (!this.roles) return '<none>';
return this.roles.join(',');
}
});
发布方法
Meteor.methods({
createUsers: function(email,password,roles,name){
var users = [{name:name,email:email,roles:[roles]},
];
.each(users, function (user) {
var id;
id = Accounts.createUser({
email: user.email,
password: password,
profile: { name: user.name }
});
if (user.roles.length > 0) {
Roles.addUsersToRoles(id, user.roles);
}
});
},
deleteUser : function(id){ ///Some Delete Method (ignore if dont needed)
return Meteor.users.remove(id);
},
});
//publish roles
Meteor.publish(null, function (){
return Meteor.roles.find({})
})
Meteor.publish("Super-Admin", function () {
var user = Meteor.users.findOne({_id:this.userId});
if (Roles.userIsInRole(user, ["Super-Admin"])) {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
}
this.stop();
return;
});
Meteor.publish("Admin", function () {
var user = Meteor.users.findOne({_id:this.userId});
if (Roles.userIsInRole(user, ["Admin"])) {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1, roles: 1}});
}
this.stop();
return;
});
Meteor.publish(null, function (){
return Meteor.roles.find({})
})
Template.registrar.helpers({
users: function () {
return Meteor.users.find();
},
email: function () {
return this.emails[0].address;
},
roles: function () {
if (!this.roles) return '<none>';
return this.roles.join(',');
}
});
因此在客户端Client/register.html上
<template name="register">
<form id="register-form" action="action" >
<input type="email" id="register-email" placeholder="Nombre Nuevo Usuario">
<input type="password" id="register-password" placeholder="Password">
<select id="register-rol" class="form-control">
<option value="Admin" selected>Admin</option>
<option value="Super-Admin" selected>Super Admin</option>
<option value="Normal" selected>Normal</option>
</select>
<input type="submit" value="Register">
</form>
</tempalate>
删除部分(html)这是可选的,也可以查看帐户创建是否正确
{{#each users}}
<li id="user"><h6>{{email}}</h6><h6>{{roles}}</h6></li>
<button id="deleteUser" class="btn btn-danger btn-xs" > Borrar Usuario {{email}} </button>
{{/each}}
希望此帮助为混乱的代码感到抱歉以下方法必须仅在服务器端运行。提示是,您需要首先创建用户,从创建中获取id,然后向其附加角色
Meteor.methods({
rolesCreateUser: function (user) {
if (_.isObject(user)) {
if (user.username) {
var id = Accounts.createUser({
username: user.username,
email: user.email,
password: user.password
});
//We add roles to the user
if (user.roles.length > 0) {
Roles.addUsersToRoles(id, user.roles);
}
_.extend(user, {id: id});
return user;
}
}
}
});
然后在客户端使用用户数据调用该方法:
Meteor.call('rolesCreateUser', newUserData, function (error, newCreatedUser) {
if (error) {
//The error code
} else {
//Do something with newCreatedUser
}
});
是的,我试过了。我已经发布了代码编辑,但有些东西不起作用。我想你需要在用户创建后再做。我想知道这是否有效:var newUser=Accounts.onCreateUser(函数(选项,用户){returnuser;});变量角色=['unselected'];Roles.addUsersToRoles(newUser,role);TypeError:Accounts.onCreateUser不是函数请记住,这应该在服务器端运行。服务器端再次出现错误。错误:缺少“users”参数。感谢您的时间和帮助。