【发布时间】:2016-11-11 13:48:10
【问题描述】:
这是我关于如何允许特定用户访问他们分配的“商店”对象的方法。
func shopFetch() {
self.shop.removeAll()
let shopRef = FIRDatabase.database().reference().child("shop").child(someIDforShop).child("menu")
shopRef.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let something1 = dictionary["some1"]
let something2 = dictionary["some2"]
let shopper = Shop(some1: something1, some2: something2)
self.shop.insert(shopper, at: 0)
self.collection.reloadData()
}
}, withCancel: nil)
}
这些是我的安全规则
rules: {
"users": {
"$uid": {
".read": "auth != null || auth.uid == $uid",
".write": "auth != null && auth.uid == $uid",
"$yourShop": {
".read": "true"
}
},
"shop": {
"$yourShop": {
".read": "root.child('users').child(auth.uid).child($yourShop).val() == true",
".write": "root.child('users').child(auth.uid).child($yourShopManager).val() == true || root.child('users').child(auth.uid).child('isAdmin').val() == true"
}
},
}
假设我的数据库中有这个:
<app-name>: {
"shop": {
"shop1": {
"menu": {
"some1": "thing1",
"some2": "thing2"
}
},
"shop2": {
"menu": {
"some1": "thing1",
"some2": "thing2"
}
},
"shop3": {
"menu": {
"some1": "thing1",
"some2": "thing2"
}
},
"users": {
"K4of4ofp32kfoE": {
"shop": {
"shop1": true
}
},
"fj3i53ogF34oGf329ieQ": {
"shop": {
"shop2": true
}
},
}
let shopRefsomeIDforShop 应该写什么?
这样做,将允许每个用户访问他/她的“商店”。我尝试使用childByAutoID(),假设它将按照规则中的规定获取与用户相关的数据。但是当我向print(snapshot) 询问时,shopFetch() 什么也没返回
【问题讨论】:
标签: swift firebase firebase-realtime-database firebase-security