首页 > 解决方案 > 您如何调整存储规则以适应您的应用程序?

问题描述

前提:这些是目前的规则

       rules_version = '2';
   service firebase.storage {
 match /b/{bucket}/o {
   match /{allPaths=**} {
     allow read, write: if request.auth != null;
   }
 }
}

当我运行上述规则时,并非所有用户的图像都会显示。我不知道是不是因为所有用户都在一部手机上(我自己测试,注销,登录,冲洗并重复),但我对此表示怀疑。这些规则中有一些东西(特别是 auth!not nil 部分)使得并非所有图像都返回。我说这是因为使用以下规则,每个用户都会获得一张图片(尽管并非总是他上传的图片,因为规则允许任何内容,但它表明代码可以从 firebase 获取图片给所有用户)。

     rules_version = '2';
    service firebase.storage {
   match /b/{bucket}/o {
     match /{allPaths=**} {
      allow read, write;
    }
 }
 }

这是从 firebase 获取图像的代码。

class homepage: UITableViewController, CLLocationManagerDelegate{

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])   {
    let databaseRef = Database.database().reference()
    let uid = Auth.auth().currentUser!.uid
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
    latestLocation = ["latitude" : locValue.latitude, "longitude" : locValue.longitude]
    let lat = locValue.latitude
    let lon = locValue.longitude
    dict = CLLocation(latitude: lat, longitude: lon)
    print("dict", dict)
    if let locationDictionary = latestLocation {       databaseRef.child("people").child(uid).child("Coordinates").setValue(locationDictionary)   
    }  
}

......

public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return people.count
}

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
    let immy = cell.viewWithTag(1) as! UIImageView
    let person: Userx = people[indexPath.row]
    let like = cell.viewWithTag(3) as! UIButton
    cell.lblName.text = person.Education
    cell.postID = self.people[indexPath.row].postID
    if let PhotoPosts = person.PhotoPosts {
        let url = URL(string: PhotoPosts)
        immy.sd_setImage(with: url)
    }
    return cell
}

……

  override func viewDidLoad() {
    super.viewDidLoad()
    table.dataSource = self
    table.delegate = self
   ........      
        if snapshot.childrenCount>0{
            self.people.removeAll()
            for people in snapshot.children.allObjects as! [DataSnapshot] {   
                    if people.key != thisUsersUid {
                        print("peoplekey",people.key)
                    let peoplePhotoPosts = peopleObject?["PhotoPosts"]  as? String
                    let peopleimageDownloadURL = peopleObject?["imageDownloadURL"]  as? String

                            let peopl = Userx(............PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL, postID: peoplepostID, .........)

                    self.people.append(peopl)

                        } else {
                            print ("145454")
                        }
                                    } else {
                                                                                                   print ("alphaaa")                                                                                                                                       
                     }

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

……

extension UIImageView {
public func imageFromServerURL(urlString: String) {
    self.image = nil
    URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in

        if error != nil {
            print("error")
            return
        }
        DispatchQueue.main.async(execute: { () -> Void in
            let image = UIImage(data: data!)
            self.image = image
        })

    }).resume()
}}



  }

标签: swiftfirebasefirebase-storagefirebase-security

解决方案


推荐阅读