2017-02-16 20 views
2

设计火力地堡规则验证和追赶验证的Android应用程序失败

  • 谁可以登录到用户登录应用
  • 可以创建一个将其值将存储node可根据客户用户的当前登录用户ID中

当前DB值

{ 
    "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个字符。另外我如何添加一个验证,验证每个userId的唯一性customerCode?截至目前它有!newData.exists()但它不工作。

任何人都可以请指导我在正确的方向吗?

回答

1

我认为您最多可以插入5个字符,导致您的.lengthnewData.val().lenght从0开始,如array。在其他的方式,如果你想为每个用户id链接customerCode的独特性,你应该在你的火力点数据库中创建一个新表,喜欢的东西:

{ 
    "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

首先,非常感谢您的时间..但是,它没有问题,从数组开始'0' ..它也接受10个字符..没有任何其他的解决办法来添加规则,我可以检查因为我认为额外的链接表是冗余的,所以''customerCode'每个'userID'的唯一性... –

+1

对不起,它没有帮助你,无论如何,看看我上面链接你的存储库,这是一个很好的例子有很多'firebase规则'的条件。关于你的表,如果你检查一个唯一的'CustomerCode'是否与一个唯一的用户相关联,而不是检查你的整个数据库中的任何用户是否具有与'CustomerCode'相关联的关系,那么在数据级上它会更简单和更便宜。 另一种方式可以在某些地方存储您的用户已经在应用程序中使用的所有'CustomerCodes',并且只是在新用户想要创建代码时检查代码是否已经存在 –

+0

我可以通过您的建议获取一些想法创建独特的'customerCode' ..感谢您的努力的链接和+1 .. :)我会尽力通过您提供的链接解决这个问题,如果我可以的话.. :) –