Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore security rules - single protected field in a document

I'd like to have a read-only property called suspendedProfile in a user document with all the other properties with read/write access for currently logged in user. Is there a way to do it with a simple security rule?

I thought about 2 solutions:

  1. disallow writes that modify the property like allow write: if request.resource.data.suspendedProfile == null;
  2. a /secure collection with allow read; inside the user document

I think the first option is better all the user-related properties are in a single docment, but I'd love to hear your thoughts. Is there any other simpler way to achieve this?

like image 886
k0ff33 Avatar asked Dec 21 '17 09:12

k0ff33


People also ask

How do you secure Firestore rules?

Use the Firebase console To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.

What file should be used for Firestore rules Firestore rules?

firestore. rules // is a file used to define the security rules for your Firestore database. firestore.

How do you make a field unique in Firebase?

If you need some value (or combination of values) to be unique, you need to create a node that contains that value (or combination) as its key. If you need to guarantee that multiple values (or combinations) are unique, you'll need multiple of such nodes.


1 Answers

I think I managed to find a solution for my own answer using Firebase documentation.

// A user can update a product reviews, but they can't change
// the headline.
// Also, they should only be able up update their own product review,
// and they still have to list themselves as an author
allow update: if request.resource.data.headline == resource.data.headline
                    && resource.data.authorID == request.auth.userID
                    && request.resource.data.authorID == request.auth.userID;

So in my case, I will just allow update: if request.resource.data.suspendedProfile == resource.data.suspendedProfile

like image 64
k0ff33 Avatar answered Sep 23 '22 07:09

k0ff33