ios - Firestore 规则 - 如何允许创建但不允许更新?

标签 ios firebase google-cloud-platform google-cloud-firestore firebase-security

这是我写的;

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    match /reservedUsernames/{username} {
      allow update: if false;
      allow create: if request.auth != null;
    }
  }
}

我已经添加了一个 ID 为 sam 的文档和一个字段 userId = 122。如果我对该文档运行更新,请参阅下面的内容,它会成功!如何允许创建但不允许更新?

db.collection("reservedUsernames")
  .document(searchableUsername)
  .setData(["userId": userId])

最佳答案

我设法通过使用安全规则来做到这一点:

rules_version = '2'

service cloud.firestore {
  match /databases/{database}/documents {
    match /reservedUsernames/{documentId} {
      allow create: if request.auth != null && existingData(resource) == null
    }

    function incomingData(request) {
      return request == null || request.resource == null || request.resource.data == null ? null : request.resource.data
    }

    function existingData(resource) {
      return resource == null ? null : resource.data
    }
  }
}

通过这种方式,我可以检查是否正在更新现有文档,并且只有在没有更新的情况下才会通过!

关于ios - Firestore 规则 - 如何允许创建但不允许更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73505778/

相关文章:

ios - 由于错误 :,JSON 无法序列化

javascript - 如何从 Firebase 中的父节点获取所有子节点?

ios - 用户注册期间,Firebase将镜像上传到存储

kubernetes - 错误 : failed to discover supported resources kubernetes google cloud platform

postgresql - 我可以将数据从 CloudSQL 实时加载到 BigQuery 吗?

google-app-engine - Google 日历与 Google App Engine 的集成

objective-c - 获得 ASIFormDataRequest 响应后应用程序崩溃

iphone - 重复 self 执行选择器

iphone - 将 CLLocation 或 CLLocationCoordinate2D 转换为 CGPoint

firebase - 在 firebase 规则推送 Id 中添加字符串并检查相等性