Firestore 内でドキュメントのセキュリティ ルールを設定するのに苦労しています。RTDB を使用すると、特定のオブジェクト プロパティにルールを設定することができ、Firestore でも同じことを実行しようとしています。
RTDBコード:
"users": {
".read": true,
".indexOn": ["profile/name"],
"$uid": {
".read": "auth != null",
".write":
"$uid === auth.uid && !data.exists()",
"profile": {
"birthday": {
".write": "$uid === auth.uid"
},
"name": {
".write": "$uid === auth.uid"
},
"banned": {
".write": "root.child('admins').child(auth.uid).exists()"
}
}
}
}
Firestore の同じコードは以下の通りです。
service cloud.firestore {
match /databases/{database}/documents {
match /users/ {
allow read
match /{$user} {
allow read: if request.auth.uid != null
allow write: if request.auth.uid == request.resource.id && exists(/databases/$(database)/documents/users/$(request.resource.id)) === false
match /birthday {
allow write: if request.auth.uid == request.resource.id
}
match /name {
allow write: if request.auth.uid == request.resource.id
}
match /banned {
allow write: get(/databases/$(database)/documents/users/$(request.auth.uid)).data.userType > 3
}
}
}
}
}
サブコレクションのセキュリティ ルールを記述しているときは正常に動作します。しかし、ドキュメント フィールドの場合は動作しません。これは不可能ですか、それともpath
一致参照に特別なセグメントがありますか?ドキュメントこれについては何も述べていません。
ベストアンサー1
これはプロパティをチェックすることで実行できますrequest.resource.data
。ドキュメンテーションドキュメント レベルのみを一致させる必要があります。条件を使用してフィールド ルールをチェックしますif
。
ただし、個々のフィールドへの読み取りアクセスを制御することはできません。ユーザーはドキュメント全体を読み取ることも、読み取らないこともできます。プライベート データを保存する必要がある場合は、ユーザー ドキュメントのサブコレクションに追加することを検討してください。
ここに例があります
service cloud.firestore {
match /databases/{database}/documents {
// Make sure all cities have a positive population and
// the name is not changed
match /cities/{city} {
allow update: if request.resource.data.population > 0
&& request.resource.data.name == resource.data.name;
}
}
}