2017-10-13 83 views
0

我的意思是,我知道這些是什麼,但是我有點迷惑有關安全規則,例如:Firestore規則,什麼是集合和文檔?

service cloud.firestore { 
    match /databases/{database}/documents { 
    // This is probably a mistake 
    match /spaceships { // <= what is this a collection or a document? 
     allow read; 
     // In spite of the above line, a user can't read any document within the 
     // spaceship collection. 
    } 
    } 
} 

火力地堡文件說:

規則集合並不適用於內的文檔那個集合。在集合級別而不是文檔級別寫入安全規則是不尋常的(也可能是錯誤)。

這意味着這match /spaceships {...是一個收集權?

但後來,我們有這樣的:

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /{document=**}{ // <= what is this a document or a collection? 
     allow read, write: if true; 
    } 
    } 
} 

我不明白的是這match /{document=**}{...文檔?還是集合?我的意思是在收集水平。

回答

1

路徑在公司的FireStore是交替的收藏和文件:/collection/document/subcollection/subdocument

例如:

// Matches kaylee, the mechanic on serenity 
/spaceships/serenity/crew/kaylee/... 

當使用安全規則,你可以指定通配符:

// This will match any spaceship, and any crewmember 
/spaceships/{spaceshipId}/crew/{crewmemberId}/... 

現在,假設你有spaceships下的另一個子集合:

/spaceships/{spaceshipId}/stowaways/{stowawayId}/... 

如果要編寫針對多個子集的規則,你需要:

// You could use multiple single wildcards 
/spaceships/{spaceshipId}/{crewOrStowaway}/{crewOrStowawayId}/... 

// Or you could use a multi-segment wildcard 
/spaceships/{spaceshipId}/{allShipInformation=**} 

這將返回allShipInformation作爲路徑,這將在和路徑下的匹配所有文件集合。請注意,它是一個或多個路徑段,而不是零個或更多。

你可以閱讀更多關於這個in the docs

0

在你的第一個例子是/spaceships在集合級別。正如您引用的報價中所述,在這裏放置一條規則是沒有用的,因爲它不會應用於集合中的任何文檔。

在第二個示例中/{document=**}處於集合級別,但使用的是recursive wildcard。簡而言之,這是將規則應用於此集合中的文檔以及此集合的任何子集合中的任何文檔。

這允許你寫:

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /{document=**}{ 
     allow read, write: if true; 
    } 
    } 
} 

相反的:

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /spaceships/{shipId} { 
     allow read, write: if true; 
    } 
    match /spaceships/{shipId}/crew/{crewMemberId} { 
     allow read, write: if true; 
    } 
    } 
}