Firebase 规则与 Cloud Firestore 一起



Firebase 的设置乘法规则。

包含 3 个数据库集合的示例。

云修复

在收集国家/地区的 Firebase 中,应允许所有用户读取和写入。

在 Firebase 收集汽车时,只允许管理员编写。

在飞机的 firebase 集合中,允许所有经过身份验证的用户写入。

不工作文档: https://firebase.google.com/docs/rules/basics#cloud-firestore

如何用正确的语法设置规则?

// All public to include countries
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true ; 
allow write: if true ;
}
}
// check cars collection
match /databases/{database}/documents/Cars {
// For attribute-based access control, Check a boolean `admin` attribute
allow read: if true ;
allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
}
// check airplanes collection
match /databases/{database}/documents/Airplanes {
// Allow only authenticated content owners access
match /{database}/{userId}/{documents=**} {
allow read: if true ; 
allow write: if request.auth.uid == userID
}
}
}

你的规则中有一些错误。

  1. 您有一个语句,允许每个人编写每个文档。当有多个匹配语句与当前请求匹配,并且其中一个语句允许该请求时,最终判定为 ALLOW。去掉花纹:
match /{document=**} {
allow read: if true ; 
allow write: if true ;
}
  1. >Fi恢复区分大小写。为避免错误,请使用一致的命名对流,如驼峰大小写或pascal_case。

  2. 您必须在匹配语句的末尾添加一个文档匹配变量

这应该有效:

service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read: if true;
allow write: if request.auth != null && request.auth.uid == userId;
}
match /cars/{carId} {
allow read: if true ;
allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
}
match /airplanes/{airplane} {
allow read: if true ; 
allow write: if request.auth != null ;
}
}
}

最新更新