Firestore 中唯一属性的 firebase 规则 [英] firebase rule for unique property in firestore

查看:20
本文介绍了Firestore 中唯一属性的 firebase 规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个问题在这里比较常见,但我仍然不知道如何管理唯一属性的规则.我有以下文档数据模型:

I've found that question relatively often asked here, but i still cant figure out how to manage a rule for unique properties. I have following document datamodel:

users/{usereId}/Object
users/usernames/Object

第一个对象包含有关用户的基本信息,例如:

The first Object contains basic information about the user, like:

{
email: "example@hotmail.edu"
photoURL: null
providerId: null
role: "admin"
username:"hello_world"
}

同时,usernames 对象只包含 username 作为属性键和 uid 作为值,例如:

meanwhile the usernames objects only contains the username as the property key and the uid as the value, for instance:

{
hello_world:"F3YAm8ynF1fXaurezxaQTg8RzMB3"
}

我是这样设置的,因为我希望每个用户都有一个唯一的用户名.与遍历第一个对象相比,遍历第二个对象花费的时间更少.但是回到我的问题.我需要 hello_world 在写操作中是唯一的.但到目前为止我的规则不起作用.我有:

I set it up this way, because I want that every user has a unique username. And its less time consuming iterating through the second object than through the first ones. But back to my issue. I need that hello_world is unique within the write operation. But my rules so far does not work. I have:

service cloud.firestore {
  match /databases/{database}/documents {
  
    match /{document=**} {
      allow read, write: if request.auth.uid != null
    }
    
    match /users/{userID} {
        allow create: if !exists(/databases/$(database)/documents/users/$(request.resource.data.username)) <== does not apply
    }
    
  }
}

第二个匹配是,什么应该应用唯一属性规则.有人知道如何正确设置规则吗?

The second match is, what should apply the unique property rule. Has anyone an idea how to set the rule correctly?

推荐答案

您在 users 集合中创建了一个名为 usernames 的文档,以跟踪正在使用的名称.但是你的规则试图找到一个以当前用户名命名的文档,它永远不会存在于这个结构中.

You created a document called usernames in your users collection, to track the names that are in use. But your rules are trying to find a document named after the current user's name, which will never exist in this structure.

在您当前的结构中,您将需要这样的东西:

In your current structure, you will need something like this:

allow create: if get(/databases/$(database)/documents/users/usernames).data[$(request.resource.data.username)] == request.auth.uid

所以上面得到了保存所有用户名的文档,然后检查当前用户名是否在他们当前 UID 中.

So the above gets the document where you keep all the user names, and then checks if the current user name is in their for the current UID.

另一种方法是保留所有用户名的附加 集合,然后让其中的每个文档将单个用户名映射到一个 UID.在这种情况下,您的 /usernames 集合将是顶级的,因为它在所有用户之间共享.此规则与您目前拥有的规则更接近:

An alternative approach is to keep an additional colllection of all user names, and then make each document in there map a single user names to a UID. In this scenario your /usernames collection would be top-level, since it's shared between all users. The rules for this would be closer to what you currently have:

allow create: if !exists(/databases/$(database)/documents/usernames/$(request.resource.data.username))

这篇关于Firestore 中唯一属性的 firebase 规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