我在firestore中有這組規則
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document} {
allow read: if true;
allow write: if isTrustworthy();
}
match /stores/{storeId}{
match /{document} {
allow read: if true;
allow write: if isTrustworthy();
}
match /departures/{departureId} {
allow read: if true;
allow write: if !(resource.data.locked);
}
}
function isTrustworthy(){
return isSignedIn() && emailVerified();
}
function isSignedIn() {
return request.auth != null;
}
function emailVerified() {
return request.auth.token.email_verified;
}
}
}
但是在出發路徑中設定、更新或洗掉時出現錯誤。
例如。
this.afs.collection('stores').doc(this.auth.store)
.collection('departures')
.doc(element.shipping.trackingNumber.toString())
.set({"test":true})
檔案不存在,但如果檔案存在,它也不會作業,獨立于引數“鎖定”我收到以下錯誤:
錯誤錯誤:未捕獲(承諾):FirebaseError:[code=permission-denied]:權限缺失或不足。
FirebaseError:權限缺失或不足。
我需要的是,如果檔案的引數“鎖定”設定為 true,則無法洗掉、更新或設定 {merge:true} 選項。
任何幫助都感激不盡。
uj5u.com熱心網友回復:
您的規則和代碼存在一些問題:
- 上
create,resource.data不存在 - On
update或delete,可能是該欄位locked不存在 - 當您用于
set在 Firestore 中創建或更新檔案時,您必須添加 options{merge: true}來更新檔案而不是在它已經存在的情況下覆寫它。
試試這個:
代碼:
this.afs.collection('stores').doc(this.auth.store) .collection('departures') .doc(element.shipping.trackingNumber.toString()) .set({"test":true}, {merge: true})規則:
match /departures/{departureId} { allow read: if true; allow create: if true; allow update, delete: if !resource.data.get("locked", false); }
uj5u.com熱心網友回復:
我在我的規則中添加了一組函式,以使撰寫這樣的規則更容易:
function existing() {
return resource.data;
}
function resulting() {
return request.resource.data;
}
function matchField(fieldName) {
return existing()[fieldName] == resulting()[fieldName];
}
resource指被訪問的檔案,request.resource指的是檔案的新資料。通過這些,您可以參考現有或傳入檔案中的值。
在matchField各種權利要求/角色保護是在組合有用部分被改變所述檔案的(一個或多個)。
在您的情況下,您可以檢查特定欄位“ locked”的狀態,例如某種形式:
match /whatever/points/to/the/doc {
allow write: if !existing().locked
}
(我主要使用幾十個這樣的函式來使我的規則幾乎看起來像文本)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/333923.html
