[英]Allow actions based on Meteor.user() porperties?
我想允許基於用戶屬性在我的Meteor集合中添加和刪除。
這是我設置管理員用戶的方式:
if (Meteor.isServer) {
if (Meteor.users.find().count() === 0) {
Accounts.createUser({
username:'Greg',
password:'default',
isAdmin: 1
});
}
}
我現在想允許每個具有isAdmin = true的用戶通過Accounts.createUser創建另一個用戶:
Meteor.methods({
makeUser: function(attributes) {
var user = Meteor.user();
if (user.isAdmin)
Accounts.createUser(attributes)
else
console.log('User ' + user.username + ' created a player.')
}
})
永遠不會創建用戶,就好像user.isAdmin永遠不等於true。 我究竟做錯了什么? 這與發布和訂閱有關嗎? 目前,我仍處於自動發布狀態。
將標記isAdmin
添加到配置文件對象:
Accounts.createUser({
username:'Greg',
password:'default',
profile:{
isAdmin: 1
}
});
查看文件
Accounts.createUser
方法允許僅將字段username
, password
, email
和profile
到用戶對象。
Meteor.methods({
makeUser: function(attributes) {
var user = Meteor.user();
if (user.profile && user.profile.isAdmin)
Accounts.createUser(attributes)
else
console.log('User ' + user.username + ' created a player.')
}
})
考慮使用包角色 。
在那種情況下,普通用戶仍然可以調用Accounts.createUser
並完全繞過makeUser
來創建用戶,我認為這不是您想要看到的行為。 我建議使用@Kuba Wyrobek的isAdmin邏輯包裝Accounts.onCreateUser
:
// server side
Accounts.onCreateUser(function(options, user) {
user.profile = options.profile ? options.profile : {};
if (user.profile && user.profile.isAdmin) {
return user;
} else {
throw new Meteor.Error(403, "Forbbiden");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.