LoginSignup
1
0

More than 3 years have passed since last update.

firestoreのルールは存在しないレコードに対するクエリにも効く

Posted at

たとえば、対象となるレコードにclaimというフィールドがある場合、
そのCustomClaimがないとreadできないというようなルールを書くとする。

allow read :if request.auth.uid != null && ((!resource.data.keys().hasAll(['claim']) || request.auth.token[resource.data.claim] == true))

ここで上記のように書くと、大体のケースではうまくいくのだが、
存在しないレコードに対してクエリを投げると、権限不足でエラーになってしまう。
感覚的にはclaimというカラムがないのだから権限無しでもOKにして欲しいものだが、ruleの仕様上NGである。

対応策

    match /hoge/{hogeId} {
      allow read :if request.auth.uid != null && !exists(/databases/$(database)/documents/hoge/$(hogeId))
    }

上記のように、「存在しなければread許可」の条件を足してやればOK。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0