【问题标题】:Firebase: Access to different sections depending on userFirebase:根据用户访问不同的部分
【发布时间】: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


    【解决方案1】:

    我想出了一种方法来为每个用户获取每个商店的变量名称。

    import UIKit
    import Firebase
    
    let shopVar = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let userReference = DataService.instance.REF_USERS.child((user?.uid)!).child("shop")
            userReference.observe(.value, with: { (snapshot) in       
    
            if let dict = snapshot.value as? [String:AnyObject] {
    
                    let shopName = dict["name"] as! String
                    print(shopName)
    
                    self.shopVar = shopName
    
                }
    
            }, withCancel: nil)
    }
    

    规则改变

    "users": {
           "K4of4ofp32kfoE": {
             "shop": {
               "name": "coolShop"
             }
          },
          "fj3i53ogF34oGf329ieQ": {
             "shop": {
               "name": "fancyShop"
             }
          },
    }
    

    然后我将shopVar -> someIDforShop 替换为shopFetch()

    现在每个用户都会收到他/她自己的“商店”物品。

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 2021-08-21
      • 1970-01-01
      • 2021-06-24
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      相关资源
      最近更新 更多