Can't write to Firebase database using rules - firebase

I'm trying to create a rule that allows some users to write but not all.
I need that all user can read 'menu' items but only users listed at store data can write.
My data structure:
{
"category" : [ null, "Burger", "Drinks" ],
"menu" : [ null, {
"available" : true,
"category" : "1",
"description" : "item1 description",
"image" : "chicken_maharaja",
"name" : "New Chicken Maharaja",
"price" : 1300,
"store" : 1
}, {
"available" : true,
"category" : "1",
"description" : "item2 description",
"image" : "big_spicy_chicken_wrap",
"name" : "Big Spicy Chicken Wrap",
"price" : 120,
"store" : 1
}, {
"available" : true,
"category" : "2",
"description" : "item3 description",
"image" : "thumsup",
"name" : "Thumsup 100ml",
"price" : 40,
"store" : 1
}, {
"available" : true,
"category" : "2",
"description" : "item4 description",
"image" : "mccafe_ice_coffee",
"name" : "Ice Coffee",
"price" : 140,
"store" : 1
}, {
"available" : true,
"category" : "1",
"description" : "item5 description",
"image" : "mc_chicken",
"name" : "MC Chicken",
"price" : 190,
"store" : 1
}, {
"available" : true,
"category" : "2",
"description" : "item6 description",
"image" : "Smoothie",
"name" : "Smoothie",
"price" : 70,
"store" : 2
}, {
"available" : true,
"category" : "1",
"description" : "item8 description",
"image" : "salad_wrap",
"name" : "Salad Wrap",
"price" : 150,
"store" : 2
} ],
"stores" : [ null, {
"location" : "Campinas - Taquaral",
"name" : "Store 1",
"user" : {
"pyixsRTw9qdiuESt62YnmEYXQt13" : true
}
}, {
"location" : "São Paulo - Perdises",
"name" : "Store 2",
"user" : {
"LBNZ8Dwp2rdJtlSh0NC1ApdtbAl2" : true,
"TLomOgrd3gbjDdpDAqGiwl0lBhn2" : true
}
} ],
"userProfile" : {
"LBNZ8Dwp2rdJtlSh0NC1ApdtbAl2" : {
"birthDate" : "1974-02-10",
"email" : "asd#asd.com",
"firstName" : "João",
"lastName" : "Silva"
},
"pyixsRTw9qdiuESt62YnmEYXQt13" : {
"birthDate" : "1974-02-10",
"email" : "leandro.garcias#gmail.com",
"firstName" : "Leandro",
"lastName" : "Garcia"
}
}
}
My rule:
{
"rules": {
"menu": {
"$items": {
".read": "true",
".write": "root.child('stores').child('1').child(data.child('user').val()).hasChild(auth.uid)"
}
},
"stores": {
"$store": {
".read": "true",
".write": "root.child('stores').child('$store').child(data.child('user').val()).hasChild(auth.uid)"
}
}
}
}
The read is ok. :-) But I can't write.

