Firebase规则通配符键

huangapple go评论66阅读模式
英文:

Firebase Rules wildcard Key

问题

我在论坛上搜索了答案,但没有找到一个。
我正在向Firebase数据库写入键值对,并希望设置规则。

这是一个聊天应用程序,我正在为每个他是成员的聊天添加一个字段,例如user/$uid/chat/$chat_id。

我正在使用以下方式创建chatId:

FirebaseDatabase.getInstance().getReference().child("chat").push().getKey();

我正在尝试添加某种验证,以便数据必须包含$chat_id: true。
但是我在尝试使用通配符时遇到了困难。

我写入的测试数据 -
"-M5STgMVWyafV-LhPIT2": "true",但我需要Key作为通配符,因为它每次都是随机的。

数据结构如下。

chat
	-M5STgMVWyafV-LhPIT2   // <<< $chat_id
		info
			admin: "zQvmGG2vPfTrdjZBfWP2ZotajYE2"
			groupName: "Some Group"
			id: "-MJD6cAtpxY_jmFyk-sb"
			no_of_users: 2
			timer: 60000
			users
				y1EKihfmIJTDCD9OJt0N89WDC642: true
				zQvmGG2vPfTrdjZBfWP2ZotajYE2: true
		messages
			-MJ0G0gdecak8ACR7fb2
				creator: "wjl8VNmcaPNHJAUQnCDRHWkJ33p2"
				date: "Oct 06, 2020"
				text: "Some Text"
				time: "23:13"
				userName: "Bob"

user
	zQvmGG2vPfTrdjZBfWP2ZotajYE2
		chat
			-M5STgMVWyafV-LhPIT2: true  // <<< $chat_id
			-MGNk6fl8jneDhw7Jv02: true
			-MGNkFGD4OYb7ZSzX8P2: true
			-MGNl47kbPuNZFc3RLD2: true
			-MGNyFIuhjrwyQT4VME2: true
			-MGNydtnFHW60i5TbzW2: true
			-MJ-ETJ-LcOxvYkZ8b72: true

		info
			email: "someemailaddress@me.com"
			name: "Bob"
			notificationKey: "be5df5fa-3a20-4704-986f-8a35d"
			profilePic: "https://"
			status: "Some status"
	zQvmGG2vPfTrdjZBfWP2ZotajYE3
	zQvmGG2vPfTrdjZBfWP2ZotajYE4

我尝试过的规则,如果我将$chat_id指定为字符串,这些规则将起作用。

{
  "rules": {
    "user": { 
      ".read": "auth != null",
        ".indexOn": "info/email",
      "$user_id": {
          
           		"chat": {        					
                ".write": "auth != null",
                ".validate": "newData.child($chat_id).val() == 'true'", // <<< 这不起作用 未知变量 '$chat_id'。
                //".validate": "newData.child("-M5STgMVWyafV-LhPIT2").val() == 'true'",  <<< 这起作用,但不是$chat_id,我需要这个Child成为通配符

              },
               "info": { 
                //".indexOn": "email", 
                ".write": "auth != null && $user_id === auth.uid",
              },
         	}
   		},
    "chat": {
     //".write": "auth != null && !data.child('chat').child('$chat_id').exists()",
    "$chat_id": {
          "info": {
            ".read": "data.child('users').child(auth.uid).exists()",
            ".write": "auth != null && !data.exists()&&newData.child('admin').val()==auth.uid || data.child('users').child(auth.uid).exists()",
          },
          "messages": {
              "$message_id": {
                ".write": "root.child('chat').child($chat_id).child('info').child('users').child(auth.uid).exists()",
                ".read": "root.child('chat').child($chat_id).child('info').child('users').child(auth.uid).exists()",    
             }
        }
      }
    },
  }
}

任何帮助都将不胜感激。
谢谢

英文:

I've searched the forums for an answer to this but can't find one.
I'm writing a Key:value pair to firebase database and want to set rule.

It's a chat application, and I'm adding a field to my user for each chat he's a member of, eg user/$uid/chat/$chat_id

The chatId i'm creating using

FirebaseDatabase.getInstance().getReference().child(&quot;chat&quot;).push().getKey();

I'm trying to add some kind of validation so that the data has to contains $chat_id : true.
But I'm strugging to get the wildcard to work.

The test data I'm writing -
"-M5STgMVWyafV-LhPIT2": "true", but I need the Key to be a wildcard as it's random each time.

The data structure looks like this.

chat
	-M5STgMVWyafV-LhPIT2   // &lt;&lt;&lt; $chat_id
		info
			admin: &quot;zQvmGG2vPfTrdjZBfWP2ZotajYE2&quot;
			groupName: &quot;Some Group&quot;
			id: &quot;-MJD6cAtpxY_jmFyk-sb&quot;
			no_of_users: 2
			timer: 60000
			users
				y1EKihfmIJTDCD9OJt0N89WDC642: true
				zQvmGG2vPfTrdjZBfWP2ZotajYE2: true
		messages
			-MJ0G0gdecak8ACR7fb2
				creator: &quot;wjl8VNmcaPNHJAUQnCDRHWkJ33p2&quot;
				date: &quot;Oct 06, 2020&quot;
				text: &quot;Some Text&quot;
				time: &quot;23:13&quot;
				userName: &quot;Bob&quot;

