【问题标题】:Allow update on specific field on specific user - Firebase Firestore Rule允许更新特定用户的特定字段 - Firebase Firestore 规则
【发布时间】:2019-03-12 03:54:25
【问题描述】:

您好,我正在开发一个应用程序,其中有两种类型的用户,第一种是管理员,第二种是普通用户。现在我想允许更新普通用户文档中名为disable 的特定字段,只有管理员可以更新此字段。但另一个问题是我只想允许用户自己在文档字段中进行更新,例如if (uid == documentId) allow write in fields,因为我不希望其他用户只更新某人的帐户信息。我怎么可能执行这些事情,它们在规则中可用吗?

注意:将有两个 Collection AdminsUsers,其中每个文档 id 是帐户的 UID。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    首先,建议您在一个集合Users 下同时创建AdminsUsers,并为管理员帐户添加一个字段admin = true

    为了确保没有人可以更新文档,除非它是同一个用户帐户或管理员帐户,您可以使用以下规则:

    service cloud.firestore {
        function admin() {
            return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true
        }
        match /databases/{database}/documents {
            match /users/{userId} {
                  allow read: if true;
                  allow update, create: if admin() || (request.auth.uid == userId && !('admin' in request.writeFields));
                  allow delete: if request.auth.uid == userId || admin();
            }
        }
    }
    

    希望这有帮助,祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多