Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore security rules get field/id of reference

I have two collections - tenancies and users.

A tenancy doc has a field called "landlordID" and is of type REFERENCE (not String).

Now in my Firestore Security Rules I want to allow a tenancy to be updated ONLY IF the landlordID field of that tenancy matches with the uid of the user making the request, namely request.auth.uid.

Read it as " allow a tenancy document to be updated if the user making the user is authenticated, hence request.auth.uid != null, and the landlordID field's ID should be equal to that of the request.auth.uid.

Hence the code should me something like this:

    service cloud.firestore {

      match /databases/{database}/documents {

        match /tenancies/{tenancyID}{

            allow update: if request.auth.uid != null && 
                        request.auth.uid == get(resource.data.landlordID).id
    }

}

I have also tried get(/databases/$(database)/documents/users/$(resource.data.landlordID)).data.id

Supporting screenshot of my database

enter image description here

This should be very simple but get() simply does not work. Firebase Docs, scroll to "Access other documents" was not helpful at all for my situation and I am not sure how to get it working.

It would be a shame if references can't be used like this as they are just like any other field of a document.

like image 225
John Dough Avatar asked Mar 14 '19 10:03

John Dough


People also ask

What are FireStore security rules and writing conditions?

This page builds on the concepts in Structuring Security Rules and Writing Conditions for Security Rules to explain how you can use Firestore Security Rules to create rules that allow clients to perform operations on some fields in a document but not others.

How do I deploy Cloud Firestore security rules for my App?

Before you can start using Cloud Firestore from your mobile app, you will need to deploy security rules. You can deploy rules in the Firebase console or using the Firebase CLI. Updates to Cloud Firestore Security Rules can take up to a minute to affect new queries and listeners.

Do firebase security rules apply to the FireStore?

Have in mind that Firestore rules do not apply to the Firebase Admin SDK, commonly used in Cloud Functions and other trusted environments. This is a list of simple and complex Firebase Security rules that you can use in your project today. I hope you will find them helpful.

How secure is FireStore?

Security is the most important part of a database. If you don’t properly secure your database, then the data inside is just waiting to be stolen, corrupted, or completely wiped. Firestore has some smart and straightforward security rules to help keep the database running safe and smooth.


1 Answers

Here is a function I made that works for me. I guess you have a user collection with users having the same id as their auth.uid

    function isUserRef(field) {
      return field in resource.data
        && resource.data[field] == /databases/$(database)/documents/users/$(request.auth.uid)
    }

Adjusting to your use case you'd call the function so: isUserRef('landlordID') although the ID at the end of it is a bit misleading as this field is in fact a reference.

like image 111
Gerardlamo Avatar answered Sep 21 '22 20:09

Gerardlamo