我正在研究一个学校项目,该项目是android上的交通应用程序。我试图以登录状态编辑另一个用户的用户信息。当我尝试编辑另一个用户的已保存变量时,出现
java.lang.IllegalArgumentException:无法保存一个ParseUser 未认证。
[在搜索中,我看到有人建议将ACL更改为对用户公开写以对其进行编辑,但我尝试了这一点,很遗憾,它没有做任何更改,我仍然遇到此错误。另一个建议是使用云代码或主密钥,但是我找不到任何显示如何实现它们的文档。如果有人帮助我,我会很高兴。非常感谢。
otherUser.save(null,{useMasterKey:true});
这里是使用云代码和主密钥的完整示例:
Parse.Cloud.define("saveOtherUser", async (request) => {
const otherUserID = request.params.otherUserID;//other user's ID;
const user = request.user; //This is you. We are NOT gonna update this.
//you can check your security with using this user. For example:
if(!user.get("admin")){
//we are checking if th requesting user has admin privaleges.
//Otherwise everyone who call this cloud code can change other users information.
throw "this operation requires admin privilages"
//and our cloud code terminates here. Below codes never run
//so other users information stays safe.
}
//We create other user
const otherUser = new Parse.User({id:otherUserID});
//Change variables
otherUser.set("variable","New Variable, New Value");
//Now we are going to save user
await otherUser.save(null,{useMasterKey:true});
//this is the response our android app will recieve
return true;
});
这是我们用于Android应用程序的Java代码:
HashMap<String, Object> params = new HashMap<>();
params.put("otherUserID", otherUser.getObjectId());
ParseCloud.callFunctionInBackground("saveOtherUser", params, new FunctionCallback<Boolean>() {
@Override
public void done(Boolean object, ParseException e) {
if(e==null&&object){
//save operation successful
}
else{
//save operation failed
}
}
});