Firebase rules with number of childs varying
I'm building an angular firebase web app based on a chat. Simplified example of my structure:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?
This is what this part of the rules look now:
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).
EDIT:
Based on the example above, let's imagine a user user4 joined the room 1:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
And now a user write a message:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
The message must follow list of users.
firebase firebase-realtime-database firebase-security-rules
add a comment |
I'm building an angular firebase web app based on a chat. Simplified example of my structure:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?
This is what this part of the rules look now:
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).
EDIT:
Based on the example above, let's imagine a user user4 joined the room 1:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
And now a user write a message:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
The message must follow list of users.
firebase firebase-realtime-database firebase-security-rules
Did you try the rule you gave? Because that's basically how a$
variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under ausers
subnode though, just to make things more explicit.
– Frank van Puffelen
Nov 12 '18 at 19:32
Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03
Ah, that means that you still want to add anewData.hasChildren(...)
to the parent node's.validate
rule. I'll write up an answer.
– Frank van Puffelen
Nov 12 '18 at 20:16
add a comment |
I'm building an angular firebase web app based on a chat. Simplified example of my structure:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?
This is what this part of the rules look now:
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).
EDIT:
Based on the example above, let's imagine a user user4 joined the room 1:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
And now a user write a message:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
The message must follow list of users.
firebase firebase-realtime-database firebase-security-rules
I'm building an angular firebase web app based on a chat. Simplified example of my structure:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
I'm trying to write the rules ".write" for writing a new message (without cloud functions) that must have an author, a message and a number of users' id varying (here user1, user2, user3) which are here under /users (= a list of users varying). No problem with the author and the message but how can I do the part varying ? Is it even possible without cloud functions ?
This is what this part of the rules look now:
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
(if you have followed me so you should understand $other represents user1, user2 and user3 in my example).
EDIT:
Based on the example above, let's imagine a user user4 joined the room 1:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
And now a user write a message:
|__ messages
|__ room1
|__ msg1
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ msg2
|__ author: ""
|__ message: ""
|__ user1: "user1"
|__ user2: "user2"
|__ user3: "user3"
|__ user4: "user4"
|__ users
|__ room1
|__ user1: ...
|__ user2: ...
|__ user3: ...
|__ user4: ...
The message must follow list of users.
firebase firebase-realtime-database firebase-security-rules
firebase firebase-realtime-database firebase-security-rules
edited Nov 13 '18 at 9:01
asked Nov 12 '18 at 18:10
Curse
1531111
1531111
Did you try the rule you gave? Because that's basically how a$
variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under ausers
subnode though, just to make things more explicit.
– Frank van Puffelen
Nov 12 '18 at 19:32
Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03
Ah, that means that you still want to add anewData.hasChildren(...)
to the parent node's.validate
rule. I'll write up an answer.
– Frank van Puffelen
Nov 12 '18 at 20:16
add a comment |
Did you try the rule you gave? Because that's basically how a$
variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under ausers
subnode though, just to make things more explicit.
– Frank van Puffelen
Nov 12 '18 at 19:32
Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03
Ah, that means that you still want to add anewData.hasChildren(...)
to the parent node's.validate
rule. I'll write up an answer.
– Frank van Puffelen
Nov 12 '18 at 20:16
Did you try the rule you gave? Because that's basically how a
$
variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users
subnode though, just to make things more explicit.– Frank van Puffelen
Nov 12 '18 at 19:32
Did you try the rule you gave? Because that's basically how a
$
variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under a users
subnode though, just to make things more explicit.– Frank van Puffelen
Nov 12 '18 at 19:32
Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03
Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03
Ah, that means that you still want to add a
newData.hasChildren(...)
to the parent node's .validate
rule. I'll write up an answer.– Frank van Puffelen
Nov 12 '18 at 20:16
Ah, that means that you still want to add a
newData.hasChildren(...)
to the parent node's .validate
rule. I'll write up an answer.– Frank van Puffelen
Nov 12 '18 at 20:16
add a comment |
1 Answer
1
active
oldest
votes
You want a few things here:
- The node must have
user1
,user2
anduser3
properties. - The value of those properties must exist as a key under
/users
The relevant snippet of your rules:
"messages":
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",
"message":
".validate": "newData.isString()",
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Yeah, that I'd only see possible if you put all of them in a$msgid/users
node under each message, because then each child would have to exist under/users
too.
– Frank van Puffelen
Nov 12 '18 at 22:55
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267807%2ffirebase-rules-with-number-of-childs-varying%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want a few things here:
- The node must have
user1
,user2
anduser3
properties. - The value of those properties must exist as a key under
/users
The relevant snippet of your rules:
"messages":
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",
"message":
".validate": "newData.isString()",
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Yeah, that I'd only see possible if you put all of them in a$msgid/users
node under each message, because then each child would have to exist under/users
too.
– Frank van Puffelen
Nov 12 '18 at 22:55
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
add a comment |
You want a few things here:
- The node must have
user1
,user2
anduser3
properties. - The value of those properties must exist as a key under
/users
The relevant snippet of your rules:
"messages":
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",
"message":
".validate": "newData.isString()",
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Yeah, that I'd only see possible if you put all of them in a$msgid/users
node under each message, because then each child would have to exist under/users
too.
– Frank van Puffelen
Nov 12 '18 at 22:55
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
add a comment |
You want a few things here:
- The node must have
user1
,user2
anduser3
properties. - The value of those properties must exist as a key under
/users
The relevant snippet of your rules:
"messages":
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",
"message":
".validate": "newData.isString()",
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
You want a few things here:
- The node must have
user1
,user2
anduser3
properties. - The value of those properties must exist as a key under
/users
The relevant snippet of your rules:
"messages":
"$messageId":
".validate": "newData.hasChildren('author', 'message', 'user1', 'user2', 'user3')",
"author":
".validate": "newData.isString()",
"message":
".validate": "newData.isString()",
"$other":
".validate": "$other === newData.val() && root.child('users/'+newData.val()).exists()"
answered Nov 12 '18 at 20:19
Frank van Puffelen
227k28372397
227k28372397
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Yeah, that I'd only see possible if you put all of them in a$msgid/users
node under each message, because then each child would have to exist under/users
too.
– Frank van Puffelen
Nov 12 '18 at 22:55
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
add a comment |
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Yeah, that I'd only see possible if you put all of them in a$msgid/users
node under each message, because then each child would have to exist under/users
too.
– Frank van Puffelen
Nov 12 '18 at 22:55
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Maybe I wasn't clear enough sorry for that. As I said $other represents user1, user2 and user3; it was just an example, they aren't fixed childs but varying childs, please look at my edit I added an example.
– Curse
Nov 12 '18 at 20:40
Yeah, that I'd only see possible if you put all of them in a
$msgid/users
node under each message, because then each child would have to exist under /users
too.– Frank van Puffelen
Nov 12 '18 at 22:55
Yeah, that I'd only see possible if you put all of them in a
$msgid/users
node under each message, because then each child would have to exist under /users
too.– Frank van Puffelen
Nov 12 '18 at 22:55
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
I wanted to simplify my example but in reality you are absolutely right each message and each user are under a "room id". I updated it, let me know if it's still not clear !
– Curse
Nov 13 '18 at 7:28
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
Frank any idea ?
– Curse
Nov 19 '18 at 11:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267807%2ffirebase-rules-with-number-of-childs-varying%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you try the rule you gave? Because that's basically how a
$
variable works: the rules under that apply to all child nodes for which no explicit name match exists. I would typically put the users under ausers
subnode though, just to make things more explicit.– Frank van Puffelen
Nov 12 '18 at 19:32
Yes I did. The problem here is I can write a message with user1 and/or user2 and/or user3 or without all of them: this is the part missing checking that all of them are here.
– Curse
Nov 12 '18 at 20:03
Ah, that means that you still want to add a
newData.hasChildren(...)
to the parent node's.validate
rule. I'll write up an answer.– Frank van Puffelen
Nov 12 '18 at 20:16