【问题标题】:Firebase firestore security rule access path paramsFirebase Firestore 安全规则访问路径参数
【发布时间】:2018-05-27 05:30:23
【问题描述】:

考虑这条安全规则

service cloud.firestore {
  match /databases/{database}/documents {
    match /notifications/{user} {
      allow read, write: if request.auth.uid != null;
    }
    match /items/{item} {
      allow read: if true;
      allow write: if false
    }
  }
}

我想访问参数 {item} 的值。有没有办法做到这一点?对于云功能,它类似于“context.params.item”。对于 Firestore 安全规则,我需要类似的东西。

【问题讨论】:

    标签: javascript firebase google-cloud-firestore firebase-security


    【解决方案1】:

    根据documentation,该参数存储在resource.data变量下:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /notifications/{user} {
          allow read, write: if request.auth.uid != null;
        }
        match /items/{item} {
          allow read: if resource.data != null;
          allow write: if false
        }
      }
    }
    

    【讨论】:

    • 是的,我可以使用资源对象访问路径“/items/{item}”中的值。但我不想那样。例如,如果路径是“/items/23”,它可能包含一些数据,例如“{name: 'orange', price: 5}”。我可以通过资源对象轻松访问它。但我想知道路径的最后一部分,即值“23”。
    • 那么你可能想使用resource.id
    【解决方案2】:

    Writing conditions for Cloud Firestore Security Rules

    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure the uid of the requesting user matches name of the user
        // document. The wildcard expression {userId} makes the userId variable
        // available in rules.
        match /users/{userId} {
          allow read, update, delete: if request.auth != null && request.auth.uid == userId;
          allow create: if request.auth != null;
        }
      }
    }
    

    正如评论所解释的,在你的情况下,因为 {item} 被接受,只需在规则中使用 item。

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 2019-06-16
      • 2021-04-09
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多