【发布时间】:2016-08-09 13:00:36
【问题描述】:
这是数据库结构。
这是数据库规则:
{
"rules": {
".read": true,
"shops-product-list": {
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth !== null && auth.uid === $uid",
}
}
}
}
shops-products-list 的键是登录用户的 uid(忽略第一个键,即 -1)
登录用户尝试使用此逻辑写入新数据。
private void addShopVariant(String pid, String vid, String mrp, String sellPrice) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
final String uid = getUid();
String key = pid + "_" + vid;
//String key = databaseReference.child("shops-product-list").child(uid).push().getKey();
ShopProductVariant shopProductVariant = new ShopProductVariant(pid, vid, mrp, sellPrice);
Map<String, Object> shopProductVariantValues = shopProductVariant.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/shops-product-list/" + uid + "/"+ key, shopProductVariantValues);
//childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
databaseReference.updateChildren(childUpdates);
}
如果我设置规则,功能就可以正常工作。
{
"rules": {
".read": true,
".write": true
}
}
但我希望只有登录用户才能写入,也只有他们自己的数据。
Firebase 规则有什么问题?
【问题讨论】:
-
乍一看,这些规则似乎应该有效。您可以进一步减少代码以仅使用常量值吗?例如:
getUid()返回什么?那真的是登录用户吗?另请注意,updateChildren()带有一个完成侦听器,它可能会告诉您有关操作失败原因的更多信息。
标签: firebase firebase-realtime-database firebase-security