Firestoreでのアクセス制御
ドキュメントのパスによってアクセス制御出来る
例:
code:rule
match /{collection1}/{prefix}/{collection2}/{document=**} {
allow create:
if collection2 == "foo_dev" && request.auth != null;
allow read, write:
if collection2 == "foo_dev" && request.auth.token.get("dev-admin", false) == true;
allow create:
if collection2 == "bar_prod" && request.auth != null
allow read, write:
if collection2 == "bar_prod" && request.auth.token.get("prod-admin", false) == true;
}
placeholderやリクエストTokenの属性などからpermissionを制御出来る. read, write, writeの中でcreate, update等制御出来る
コレクションをネストして記述することも出来る
例:
code:rule
match /{collection1} {
match /{prefix} {
match /{collection2} {
match /{userId} {
allow create:
if request.auth != null
allow read, write:
if request.auth.uid == userId || request.auth.token.sub == userId
match /{document=**} {
allow read, write:
if request.auth.uid == userId || request.auth.token.sub == userId
}
}
}
}
}
この場合はリクエストUIDがパス中のuserIdに一致しているもしくはsub (ここではユーザuuid) がパスに一致しているかをチェックしている