实时数据库规则问题

时间:2020-08-21 12:37:48

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

拥有一个带有教练路径的数据库。尝试使用规则进行保护。因此,仅当它们的身份验证与用户登录时使用的身份验证匹配时,才允许阅读以下内容。如果我在规则游乐场进行测试,它将通过。

 {
      "rules": {
        "Coaches": {
          ".indexOn": "coachEmail",
            "$uid":{
               ".read": "$uid==auth.uid",
            }

但是在我的应用程序中它不起作用在logcat中得到错误 在/ Coachs上侦听失败:DatabaseError:权限被拒绝 可能是什么问题?查询数据库的代码中可能有错误?

//Query the database to get the current user
        Query query = databaseReference.orderByChild("coachEmail").equalTo(user.getEmail());
        query.addValueEventListener(new ValueEventListener() {

更新:

 "rules": {
    "Coaches": {
      ".indexOn": "coachEmail",
        "$uid":{
           ".read": "auth.uid == $uid",
                    ".write": "auth.uid == $uid" 
        }

这会通过规则游乐场

Type    read
Location    /Coaches/R6qhJIyLw9S6tI5llmuBhRvWWhn1
Data    null
Auth    { "provider": "anonymous", "uid": "R6qhJIyLw9S6tI5llmuBhRvWWhn1" }
Admin   false

每当我运行我的应用程序时,都会出现以下错误

Listen at /Coaches failed: DatabaseError: Permission denied

不确定是什么问题

1 个答案:

答案 0 :(得分:1)

Security rules do not filter your data以任何方式。它们只是确保根据您的规则允许您的代码对数据库执行的任何操作。

您的代码尝试加载 //Declaring boolean variables bool atPresent; bool periodPresent; bool spacePresent; string emailid = someemailfrom a list; atPresent = false; periodPresent = false; spacePresent = false; //looking for @ size_t foundAt = emailid.find('@'); if (foundAt != string::npos) { atPresent = true; } //looking for '.' size_t foundPeriod = emailid.find('.'); if (foundPeriod != string::npos) { periodPresent = true; } //looking for ' ' size_t foundSpace = emailid.find(' '); if (foundSpace != string::npos) { spacePresent = true; } //checking to see if all conditions match if ( (atPresent == false) && (periodPresent == false) && (spacePresent == true)) { cout << emailid << endl; } ,因此规则检查用户是否对/Coaches具有读取权限。由于不这样做,因此拒绝了读取操作。

当前规则唯一允许的读取操作是:

/Coaches

由于您要允许用户指定自己的电子邮件地址的查询,因此您也应该更新规则以检查此类查询。如securely querying data上的文档所示,类似于:

databaseReference.child(user.getUid())...