用户waitOn数据订阅:
waitOn: function(){
if(Meteor.user()){
var current_user_admin_status = Meteor.user().admin;
console.log(current_user_admin_status);
return Meteor.subscribe('users', current_user_admin_status);
}
}但是current_user_admin_status总是返回null,即使我可以进入db并找到admin字段。
db.users.find({"_id" : "6Mqx5Ky92bZfhaX8A"}, {"admin" : 1})
{ "_id“:"6Mqx5Ky92bZfhaX8A","admin”:true }
我在客户机上定义了这个变量,它传递给服务器,只在当前用户是管理员的情况下发布用户集合,但是它总是被null if else语句捕获
Meteor.publish('users', function(current_user_admin_status){
if(!this.userId){
console.log('you are not signed in');
}
else if( current_user_admin_status = 'null' ){
console.log('you are not an admin');
} else if ( current_user_admin_status = 'false' ) {
console.log('you are not an admin');
} else if (current_user_admin_status = 'undefined'){
console.log('you are not an admi');
} else {
console.log('you are logged in as an admin');
return Meteor.users.find({}, {fields: {createdAt: 1, admin: 1, emails: 1, username: 1, first_name: 1, last_name: 1}});
}
});发布于 2015-05-01 00:05:53
这里有几个要点值得注意。首先,关于问题注释,向添加管理字段是不安全的,除非您添加了特定的allow/deny规则来防止对其进行编辑。请参阅这篇文章中的第一点。默认情况下,user.profile字段是由其用户编辑的,通常您应该将敏感字段(如admin )存储在它之外。
其次,您没有在客户端上接收user.profile.admin的最可能原因是您没有发布它。您应该首先为登录用户创建一个发布,以便将注释中注意到的默认字段以外的字段发送给用户。
最后,有一个出色的和广泛使用的处理授权包,roles,它已经解决了上面提到的安全问题。我建议您使用它:https://github.com/alanning/meteor-roles
https://stackoverflow.com/questions/29975316
复制相似问题