Your newData doesn't have a child user so that check always fails. You probably mean:
"43268522": {
"menu": {
"$items": {
".read": "true",
".write": "root.child('stores').child('1').child('user').hasChild(auth.uid)"
}
}
You're probably looking for this rule:
".write": "
root.child('stores')
.child(newData.child('store').val())
.child('user')
.hasChild(auth.uid)"
So this uses the store property from the new data to look up if the current user is in the store they're trying to modify.
Unfortunately this rule won't work with your current data structure, since the value of store is a number, while the key of a store is a string: "1" !== 1.
The simplest solution is to store the store as a string, e.g.:
"store": "1"
You might want to consider that anyway, since you're now getting Firebase's array coercion, which is not helpful. For more on this see our blog post on Best Practices: Arrays in Firebase. I'd recommend storing stores using either push IDs, or simply prefixing them, e.g.
"stores": {
"store1": {
...
}
}

Related

Converting a painless script into a visualisation on Kibana (Logs from AWS Connect)

I have logs being shipped from AWS Connect to Kibana through AWS OpenSearch. I have written the following script to return the latest status of an Agent like so:
GET agent-logs-*/_search
{
"script_fields": {
"data": {
"script": {
"lang": "painless",
"source": "params._source.CurrentAgentSnapshot.Configuration.Username + ', ' + params._source.CurrentAgentSnapshot.AgentStatus.Name + ', ' + params._source.EventTimestamp"
}
}
},
"collapse": {
"field": "CurrentAgentSnapshot.Configuration.Username.keyword"
},
"sort": [
{
"EventTimestamp": {
"order": "desc"
}
}
]
}
This returns a value of:
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 65,
"successful" : 65,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [
{
"_index" : "agent-logs-2022-06-28",
"_type" : "_doc",
"_id" : "",
"_score" : null,
"fields" : {
"data" : [
"al.pacino#email.com, Available, 2022-06-28T10:52:01.238Z"
],
"CurrentAgentSnapshot.Configuration.Username.keyword" : [
"al.pacino#email.com"
]
},
"sort" : [
1656413521238
]
},
{
"_index" : "agent-logs-2022-06-28",
"_type" : "_doc",
"_id" : "",
"_score" : null,
"fields" : {
"data" : [
"robert.deniro#email.com, Available, 2022-06-28T10:50:45.622Z"
],
"CurrentAgentSnapshot.Configuration.Username.keyword" : [
"robert.deniro#email.com"
]
},
"sort" : [
1656413445622
]
},
{
"_index" : "agent-logs-2022-06-26",
"_type" : "_doc",
"_id" : "",
"_score" : null,
"fields" : {
"data" : [
"marlon.brando#email.com, Offline, 2022-06-26T14:51:55.203Z"
],
"CurrentAgentSnapshot.Configuration.Username.keyword" : [
"marlon.brando#email.com"
]
},
"sort" : [
1656255115203
]
}
]
}
}
I wanted to take the data lines from the JSON - "al.pacino#email.com, Available, 2022-06-28T10:52:01.238Z" and represent this in a visualisation such as a Data Table to get a list of agents with their corresponding status.
By using the current agent-logs, there is a delay whereby the status change and heart beats overlap, causing an inaccurate count of the status, thus need to use this script.

Elasticsearch low indexing speed

I have a blog that contains 14k posts and tried to add these posts to the elastic search index.
I indexed some of the posts, but it's extremely slow, and it will take about 6 hours to estimate. All the performance optimization tips from the official site I made. In my opinion, I removed the redundant data such as post meta. Can I increase indexing speed? Add the index configuration below:
{
"test-post-1" : {
"aliases" : { },
"mappings" : {
"date_detection" : false,
"properties" : {
"ID" : {
"type" : "long"
},
"guid" : {
"type" : "keyword"
},
"menu_order" : {
"type" : "long"
},
"permalink" : {
"type" : "keyword"
},
"post_content" : {
"type" : "text"
},
"post_date" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss"
},
"post_excerpt" : {
"type" : "text"
},
"post_id" : {
"type" : "long"
},
"post_mime_type" : {
"type" : "keyword"
},
"post_modified" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss"
},
"post_name" : {
"type" : "text",
"fields" : {
"post_name" : {
"type" : "text"
},
"raw" : {
"type" : "keyword",
"ignore_above" : 10922
}
}
},
"post_parent" : {
"type" : "long"
},
"post_status" : {
"type" : "keyword"
},
"post_title" : {
"type" : "text",
"fields" : {
"post_title" : {
"type" : "text",
"analyzer" : "standard"
},
"raw" : {
"type" : "keyword",
"ignore_above" : 10922
},
"sortable" : {
"type" : "keyword",
"ignore_above" : 10922,
"normalizer" : "lowerasciinormalizer"
}
}
},
"post_type" : {
"type" : "text",
"fields" : {
"post_type" : {
"type" : "text"
},
"raw" : {
"type" : "keyword"
}
}
}
}
},
"settings" : {
"index" : {
"mapping" : {
"total_fields" : {
"limit" : "5000"
},
"ignore_malformed" : "true"
},
"number_of_shards" : "1",
"provided_name" : "test-post-1",
"max_shingle_diff" : "8",
"max_result_window" : "1000000",
"creation_date" : "1582745447768",
"analysis" : {
"filter" : {
"shingle_filter" : {
"max_shingle_size" : "5",
"min_shingle_size" : "2",
"type" : "shingle"
},
"edge_ngram" : {
"min_gram" : "3",
"side" : "front",
"type" : "edgeNGram",
"max_gram" : "10"
},
"ewp_word_delimiter" : {
"type" : "word_delimiter",
"preserve_original" : "true"
},
"ewp_snowball" : {
"type" : "snowball",
"language" : "russian"
}
},
"normalizer" : {
"lowerasciinormalizer" : {
"filter" : [
"lowercase",
"asciifolding"
],
"type" : "custom"
}
},
"analyzer" : {
"ewp_lowercase" : {
"filter" : [
"lowercase"
],
"type" : "custom",
"tokenizer" : "keyword"
},
"shingle_analyzer" : {
"filter" : [
"lowercase",
"shingle_filter"
],
"type" : "custom",
"tokenizer" : "standard"
},
"default" : {
"filter" : [
"ewp_word_delimiter",
"lowercase",
"stop",
"ewp_snowball"
],
"char_filter" : [
"html_strip"
],
"language" : "russian",
"tokenizer" : "standard"
}
}
},
"number_of_replicas" : "1",
"uuid" : "cWGjSF4FQ1Or0A_0oSlA2g",
"version" : {
"created" : "7050299"
}
}
}
}
}
Wordpress version: 5.3.2
Elasticsearch version: 7.5.2
Enabled plugins: ElasticPress

