如何正确设置 Firestore 安全规则
How to setup Firestore security rules correctly
虽然看起来很简单,但我仍在努力设置一些基本的 Firestore 规则,这些规则没有按预期工作。
对于下面发布的场景和查询,使用此数据库:
场景一
无法找出数据库名称?我以为是 restaurants
,但根据这个假设,下面的代码不起作用,并得到 PERMISSION_DENIED
异常:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if database == "restaurants";
}
}
}
场景二
Collection restaurants 有 10 个文档,如上图所示,我已经硬编码了这 10 个,并允许它们如下所示读写,但它不起作用,并且出现相同的 PERMISSION_DENIED
异常:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read : if document in ['2uFMIc2BSH6oslxEABpB','8GMNVxVUb1HzRAk2QmmX',
'AryyMURod8AeWAfBVavF','AvnpKGMeUWDSfowFLpwa','H0qi7gI8WmSVobu19G49',
'H2xhfoj0Rn75BH9nnbuI','NRfgppqWRfj3DtHDlft4','R9gZ0hTxCPXwSeV2prNV',
'YgXXwndeIfZx6rUhdlc4','v22FlL7LBBY851N8sIvQ'] ;
allow write : if document in ['2uFMIc2BSH6oslxEABpB','8GMNVxVUb1HzRAk2QmmX',
'AryyMURod8AeWAfBVavF','AvnpKGMeUWDSfowFLpwa','H0qi7gI8WmSVobu19G49',
'H2xhfoj0Rn75BH9nnbuI','NRfgppqWRfj3DtHDlft4','R9gZ0hTxCPXwSeV2prNV',
'YgXXwndeIfZx6rUhdlc4','v22FlL7LBBY851N8sIvQ'] ;
}
}
}
这2个可能不是很实际的场景,但更适合我的理解。
在您的第一个示例中,database
是您的数据库的名称,可能类似于“(默认)”。
在您的第二个示例中,document
将成为您文档的完整路径;这就是 =**
通配符的作用——它是一种 "everything else in my path" 通配符。所以它等于 restaurants/2uFMIc2BSH6oslxEABpB
如果你想创建一个规则,说 "A user can read any document in my restaurants collection" 你想要这样的东西:
service cloud.firestore {
match /databases/{database}/documents {
match /restaurants/{restaurantID} {
allow read, write: if true;
}
}
}
如果您想对您的各个餐厅的文档 ID 做一些有趣的事情,您可能想做一些更像这样的事情:
service cloud.firestore {
match /databases/{database}/documents {
match /restaurants/{restaurantID} {
allow read, write: if restaurantID == '2uFMIc2BSH6oslxEABpB; ;
}
}
}
虽然看起来很简单,但我仍在努力设置一些基本的 Firestore 规则,这些规则没有按预期工作。
对于下面发布的场景和查询,使用此数据库:
场景一
无法找出数据库名称?我以为是 restaurants
,但根据这个假设,下面的代码不起作用,并得到 PERMISSION_DENIED
异常:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if database == "restaurants";
}
}
}
场景二
Collection restaurants 有 10 个文档,如上图所示,我已经硬编码了这 10 个,并允许它们如下所示读写,但它不起作用,并且出现相同的 PERMISSION_DENIED
异常:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read : if document in ['2uFMIc2BSH6oslxEABpB','8GMNVxVUb1HzRAk2QmmX',
'AryyMURod8AeWAfBVavF','AvnpKGMeUWDSfowFLpwa','H0qi7gI8WmSVobu19G49',
'H2xhfoj0Rn75BH9nnbuI','NRfgppqWRfj3DtHDlft4','R9gZ0hTxCPXwSeV2prNV',
'YgXXwndeIfZx6rUhdlc4','v22FlL7LBBY851N8sIvQ'] ;
allow write : if document in ['2uFMIc2BSH6oslxEABpB','8GMNVxVUb1HzRAk2QmmX',
'AryyMURod8AeWAfBVavF','AvnpKGMeUWDSfowFLpwa','H0qi7gI8WmSVobu19G49',
'H2xhfoj0Rn75BH9nnbuI','NRfgppqWRfj3DtHDlft4','R9gZ0hTxCPXwSeV2prNV',
'YgXXwndeIfZx6rUhdlc4','v22FlL7LBBY851N8sIvQ'] ;
}
}
}
这2个可能不是很实际的场景,但更适合我的理解。
在您的第一个示例中,
database
是您的数据库的名称,可能类似于“(默认)”。在您的第二个示例中,
document
将成为您文档的完整路径;这就是=**
通配符的作用——它是一种 "everything else in my path" 通配符。所以它等于restaurants/2uFMIc2BSH6oslxEABpB
如果你想创建一个规则,说 "A user can read any document in my restaurants collection" 你想要这样的东西:
service cloud.firestore {
match /databases/{database}/documents {
match /restaurants/{restaurantID} {
allow read, write: if true;
}
}
}
如果您想对您的各个餐厅的文档 ID 做一些有趣的事情,您可能想做一些更像这样的事情:
service cloud.firestore {
match /databases/{database}/documents {
match /restaurants/{restaurantID} {
allow read, write: if restaurantID == '2uFMIc2BSH6oslxEABpB; ;
}
}
}