gpt4 book ai didi

meteor - 如何将 Meteor 帐户创建限制为管理员?

转载 作者:行者123 更新时间:2023-12-01 11:41:17 24 4
gpt4 key购买 nike

这可以分为两部分:

1) 如何指定一个账号为管理员?

这就是我现在正在做的事情,但没有用。

Meteor.startup(function () {
if (Meteor.users.find().count() === 0) {
console.log("Adding fake data");
Accounts.createUser({username:"admin", email:"admin@admin.com", password:"1234", admin: true, profile:{name:"Administrator"}});
}

用户的“admin”属性不起作用。我不确定将其放入配置文件是否正确...这里有什么建议吗?

2) 如何限制只有管理员才能创建用户?

这就是我要做的,但它也不起作用

Meteor.users.allow({
insert: function(userId, doc) {
// only admin and create
return (userId && Meteor.users(userId).admin);
},

最佳答案

你可以这样做:

服务器端代码:

Meteor.methods({
createUser:function(username, email, password, name) {
if(Meteor.user() && Meteor.user().admin === true) { //You'll have to customize this to how you want it

return Accounts.createUser({
username: username,
email: email,
password: password,
profile: {
name: name
}
});
}else{
console.log("not logged in or not an admin");
}
},
makeMeAdmin: function() {
//You can customize this to have a password or something this is just an example
Meteor.users.update({_id: this.userId}, {$set:{admin:true}});
}
});

客户端代码:

让自己成为管理员:

Meteor.call("makeMeAdmin");

创建用户:

Meteor.call("createUser", "username", "email@email.com", "password123", "Bob Bob");

关于meteor - 如何将 Meteor 帐户创建限制为管理员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20449001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com