对于Firebase规则,为什么在uid级别拒绝权限?

时间:2020-06-28 00:02:22

标签: swift firebase firebase-realtime-database firebase-security

我在控制台中收到此错误

[Firebase / Database] [I-RDB038012] / people / cQEVsQGXS3VJQzPBU4nL1HLx0Kq2处的侦听器失败:Permission_denied

以下是我的规则。由于我收到了控制台消息,我有一个错误可能是uid的想法,但是我不确定。我使用了Firebase文档来尝试弄清楚如何编写规则。

{
"rules": {
  "people" : {
    "$uid":  {
      "Education" :{
         ".read": "auth.uid != null" 
        ,".write": "$uid == auth.uid"
      }
      ,"Coordinates" : {
         ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }  
      ,"ForReUpload": {
         ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }
      ,"PhotoPosts": {
         ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }
        
      ,"WhatIamConsideringBuying":  { 
         ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }
      ,"caption": {
        ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }
      ,"peopleWhoLike" : {
        ".read": "$uid == auth.uid",
        ".write": "auth.uid != null"
      }
      ,"peopleWhoLike2" : {
         ".read": "auth.uid != null",
        ".write": "auth.uid != null"
      }
     
      ,"postID" : { 
         ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }
      ,"users" : {     
         ".read": "$uid == auth.uid",
       ".write": "$uid == auth.uid"  
      }
    }
  }
  }
}

这是我的数据库的JSON:

"people" : {
"1ZWT7FAE2qThNQfBj7tbMO7BnMo1" : {
  "Coordinates" : {
    "latitude" : 50.054738,
    "longitude" : 8.226809826085624
  "peopleWhoLike2" : {
    "1vLVFwrXrHUoakmDrnQKwbv08Yj1" : 1581548952597,
    "F9NX0UCG4fVHCKFk2VZ1NZKsLro2" : 1586210112155,
    "IrrBgFY9C1ekMmHUkQRzc5LhbDu1" : 1581547417432,
    

触发查询:

    let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid
    
    refArtists = Database.database().reference().child("people");
    
    refArtists.observe(DataEventType.value,  with: {snapshot in
        
        if snapshot.childrenCount>0{
            
            self.people.removeAll()
            
            for people in snapshot.children.allObjects as! [DataSnapshot] {
                
                    if people.key != thisUsersUid {
                        print("peoplekey",people.key)
                        

               
                    let peopleObject = people.value as? [String: AnyObject]
                    let peopleEducation = peopleObject?["Education"] as? String
                      let locCoord = CLLocation(latitude: lat, longitude: lon)
                      let distance = locCoord.distance(from: self.dict)
                            print(distance, "distancexy")




....
   self.people.append(peopl)
  .....

   self.people.sort { ($0.distance ?? 0) < ($1.distance ?? 0) }

           print("aaaaaaaa", self.people.map {$0.distance})
                
           self.table.reloadData()

            }
        }

1 个答案:

答案 0 :(得分:0)

您正在尝试从/people/cQEVsQGXS3VJQzPBU4nL1HLx0Kq2进行读取,但是您的规则不会授予任何人对该路径的读取权限。

我认为您正在寻找这些简单得多的规则:

{
  "rules": {
    "people" : {
      "$uid":  {
        ".read": "auth.uid != null",
        ".write": "$uid == auth.uid"
      }
    }
  }
}

如果只想允许某些子节点,通常会使用验证规则。像这样:

{
  "rules": {
    "people" : {
      "$uid":  {
        ".read": "auth.uid != null",
        ".write": "$uid == auth.uid",
        "Education" :{ ".validate": true },
        "Coordinates" : { ".validate": true },
        "ForReUpload": { ".validate": true },
        "PhotoPosts": { ".validate": true },
        "WhatIamConsideringBuying":  { ".validate": true },
        "caption": { ".validate": true },
        "peopleWhoLike" : { ".validate": true },
        "peopleWhoLike2" : { ".validate": true },     
        "postID" : { ".validate": true },
        "users" : { ".validate": true },
        "$other" : { ".validate": false },
      }
    }
  }
}

以上内容允许所有已命名的子节点,但拒绝所有其他子节点(由于$other规则)。