【问题标题】:Firebase Rules validation and catching validation fail in android applicationFirebase 规则验证和捕获验证在 android 应用程序中失败
【发布时间】:2017-07-05 05:54:24
【问题描述】:

设计

  • 可以登录应用程序的用户
  • 登录用户可以创建客户,这些客户将存储在node 下,其值将是当前登录的用户ID

当前数据库值

{
  "customers" : {
    "UserId1" : {
      "custId1" : {
        "customerCode" : "thk",
        "customerLimit" : "58866",
        "customerName" : "Test New "
      },
      "custId2" : {
        "customerCode" : "thh",
        "customerLimit" : "5698",
        "customerName" : "Yeth"
      }
    },
    "UserId2" : {
      "custId3" : {
        "customerCode" : "thh",
        "customerLimit" : "5886",
        "customerName" : "Test "
      },
      "custId4" : {
        "customerCode" : "tbh",
        "customerLimit" : "58669",
        "customerName" : "New Test"
      }
    }
  }
}

以下是当前为firebase 中的customers 表定义的规则。

{
  "rules": {
    "customers":{
      ".read": "auth != null", 
      ".write": "auth != null", 
      "$CID":{
            "customerName":{
                ".validate": "newData.isString() && newData.val().length < 100"
            },
            "customerCode":{
                ".validate": "newData.isString() && newData.val().length<4 && !newData.exists()"
            },
            "customerLimit":{}
      }
    }   
  }
}

尽管我已经为customerCode 定义了一条规则,即它的值应该小于 4 个字符,但它允许插入 5 个字符。另外,如何添加一个验证来验证每个 customerCode 的唯一性 userId?截至目前,它有!newData.exists(),但它也不起作用。

谁能指导我正确的方向?

【问题讨论】:

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


    【解决方案1】:

    我认为您最多可以插入 5 个字符,因为您的 .length 和您的 newData.val().lenght 从 0 开始,例如 array。换句话说,如果您想为每个 userId 链接 customerCode 的唯一性,您应该在您的 firebase 数据库中创建一个新表,例如:

    {
      "customers": {
        "UserId1": {
          "custId1": {
            "customerCode": "customerId1",
            "customerLimit": "58866",
            "customerName": "Test New "
          },
          "custId2": {
            "customerCode": "customerId2",
            "customerLimit": "5698",
            "customerName": "Yeth"
          }
        },
        "UserId2": {
          "custId3": {
            "customerCode": "customerId3",
            "customerLimit": "5886",
            "customerName": "Test "
          },
          "custId4": {
            "customerCode": "customerId4",
            "customerLimit": "58669",
            "customerName": "New Test"
          }
        }
      }
      "customerCode": {
        "custId1": {
          "customerId1": "thh"
        }
        "custId2": {
          "customerId2": "thk"
        }
        "custId3": {
          "customerId3": "thh"
        }
        "custId4": {
          "customerId4": "tbh"
        }
      }
    }
    

    使用此模型,您可以将规则修改为如下所示:

    {
      "rules": {
        "customers": {
          ".read": "auth != null",
          ".write": "auth != null",
          "$CID": {
            "customerName": {
              ".validate": "newData.isString() && newData.val().length < 100"
            },
            "customerCode": {
              //Code shouls be string, less than 4 characters and it can't be already stored on customerCode table for the current userId
              ".validate": "newData.isString() && newData.val().length<3  && !root.child('customerCode').child(auth.uid).exists()"
            },
            "customerLimit": {}
          }
        }
        "customerCode": {
          "$CID": {
            "CustomerCodeId": {
              ".read": "auth != null",
              ".write": "auth != null",
            }
          }
        }
      }
    

    也许上一个示例中的规则并不完美,但我相信您可以通过查看 firechat database rules 找到如何将其融入您的应用程序的方法。通过查看该存储库,我学到了很多东西。

    【讨论】:

    • 首先,非常感谢您抽出宝贵的时间。但是,从0开始的数组没有问题。它也接受10个字符。没有任何其他解决方法可以添加我可以根据userID 检查customerCode 的唯一性的规则,因为我觉得额外的链接表是多余的......
    • 很抱歉没有帮到你,反正你看看我上面给你链接的那个仓库,firebase rules的条件很多的就是一个很好的例子。关于您的表,如果您检查唯一的 CustomerCode 是否与唯一用户相关联,那么在数据级别将更简单且成本更低,而不是检查整个数据库的任何用户是否与 CustomerCode 相关联。另一种方法是将用户已在应用中使用的所有CustomerCodes 存储在某个位置,并在新用户想要创建代码时检查代码是否已经存在
    • 我可以从您的建议中获得一些想法,以创建独特的 customerCode.. 感谢您的链接和 +1 的努力。.. :) 我会尝试通过您提供的链接解决这个问题,如果我可以.. :)
    • 别担心,伙计,自从 Google 将 Firebase 用作平台以来,每个月都会有很多新功能,但有些功能仍然没有太多示例或文档。希望回答对你有点帮助。此外,如果您愿意,请查看下一个有关 Firebase 数据库结构的链接:airpair.com/firebase/posts/structuring-your-firebase-data祝您有美好的一天,如果您找到解决方案,请发布您的答案!对更多的人真的有很大的帮助!
    • 当然,伙计..我会发布解决方案,一旦我完成了这个.. :) 你也有美好的一天.. :) 快乐编码..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 2018-01-24
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多