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

查看:102
本文介绍了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"
}

同时,用户名对象仅包含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天全站免登陆