Firebase authentication with strings

I am wondering if it's possible to do the firebase authentication on strings? I have seen it being done on keys like -XDhabsuduydvtest <- random test key.
Now i have this structure:
{
"conversations" : {
"person1" : {
"person2" : {
"0" : 0,
"last_read" : 1460377167535
}
},
"person2" : {
"person1" : {
"0" : 0,
"last_read" : 1460377125515
}
}
},
"messages" : {
"eb-uuid-729fc0ac-49b8-48fa-a2ff-f2ada67b9e4c-person1" : {
"body" : "Dit is een test vanuit iOS",
"date" : 1460383996536,
"sender" : "eb-uuid-729fc0ac-49b8-48fa-a2ff-f2ada67b9e4c"
},
"person1-person2" : {
"-KF49xHKqnDawywJ9H0b" : {
"body" : "test message",
"date" : 1460375639190,
"sender" : "person1"
},
"-KF4Fc1KHH31O4_l0QWh" : {
"body" : "new",
"date" : 1460377125013,
"sender" : "person2"
}
}
},
"users" : {
"eb-uuid-729fc0ac-49b8-48fa-a2ff-f2ada67b9e4c" : {
"chat_enabled" : true,
"email" : "test#gmail.com",
"first_name" : "John",
"last_name" : "Smith",
"provider" : "password",
"push_muted" : false,
"push_token" : "some-push-token",
"uid" : "729fc0ac-49b8-48fa-a2ff-f2ada67b9e4c",
"username" : "johnsmith"
},
"person1" : {
"first_name" : "person",
"last_name" : "one"
},
"person2" : {
"first_name" : "person",
"last_name" : "two"
}
}
}
plus some security rule i tried, obviously i'd like to check if the uuid's are the same, and not only if one exists
{
"rules": {
"messages": {
"$conv":{
//Person can only read a conversation he/she is participating in
".write": true,
".read": true,
".indexOn": ["date"]
}
},
"users":{
"$ebuuid":{
".write": "root.child('users').child($ebuuid).hasChild('uid')",
".read": "root.child('users').child($ebuuid).hasChild('uid')"
}
},
"conversations":{
".write": "data.child('uid').val() === auth.uid",
".read": true
}
}
}
(Replaced images with text)
I'm really not sure how to go from here now .. the user has some uid but i'm not sure how to check it with whether he or she can post messages etc ..

meteor sort find() how?

