【问题标题】:Firestore rules. How to let users only modify themselves and certain values among themFirestore 规则。如何让用户只修改自己和其中的某些值
【发布时间】:2017-11-18 15:46:03
【问题描述】:

我目前正在为 android 制作一个简单的社交媒体应用程序,我刚开始使用 firebase,它需要吸收大量信息。但我在理解规则时遇到了问题。我的社交媒体应用程序有一个自定义货币,我不希望他们能够编辑其货币字段的值,例如出生日期、用户名、密码、电子邮件、性别等;即使在阅读了文档之后,我也完全不知道我应该做什么。非常感谢任何帮助。

这是我的尝试,但我不确定如何限制他们更新其个人资料的某些方面。

    service cloud.firestore {
    match /user_profiles/{profileid} {
         allow update: if request.resource.data.userId == request.auth.uid;
    }
}

【问题讨论】:

    标签: java android google-cloud-firestore


    【解决方案1】:

    你可以给那个“允许”添加一个函数:

      service cloud.firestore {
        match /user_profiles/{profileid} {
            allow update: if request.resource.data.userId == request.auth.uid && profileModel();
        }
        function profileModel() {
            // Currency must be the same value as before (avoid edition)
            return request.resource.data.currency == resource.data.currency
            // The new data provided must have "name" and "surname" properties
            && request.resource.data.keys().hasAll(["name", "surname"])
            // Some other possible restrictions
                     && request.resource.data.name is string
                     && request.resource.data.surname is string
                     // Age must be an integer
                     && request.resource.data.age is int
        }
      }
    

    您可以在Firestore Rules Reference 中查看许多其他可能性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多