代码之家  ›  专栏  ›  技术社区  ›  Christophe Le Besnerais

关于子集合的FielSt店规则

  •  3
  • Christophe Le Besnerais  · 技术社区  · 8 年前

    我在FiSt店有一个“游戏”集合,有一个“级别”子集合。我正在尝试设置安全规则,以便您只能访问您创建的游戏或级别。

    所有文件(游戏或关卡)都有 authorId 字段,其中包含创建它们的用户的uid。我试过这个规则,但还是有一个 Missing or insufficient permissions 错误:

    service cloud.firestore {
      match /databases/{database}/documents {    
        match /games/{document=**} {
            allow read, write: if document.data.authorId == request.auth.uid;
        }
      }
    }
    

    我错过了什么?

    我也尝试过以下规则,但没有成功:

    service cloud.firestore {
      match /databases/{database}/documents {    
        match /games/{game}/levels/{level} {
            allow read, write: if level.data.authorId == request.auth.uid;
        }
      }
    }
    

    service cloud.firestore {
       match /games/{game} {
         allow read, write: if game.data.authorId == request.auth.uid;     
    
           match /levels/{level} {
              allow read, write: if level.data.authorId == request.auth.uid;
           }
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Doug Stevenson    8 年前

    根据参考文献, resource 是包含用户试图写入的文档数据的对象。您使用它的 data 属性获取其字段值。

    service cloud.firestore {
      match /databases/{database}/documents {    
        match /games/{document=**} {
            allow read, write: if resource.data.authorId == request.auth.uid;
        }
      }
    }