How check first 5 characters of auth.uid in Firebase Realtime Database rules? - firebase-realtime-database

I check for the authenticated user like this:
".write": "$uid === auth.uid"
Is it possible to check for the first 5 characters of the uid instead?
".write": "$uid === auth.uid.first5Characters"

I don't think that's possible based on two full UID values. But even if it was, it would not be secure as there's a decent likeliness that two UIDs will start with the same 5 characters.
If you want to use a shorter unique code to identify users, consider assigning your own code - and using your own mechanism to ensure their uniqueness.

Related

Firebase realtime database : Does the user download the entire database every time?

I want the user to send some data to the database. But the downloaded data is a little high.
These are my rules:
{
"rules": {
".read": false,
".write": true
}
}
In one day, the downloaded data has become 7.2 mb. The size of my database is around 320kb. This is my code where I'm using it.
String id = mDatabase.child(username).push().getKey();
Record r = new Record(calendar.getTimeInMillis(),sensorValue);
mDatabase.child(username).child(id).setValue(r);
No, Firebase doesn't download all the data any time you start your application if you don't need it (depends on the data you are querying).
So the correct answer is that it depends on the code of your app. For example if you have code like
db.ref('users/user_id').once((snap) => { ... })
it means that only particular data, placed under user_id field is downloaded.
But if, for example, you write such code:
db.ref('users').once((snap) => { ... })
the whole users object will be downloaded.
Hope, now it is clear for you that bandwidth depends on how much data is queried from your app code perspective.
A common source of unexpected bandwidth usage in the Firebase Realtime Database (and in Cloud Firestore, Firebase's other database), is the fact that any data shown in the Firebase console also counts towards your quota.
So in your case, if you've kept the database open in the Firebase console, and refreshed it 10 times over the day, or if you've opened the Firebase Database console 10 times in a day, you're already well on your way to explaining the bandwidth usage that you have.

how to create a compopsite key in cloud firestore for document path that would be changing

I have a cloud firestore collection which am trying to reference in a stream builder like this:
stream:Firestore.instance.collection('htOne').document('path1')
.collection('path2').document('hats').collection('hat')
.where("${curntUser.isNotEmpty?curntUser:'me'}",isEqualTo: true).orderBy('timesent',descending: true).limit(1000).snapshots(),
The filed for the where clause is going to vary depending on the individual using the app.This would be different for everyone, which means that i'd have to create a composite key every time this filed is created and may not even know this field. Is there a way to get around this?
It looks like you have this style of field in your documents:
"users": {
"uid1": true,
"uid3": true,
"uid4": true,
"me": true
}
This used to be the recommended way to store a set of values (UIDs in your case), but that is no longer true. In fact, precisely because of the problem you are running into, Firestore now supports using arrays to store such sets of values. The equivalent in your case would be:
"users": ["uid1", "uid3", "uid4", "me"]
And you can then use the array-contains operation in your query:
.where("users", "array-contains", "${curntUser.isNotEmpty?curntUser:'me'}")
This operator was added in version 0.8 of FlutterFire, and is the solution to your problems.
Also see:
the blog post Better Arrays in Cloud Firestore!
the video Maps, Arrays and Subcollections in the "Get to Know Cloud Firestore" series

Filter users by mobilePhone or businessPhones in Microsoft Graph

Is it possible to filter users by phone? I'm trying to find contacts in the global address list by their phone numbers.
I've tried https://graph.microsoft.com/v1.0/users?$filter=mobilePhone eq '+1234567890' but I get the following error:
"code": "Request_UnsupportedQuery",
"message": "Unsupported or invalid query filter clause specified for property 'mobilePhone' of resource 'User'."
also businessPhones/any does not seem to work.
However, the above query works fine in personal contacts but then I would have to manually sync the global address list to my personal contacts which is not desirable.
Thanks in advance.
I know it's almost 4 years since the question being asked, as I'm also looking at the same question today, just going to leave the answer here for future wanderer.
According to this documentation https://learn.microsoft.com/en-us/graph/aad-advanced-queries, mobilePhone and businessPhones is consider an advanced queries, in order to query it, you need to have $count=true in the query string, and ConsistencyLevel: eventual in the header. And you might need to escape the + into %2B.
Microsoft announced this capability in 2020 https://developer.microsoft.com/en-us/office/blogs/microsoft-graph-advanced-queries-for-directory-objects-are-now-generally-available/.
You can do
https://graph.microsoft.com/v1.0/users?$filter=businessPhones/any(p:p le 'a') or mobilePhone ne null&$count=true
with headers ConsistencyLevel: eventual to get users with at least one businessPhone or a mobile.
the le 'a' compares the businessPhone string with the letter a, and since it's after +(123456789 in the alphabet, it'll return true for everything that's non empty
For copypasting:
https://graph.microsoft.com/v1.0/users?$count=true&$filter=startswith(mobilePhone,'%2B73334442211')&$orderBy=displayName&$select=id,displayName,mobilePhone,mail
Ensure ConsistencyLevel: eventual header
Test your tenant with https://developer.microsoft.com/ru-ru/graph/graph-explorer (sign in first)
Thanks to #aron-atilla-hegedus and #stephen-saw
The various phone properties are supported in $filter.
I would recommending visiting the UserVoice and adding your suggestion (and voting up others).

Typeahead.js -> Token different from value doesn't work for me

this is my data
[{"Id": "21", "name": "Šport", "value": "Šport","tokens": ["Šport", "Sport"]}]
I want to the data to be found even if user does not use 'special' characters so it doesn't matter if he types in "Sport" or "Šport"' the data "Šport" will be found.
Should this be working or maybe any workaround sugestions ?
Thanks.
It should work. If it doesn't work, then I would start by checking whether this is really the data the Typeahead sees.
Is this remote? If so, have you made sure this is indeed what you server returns and what your client sees? Look at the Firefox/Chrome developer tools network requests to be sure.
If not, maybe you have stale (old) data in local storage (where Typeahead caches stuff)? Remove any dataset name just to be sure.
Finally, to verify that it works, try this exact data with `local.

How can I check more than one condition on a logged in user? For instance, user is the author of the article or belongs to a certain role

I have a portion of a page that I'll like to display only if the user logged in satisfies any one of the 3 following conditions:
He's the author
He belongs to the 'Editors' role.
He belongs to the 'Admin' role.
The object model contains a property called AddedBy (Model.AddedBy)
How can I check all the 3 conditions on a ASP.NET MVC page.
So far I've tried the below code
<%if((Page.User.Identity.Name == Model.AddedBy) ||
(Page.User.IsInRole("Admin")) ||
(Page.User.IsInRole("Editors"))){%>
//Display this portion of page...
<%}%>
But my code doesn't seem to notice the difference and keeps displaying the portion of html enclosed inside the above code.
I must be wrong somewhere.
EDIT
The html portion enclosed in the code contains buttons such as Delete Article, Edit Article, Add Article, Allow Comments, ... So only the author of the article is supposed to see those buttons or check boxes. Except for the users in Editors role or Admin role can also manipulate articles they don't own.
That way, a user accessing the page can see that portion if he is (i) the author OR (ii) belongs to the Editors role OR (iii) to the Admin role.
Thanks for helping
EDIT: After clarification of the question by the OP it seems that OR was the condition that should be being used, and that the problem was solved by good old traditional debugging! Hurrah! What is below is left so the comments make sense, but it now does not answer the modified question. Might still be useful to someone sometime though...
Is it because you are going to the if condition if ANY of those conditions are satisfied (you are using || which is OR) and not ALL of those conditions (you want to use && which is AND), which is what your question implies you want?
I think this should give you what you want:
<%if((Page.User.Identity.Name == Model.AddedBy) &&
(Page.User.IsInRole("Admin")) &&
(Page.User.IsInRole("Editors"))){%>
//Display this portion of page...
<%}%>
using a single & would work as well I think, but using the double && will be slightly more efficient as it will not try to evaluate any subsequent conditions if one is found to be false (and so it would make sense to put the condition which is most often false as the first check in the if statement)
This documentation should be useful: && Operator and || Operator

Resources