I would like to make a ToDo list webapp in meteor, where users could define their own goals and sort them, so the higher a goal is, the bigger it's priority becomes.
I have two Collections:
meteor:PRIMARY> db.goals.find()
{ "_id" : "ZDCwfD4pWfD7sZ9DB", "gtext" : "Money", "createdAt" : ISODate("2015-12-26T13:15:59.563Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 0 }
{ "_id" : "B9oKRGssNWhjz57dP", "gtext" : "Procrastination", "createdAt" : ISODate("2015-12-26T13:16:06.272Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 3 }
{ "_id" : "famqG5vby9wBo37Ns", "gtext" : "Housework", "createdAt" : ISODate("2016-01-01T15:01:02.256Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 2 }
{ "_id" : "QKWJjTEwZQs5ErrPR", "gtext" : "Getting fit", "createdAt" : ISODate("2016-01-01T16:10:38.302Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 1 }
{ "_id" : "ByNyg9uRBH35XXgdi", "gtext" : "test1", "createdAt" : ISODate("2016-01-06T13:40:15.224Z"), "owner" : "JWdrC3bFgmHXHeDFf", "order" : 0 }
meteor:PRIMARY> db.tasks.find()
{ "_id" : "gNuyxHdyA6HN8JFsd", "text" : "clean up", "createdAt" : ISODate("2015-12-27T11:12:46.186Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "famqG5vby9wBo37Ns", "postponedate" : "", "checked" : false, "relatedgoal": { "order": 2, "_id": "famqG5vby9wBo37Ns" }}
{ "_id" : "6dEkaETWAkhsELP8r", "text" : "Sell something", "createdAt" : ISODate("2015-12-27T11:12:48.669Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "ZDCwfD4pWfD7sZ9DB", "postponedate" : "", "relatedgoal": { "order": 0, "_id": "ZDCwfD4pWfD7sZ9DB"} }
{ "_id" : "nCKz9oCkiExz4ekfK", "text" : "Win the lottery", "createdAt" : ISODate("2015-12-27T11:12:51.294Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "ZDCwfD4pWfD7sZ9DB", "postponedate" : "", "relatedgoal": { "order": 0, "_id": "ZDCwfD4pWfD7sZ9DB" } }
{ "_id" : "YvWxaxWM9qvGzkpEK", "text" : "read a magazine", "createdAt" : ISODate("2015-12-27T11:12:41.526Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "B9oKRGssNWhjz57dP", "postponedate" : "" "relatedgoal": { "order": 3, "_id": "B9oKRGssNWhjz57dP" }}
{ "_id" : "s53vn9tf8C5tyy27q", "text" : "task1", "createdAt" : ISODate("2016-01-06T13:39:36.673Z"), "owner" : "JWdrC3bFgmHXHeDFf", "notes" : "", "priority" : "ByNyg9uRBH35XXgdi", "postponedate" : "" }
{ "_id" : "bEWosRJK4ekAvSuJ6", "text" : "do some situps", "createdAt" : ISODate("2016-01-08T15:47:41.047Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "QKWJjTEwZQs5ErrPR", "relatedgoal": { "order": 1, "_id": "QKWJjTEwZQs5ErrPR" }}
My ultimate goal is to be able to sort the tasks by the order field of the related goal.
I'm using perak/meteor-joins to join the tasks collection with the related goal's oder field.
Now my problem is that I cannot get my find() to sort the goals by the oder field.
client.js:
Meteor.subscribe('Tasks');
Meteor.subscribe('Goals');
Template.body.helpers({
priorityTasks: function() {
return Tasks.find(
{postponedate: {$exists: false }},
{sort: {'relatedgoal.order': -1}}
)
}
});
I even tried, this but did not help either:
priorityTasks: function() {
return Tasks.find(
{postponedate: {$exists: false }},
{sort: {relatedgoal.order: 1}}
)
}),
Is there a way to sort the tasks by their order field?
I was thinking about a transform in the find method, but that's not reactive. I need a reactive solution.
Server.js:
Tasks = new Mongo.Collection("tasks");
Goals = new Mongo.Collection("goals");
Tasks.join(Goals, "priority", "relatedgoal", ["order"]);
Meteor.publish('Tasks',function() {
return Tasks.find({ owner: this.userId});
});
Meteor.publish('Goals',function() {
return Goals.find({ owner: this.userId});
});
Sortable.collections = ['goals'];
todo.html
<div class="container-fluid col-md-6">
<ul>
{{#each priorityTasks}}
{{> task}}
{{/each}}
</ul>
</div>
<template name="task">
<li>
<span class="text">
<a href="#modal-5" data-toggle="modal">
<strong>{{text}} - {{relatedgoal.order}}</strong>
</a>
<br>
{{postponedate}}
</span>
</li>
</template>
priorityTasks: function() {
return Tasks.find(
{
postponedate: {$exists: false }
}, {
sort: {'relatedgoal.order': 1}})
},
should work

querying elasitcsearch parent child documents

We work with two types of documents on elastic search (ES): items and slots, where items are parents of slot documents.
We define the index with the following command:
curl -XPOST 'localhost:9200/items' -d #itemsdef.json
where itemsdef.json has the following definition
{
"mappings" : {
"item" : {
"properties" : {
"id" : {"type" : "long" },
"name" : {
"type" : "string",
"_analyzer" : "textIndexAnalyzer"
},
"location" : {"type" : "geo_point" },
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"activityIndexAnalyzer" : {
"alias" : ["activityQueryAnalyzer"],
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textIndexAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["word_delimiter_impl", "trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textQueryAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop"]
}
},
"filter" : {
"spanish_stop" : {
"type" : "stop",
"ignore_case" : true,
"enable_position_increments" : true,
"stopwords_path" : "analysis/spanish-stopwords.txt"
},
"spanish_synonym" : {
"type" : "synonym",
"synonyms_path" : "analysis/spanish-synonyms.txt"
},
"word_delimiter_impl" : {
"type" : "word_delimiter",
"generate_word_parts" : true,
"generate_number_parts" : true,
"catenate_words" : true,
"catenate_numbers" : true,
"split_on_case_change" : false
}
}
}
}
}
Then we add the child document definition using the following command:
curl -XPOST 'localhost:9200/items/slot/_mapping' -d #slotsdef.json
Where slotsdef.json has the following definition:
{
"slot" : {
"_parent" : {"type" : "item"},
"_routing" : {
"required" : true,
"path" : "parent_id"
},
"properties": {
"id" : { "type" : "long" },
"parent_id" : { "type" : "long" },
"activity" : {
"type" : "string",
"_analyzer" : "activityIndexAnalyzer"
},
"day" : { "type" : "integer" },
"start" : { "type" : "integer" },
"end" : { "type" : "integer" }
}
}
}
Finally we perform a bulk index with the following command:
curl -XPOST 'localhost:9200/items/_bulk' --data-binary #testbulk.json
Where testbulk.json holds the following data:
{"index":{"_type": "item", "_id":35}}
{"location":[40.4,-3.6],"id":35,"name":"A Name"}
{"index":{"_type":"slot","_id":126,"_parent":35}}
{"id":126,"start":1330,"day":1,"end":1730,"activity":"An Activity","parent_id":35}
I'm trying to make the following query: search for all items within a certain distance to a location that have children (slots) in the specified days and within certain start and end ranges.
An item with more slots fulfilling the condition should score higher.
I tried starting with existing samples but the docs are really scarce and its hard to move forward.
Clues?
I don't think there is a way to write an efficient query that would do something like this without moving location to slots. You can do something like this, but it can quite inefficient for some data:
{
"query": {
"top_children" : {
"type": "blog_tag",
"query" : {
"constant_score" : {
"query" : {
... your query for children goes here ...
}
}
},
"score" : "sum",
"factor" : 5,
"incremental_factor" : 2
}
},
"filter": {
"geo_distance" : {
"distance" : "200km",
"location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
Basically, what this query is doing is this, it takes your range query or filter for children and whatever other conditions you need and wraps it into constant_score query to make sure that all children have score of 1.0. The top_children query collects all these children and accumulates their scores to the parents. And then filter filters out parents that are too far away.

Resources