Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active July 27, 2024 00:35
Show Gist options
  • Select an option

  • Save katowulf/01d4d0792fc6a3e4fa0af465e7c5030f to your computer and use it in GitHub Desktop.

Select an option

Save katowulf/01d4d0792fc6a3e4fa0af465e7c5030f to your computer and use it in GitHub Desktop.

Revisions

  1. Kato Richardson revised this gist May 27, 2020. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion simpleDocSharing_listMembers.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // docs/{docId}/users is an array of user ids allowed to access the doc

    /match/docs/{docId} {
    match /docs/{docId} {
    allow read if request.auth.uid in getData('docs/$(docId)').users;
    }

    2 changes: 1 addition & 1 deletion simpleGroups_rules.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // docs/{docId}/groups is an array of group ids allowed to access this doc
    // membershipList/{userId}/groups is an array of groups this user is a member of (populated by a Function)

    /match/.../{docId} {
    match /docs/{docId} {
    allow read if getData('membershipList/' + request.auth.uid).groups.hasAny( getData('docs/$(docId)').groups );
    }

  2. Kato Richardson revised this gist Jun 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simpleGroups_rules.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // membershipList/{userId}/groups is an array of groups this user is a member of (populated by a Function)

    /match/.../{docId} {
    allow read if .hasAny( getData('docs/$(docId)').groups );
    allow read if getData('membershipList/' + request.auth.uid).groups.hasAny( getData('docs/$(docId)').groups );
    }

    /**
  3. Kato Richardson created this gist Jun 7, 2019.
    19 changes: 19 additions & 0 deletions simpleDocSharing_listMembers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // docs/{docId}/users is an array of user ids allowed to access the doc

    /match/docs/{docId} {
    allow read if request.auth.uid in getData('docs/$(docId)').users;
    }

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

    /**
    * Fetch data from a given Firestore document
    */
    function getData(docPath) {
    return get(getPath(docPath)).data
    }
    22 changes: 22 additions & 0 deletions simpleGroups_functions.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // Assumes that group members are stored in a subcollection under /groups/{groupId}/members/{userId}
    const memberPath = '/groups/{groupId}/members/{userId}';

    // 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);

    function memberAdded(snap, context) {
    const [groupId, userId] = context.params;
    const data = {
    groups: admin.firestore.FieldValue.arrayUnion(groupId)
    }
    return admin.firestore().doc(`membershipList/${userId}`).update(data);
    }

    function memberDeleted(snap, context) {
    const [groupId, userId] = context.params;
    const data = {
    groups: admin.firestore.FieldValue.arrayRemove(groupId)
    };
    return admin.firestore().doc(`membershipList/${userId}`).update(data);
    }
    20 changes: 20 additions & 0 deletions simpleGroups_rules.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // docs/{docId}/groups is an array of group ids allowed to access this doc
    // membershipList/{userId}/groups is an array of groups this user is a member of (populated by a Function)

    /match/.../{docId} {
    allow read if .hasAny( getData('docs/$(docId)').groups );
    }

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

    /**
    * Fetch data from a given Firestore document
    */
    function getData(docPath) {
    return get(getPath(docPath)).data
    }