Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active May 27, 2020 20:47
Show Gist options
  • Save katowulf/c2d045c00fcf39d83672bcb53f28717e to your computer and use it in GitHub Desktop.
Save katowulf/c2d045c00fcf39d83672bcb53f28717e to your computer and use it in GitHub Desktop.

Revisions

  1. Kato Richardson revised this gist May 27, 2020. 1 changed file with 16 additions and 11 deletions.
    27 changes: 16 additions & 11 deletions rules.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    // accessMap/{userId}/{docId} is a map of users to documents they can access
    rules_version = '2';
    service cloud.firestore {
    match /databases/{database}/documents {

    match /docs/{docId} {
    // accessMap/{userId}/{docId} is a map of users to documents they can access
    allow read if exists(docPath("accessMap/$(request.auth.uid)/docs/$(docId)"));
    }

    match /docs/{docId} {
    allow read if exists(docPath("accessMap/$(request.auth.uid)/docs/$(docId)"));
    }

    /**
    * Shortcut to simplify pathing
    */
    function getPath(childPath) {
    return path('/databases/'+database+'/documents/'+childPath)
    }
    /**
    * Shortcut to simplify pathing; make sure this exists inside the match /databases block
    */
    function getPath(childPath) {
    return path('/databases/'+database+'/documents/'+childPath)
    }
    }
    }
  2. Kato Richardson renamed this gist May 27, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Kato Richardson created this gist May 27, 2020.
    37 changes: 37 additions & 0 deletions functions-index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // Assumes that group members are stored in a subcollection under /groups/{groupId}/members/{userId}
    const memberPath = '/familyMembers/{familyMemberId}/parents/{parentId}';

    // Trigger updates to our generated maps if group membership changes
    exports.memberAdded = functions.firestore.document(memberPath).onCreate(memberAdded);
    exports.memberDeleted = functions.firestore.document(memberPath).onDelete(memberDeleted);

    async function getAllowedDocuments(parentId) {
    // what goes here?
    return ['foo', 'bar'];
    }
    async function getDisallowedDocuments(parentId) {
    // what goes here?
    return ['foo', 'bar'];
    }

    async function memberAdded(snap, context) {
    const [parentId] = context.params;
    const docs = await getAllowedDocuments(parentId);
    const batch = admin.firestore().batch();
    docs.forEach(docId => {
    const doc = admin.firestore().doc(`accessMap/${parentId}/docs/${docId}`);
    batch.set(doc, {});
    );
    await batch.commit();
    }

    async function memberDeleted(snap, context) {
    const [parentId] = context.params;
    const docs = await getDisallowedDocuments(parentId);
    const batch = admin.firestore().batch();
    docs.forEach(docId => {
    const doc = admin.firestore().doc(`accessMap/${parentId}/docs/${docId}`);
    batch.delete(doc);
    );
    await batch.commit();
    }
    12 changes: 12 additions & 0 deletions rules.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // accessMap/{userId}/{docId} is a map of users to documents they can access

    match /docs/{docId} {
    allow read if exists(docPath("accessMap/$(request.auth.uid)/docs/$(docId)"));
    }

    /**
    * Shortcut to simplify pathing
    */
    function getPath(childPath) {
    return path('/databases/'+database+'/documents/'+childPath)
    }