user
	zQvmGG2vPfTrdjZBfWP2ZotajYE2
		chat
			-M5STgMVWyafV-LhPIT2: true  // &lt;&lt;&lt; $chat_id
			-MGNk6fl8jneDhw7Jv02: true
			-MGNkFGD4OYb7ZSzX8P2: true
			-MGNl47kbPuNZFc3RLD2: true
			-MGNyFIuhjrwyQT4VME2: true
			-MGNydtnFHW60i5TbzW2: true
			-MJ-ETJ-LcOxvYkZ8b72: true

		info
			email: &quot;someemailaddress@me.com&quot;
			name: &quot;Bob&quot;
			notificationKey: &quot;be5df5fa-3a20-4704-986f-8a35d&quot;
			profilePic: &quot;https://&quot;
			status: &quot;Some status&quot;
	zQvmGG2vPfTrdjZBfWP2ZotajYE3
	zQvmGG2vPfTrdjZBfWP2ZotajYE4

And the rules I've tried, which work if I specify the $chat_id as a string.

{
  &quot;rules&quot;: {
    &quot;user&quot;: { 
      &quot;.read&quot;: &quot;auth != null&quot;,
        &quot;.indexOn&quot;: &quot;info/email&quot;,
      &quot;$user_id&quot;: {
          
           		&quot;chat&quot;: {        					
                &quot;.write&quot;: &quot;auth != null&quot;,
                &quot;.validate&quot;: &quot;newData.child($chat_id).val() == &#39;true&#39;&quot;, // &lt;&lt;&lt; this doesn&#39;t work Unknown variable &#39;$chat_id&#39;.
                //&quot;.validate&quot;: &quot;newData.child(&quot;-M5STgMVWyafV-LhPIT2&quot;).val() == &#39;true&#39;&quot;,  &lt;&lt;&lt; this works, but not $chat_id, I need this Child to be a wildcard

              },
               &quot;info&quot;: { 
                //&quot;.indexOn&quot;: &quot;email&quot;, 
                &quot;.write&quot;: &quot;auth != null &amp;&amp; $user_id === auth.uid&quot;,
              },
         	}
   		},
    &quot;chat&quot;: {
     //&quot;.write&quot;: &quot;auth != null &amp;&amp; !data.child(&#39;chat&#39;).child(&#39;$chat_id&#39;).exists()&quot;,
    &quot;$chat_id&quot;: {
          &quot;info&quot;: {
            &quot;.read&quot;: &quot;data.child(&#39;users&#39;).child(auth.uid).exists()&quot;,
            &quot;.write&quot;: &quot;auth != null &amp;&amp; !data.exists()&amp;&amp;newData.child(&#39;admin&#39;).val()==auth.uid || data.child(&#39;users&#39;).child(auth.uid).exists()&quot;,
          },
          &quot;messages&quot;: {
              &quot;$message_id&quot;: {
                &quot;.write&quot;: &quot;root.child(&#39;chat&#39;).child($chat_id).child(&#39;info&#39;).child(&#39;users&#39;).child(auth.uid).exists()&quot;,
                &quot;.read&quot;: &quot;root.child(&#39;chat&#39;).child($chat_id).child(&#39;info&#39;).child(&#39;users&#39;).child(auth.uid).exists()&quot;,    
             }
        }
      }
    },
  }
}

Any help would be apreciated.
Thanks

答案1

得分: 0

这个:

newData.hasChild('$chat_id')


意味着你正在检查新数据是否有一个字面上称为 `$chat_id` 的子节点。除了 `$` 在键中不被允许(因此这是不可能为真的,因为 `$` 不被允许用于键名),这也很可能不是你想要的。

如果你想要检查新数据在你的数据库中是否有一个与 `$chat_id` 相同值的子节点,就直接使用没有引号的 `$chat_id`。所以:

newData.hasChild($chat_id)

英文:

This:

newData.hasChild(&#39;$chat_id&#39;)

Means that you're checking if the new data has a child node that is literally called $chat_id. Aside from the fact that this can't be true (as $ is not allowed in keys), it is also likely not what you want.

If you instead want to check if the new data has a child with the same value as $chat_id in your database, use $chat_id without quotes. So:

newData.hasChild($chat_id)

答案2

得分: 0

我找到了正确的语法。我需要在写入函数后面添加变量,然后在变量后面放置验证。

以下是这个例子,以防对其他人有帮助。

      "$user_id": {
            "chat": {        					
                  ".write": "auth != null",
                "$chat_id":    {
                  ".validate": "$chat_id.length === 20 && newData.val() === 'member'",
                },
                  },
英文:

I figured out the correct syntax for this. I needed to add the variable after the write function, and then put the validate after the variable.

Here it is in case it helps anyone later.

      &quot;$user_id&quot;: {
            &quot;chat&quot;: {        					
                  &quot;.write&quot;: &quot;auth != null&quot;,
                &quot;$chat_id&quot;:    {
                  &quot;.validate&quot;: &quot;$chat_id.length === 20 &amp;&amp; newData.val() === &#39;member&#39;&quot;,
                },
                  },

huangapple
  • 本文由 发表于 2020年10月10日 01:47:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64284917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定