-
-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/email automation #893
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8036a8c
remove additional profile creation method
chrismclarke aacbe53
add user revision backup function
chrismclarke 36e35ac
add template firestore rules
chrismclarke 92731d8
remove deprecated analytics code
chrismclarke cd00d7d
refactor user revision backend function
chrismclarke 6cc8973
refactor user backup
chrismclarke b94351b
add backend email integration
chrismclarke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// WiP - CC - 2020-02-24 | ||
// As part of future security update will make better use of firestore rules, as stubbed out below | ||
|
||
service cloud.firestore { | ||
// rules will apply to all docs in database | ||
match /databases/{database}/documents { | ||
// Function to check whether request user has specific permission set in user database | ||
function hasUserRole(username,role){ | ||
// TODO - look up a profile to see if the user has specific role (e.g. admin) | ||
// NOTE - this is not currently possible as the username is not sent with a request | ||
// but could be made possible by setting the firebase auth name to the username | ||
// and ensuring all users had a userRoles property | ||
// NOTE 2 - possible code below but would need testing (https://firebase.google.com/docs/reference/rules/rules.List) | ||
// return role in get(/databases/$(database)/documents/v3_users/$(username)).data.userRoles | ||
return true | ||
} | ||
// Function to check if request to modify a document is by the auth owner | ||
function isSameUser(username){ | ||
return get(/databases/$(database)/documents/v3_users/$(username)).data._authID==request.auth.uid | ||
} | ||
match /v3_users/{username} { | ||
allow read, write: if hasUserRole('admin') | ||
allow read,write | ||
} | ||
// users can read/write their own user docs | ||
match /v3_users/{username} { | ||
allow read, write: if isSameUser(username) | ||
} | ||
} | ||
} | ||
|
||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as functions from 'firebase-functions' | ||
import { IDBEndpoint } from '../models' | ||
import { db } from '..//Firebase/firestoreDB' | ||
|
||
const USER_ENDPOINT: IDBEndpoint = 'v3_users' | ||
|
||
/** | ||
* Example function to show how an automated email can be triggered | ||
* In this case it is being used temporarily to help debug | ||
* https://github.com/ONEARMY/community-platform/issues/883 | ||
*/ | ||
export const notifyEmailDemo = functions.firestore | ||
.document(`${USER_ENDPOINT}/precious-plastic`) | ||
.onWrite(async (change, context) => { | ||
return db.collection('mail').add({ | ||
to: 'chris.m.clarke@live.co.uk', | ||
message: { | ||
subject: 'PP Profile Edited', | ||
html: ` | ||
<p>Just thought you should know that an edit has been made to your profile</p> | ||
<h2>Before</h2> | ||
<code>${JSON.stringify(change.before.data())}</code> | ||
<h2>After</h2> | ||
<code>${JSON.stringify(change.after.data())}</code> | ||
<p>To see a clear breakdown of differences you could copy-paste each section into http://www.jsondiff.com</p> | ||
`, | ||
}, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as functions from 'firebase-functions' | ||
import { DBDoc, IDBEndpoint } from '../models' | ||
const USERS_ENDPOINT: IDBEndpoint = 'v3_users' | ||
|
||
/** | ||
* Automatically create user revision on update | ||
* Nests revision as subcollection of original document, | ||
* labeled by previous _modified timestamp | ||
*/ | ||
export const FirebaseUserBackup = functions.firestore | ||
.document(`${USERS_ENDPOINT}/{username}`) | ||
.onUpdate((change, context) => { | ||
const { before, after } = change | ||
const rev = before.data() as DBDoc | ||
if (rev && rev._modified) { | ||
return before.ref | ||
.collection('revisions') | ||
.doc(rev._modified) | ||
.set(rev) | ||
} | ||
return null | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably be separated in a draft PR for more clarity