Skip to content

Instantly share code, notes, and snippets.

@zeroows
Last active March 30, 2020 20:57
Show Gist options
  • Save zeroows/3ae65bcf10bd6353d3b55140d89a4873 to your computer and use it in GitHub Desktop.
Save zeroows/3ae65bcf10bd6353d3b55140d89a4873 to your computer and use it in GitHub Desktop.

Revisions

  1. zeroows renamed this gist Mar 30, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. zeroows created this gist Mar 29, 2020.
    46 changes: 46 additions & 0 deletions firestore-rules.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    rules_version = '2';
    service cloud.firestore {
    match /databases/{database}/documents {
    match /shoppinglist/{lists} {
    function isSignedIn() {
    return request.auth != null;
    }
    function isEmailVerified() {
    return request.auth.token.email_verified != false;
    }
    function isKnownUser() {
    return isSignedIn() && request.auth.uid != "" && isEmailVerified();
    }
    function UserId() {
    return request.auth.uid;
    }
    function getOwner(rsc) {
    // Read Owner in the resource (rsc).
    return rsc.data.owner;
    }
    function getShares(rsc) {
    // Read Shares in the resource (rsc).
    return rsc.data.shares;
    }
    function isOwner(rsc) {
    // Determine if the user is the owner
    return isKnownUser() && (getOwner(rsc) == UserId());
    }
    function isShared(rsc) {
    // Determine if the user was shared the list
    return isKnownUser() && (UserId() in getShares(rsc));
    }
    function getParentDoc(){
    // Get the parent document
    return get(/databases/$(database)/documents/shoppinglist/$(lists));
    }
    allow read: if isOwner(resource) || isShared(resource);
    allow write: if isOwner(resource);

    match /items/{items} {
    allow read: if isOwner(getParentDoc()) || isShared(getParentDoc());
    allow write: if isOwner(getParentDoc());
    }
    }
    }
    }