Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
update and fix bug #35
Browse files Browse the repository at this point in the history
  • Loading branch information
wp99cp committed Feb 15, 2021
1 parent 7ac235d commit 6ccd297
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 106 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/menuplanung_CloudFunctions.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ service cloud.firestore {
// check's if the user has access to this document
// this function did not check the access level
function hasAccess() {
return resource.data.access[request.auth.uid] in ['editor', 'owner', 'collaborator' , 'viewer'];
return resource.data.access[request.auth.uid] in ['editor', 'owner', 'collaborator' , 'viewer'] ||
resource.data.access['all_users'] in ['viewer'];
}

// check's if the user has access to write to this document
Expand Down Expand Up @@ -58,6 +59,13 @@ service cloud.firestore {

}

// user collecion
match /users/{userID}/private/{document=**} {

allow read, write: if request.auth.uid == userID

}


// shared data can be read but not write to it
match /sharedData/{document=**} {
Expand All @@ -66,6 +74,13 @@ service cloud.firestore {

}

// Help messages can be read by all users
match /sharedData/helpMessages/messages/{document=**} {

allow read, list;

}


// camps collection
match /camps/{campID} {
Expand Down
2 changes: 1 addition & 1 deletion functions/res/lagerhandbuch.html
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ <h1 class="page-title">[wird ersetzt]</h1>
</span>
</article>

<!-- Cannot be fix-page-size, so allow more than 8 days in week-view -->
<!-- Cannot be fix-page-size, so allow more than 8 days in week-overview -->
<article class="page">
<h1 class="page-title">Wochenübersicht</h1>
<span class="val-week-error"></span>
Expand Down
51 changes: 19 additions & 32 deletions functions/src/changeAccessData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {AccessData, FirestoreDocument, FirestoreMeal, FirestoreRecipe, Rules} from "./interfaces/firestoreDatatypes";
import {db} from "./index";
import * as functions from 'firebase-functions';
import {ResponseData} from "./CloudFunction";

/**
* Data needed to request a access change of a document.
Expand Down Expand Up @@ -38,18 +39,12 @@ export interface Refresh {
* @param requestedChanges to the accessData
* @param context of the function call
*/
export function changeAccessData(requestedChanges: AccessChange, context: functions.https.CallableContext): Promise<any> {
export function changeAccessData(requestedChanges: AccessChange, context: functions.https.CallableContext): Promise<ResponseData> {

return new Promise((resolve) => {

db.runTransaction(async (transaction) =>
await changeAccessDataWithTransaction(transaction, requestedChanges, context, false, undefined))

// function create response
.then(() => resolve({message: 'AccessData successfully updated.'}))
.catch(err => resolve({error: err.message}));

});
return new Promise(async res => {
await changeAccessDataWithTransaction(requestedChanges, context, false, undefined);
return {};
})

}

Expand Down Expand Up @@ -81,7 +76,7 @@ export function refreshAccessData(refreshRequest: Refresh, context: functions.ht
}

// execute all the changes...
await changeAccessDataWithTransaction(transaction, accessChanges, context, true, undefined)
await changeAccessDataWithTransaction(accessChanges, context, true, undefined)

}) // function create response
.then(() => resolve({message: 'AccessData successfully updated.'}))
Expand All @@ -97,21 +92,19 @@ export function refreshAccessData(refreshRequest: Refresh, context: functions.ht
* i.g. add inherited rights form meals which use this recipe.
*
* @param document snapshot of the recipe
* @param transaction current transaction to request data
* @param access current access data to be modified
*
*/
async function addMinRightsForRecipe(
document: FirebaseFirestore.DocumentSnapshot,
transaction: FirebaseFirestore.Transaction,
access: AccessData) {

const recipeData = (document.data() as FirestoreRecipe);
// upgrade to minimum rights
const mealRefs = recipeData.used_in_meals;
await Promise.all(mealRefs.map(async mealRef => {

// get access data --> TODO: use transaction
// get access data
const mealAccessRights = ((await db.doc('meals/' + mealRef).get())
.data() as FirestoreDocument).access;

Expand All @@ -131,22 +124,20 @@ async function addMinRightsForRecipe(
* Performs the access change inside a transaction.
* This function can be called iterativ with different arguments.
*
* @param transaction: the current transaction to perform all read and writes in it.
* @param requestedChanges to the access data
* @param context of the cloud function call
* @param parentId optional parameter with a parent document id to exclude in some checks
* @param onlyAccessNeeded
*/
async function changeAccessDataWithTransaction(
transaction: FirebaseFirestore.Transaction,
requestedChanges: AccessChange,
context: functions.https.CallableContext,
onlyAccessNeeded: boolean,
parentId ?: string | undefined) {

const documentRef = db.doc(requestedChanges.documentPath);

const document = await transaction.get(documentRef);
const document = await documentRef.get();

// check if changes are valid
if (!await isValidChange(document, requestedChanges, context, parentId, onlyAccessNeeded))
Expand All @@ -160,21 +151,21 @@ async function changeAccessDataWithTransaction(
const access = generateNewAccessData(JSON.parse(JSON.stringify(requestedChanges)), document);

await elevateRelatedDocumentsRights(documentRef,
JSON.parse(JSON.stringify(requestedChanges)), context, transaction, onlyAccessNeeded);
JSON.parse(JSON.stringify(requestedChanges)), context, onlyAccessNeeded);

// if it's a recipe check for min. access
if (documentRef.parent.id === 'recipes')
await addMinRightsForRecipe(document, transaction, access);
await addMinRightsForRecipe(document, access);

// TODO: possible problem with transaction...
console.log(documentRef.path + ' (1): ' + JSON.stringify(access));
transaction.update(documentRef, {access});
await documentRef.update({access});

}

// ... or decrease rights
else {
await decreaseRights(documentRef, requestedChanges, transaction, context, onlyAccessNeeded);
await decreaseRights(documentRef, requestedChanges, context, onlyAccessNeeded);
}
}

Expand All @@ -183,15 +174,13 @@ async function changeAccessDataWithTransaction(
*
* @param documentRef to the document
* @param requestedChanges to the access data
* @param transaction: the current transaction to perform writes in
* @param context of the cloud function call
* @param onlyAccessNeeded
*
*/
async function decreaseRights(
documentRef: FirebaseFirestore.DocumentReference,
requestedChanges: AccessChange,
transaction: FirebaseFirestore.Transaction,
context: functions.https.CallableContext,
onlyAccessNeeded: boolean) {

Expand Down Expand Up @@ -221,7 +210,7 @@ async function decreaseRights(
requestedAccessData: minimumRights,
upgradeOnly: true
}
const meal = await transaction.get(db.doc('meals/' + mealId));
const meal = await db.doc('meals/' + mealId).get();
minimumRights = generateNewAccessData(changes, meal);
}));

Expand All @@ -241,7 +230,7 @@ async function decreaseRights(

// catch exceptions, it may not be possible to decrease the rights of the recipes
try {
await changeAccessDataWithTransaction(transaction,
await changeAccessDataWithTransaction(
JSON.parse(JSON.stringify(changesToRecipe)), context, true, documentRef.id);
} catch (error) {
console.log(error);
Expand All @@ -252,7 +241,7 @@ async function decreaseRights(
// TODO: possible problem with transaction...
// update accessData in document
console.log(documentRef.path + '(2): ' + JSON.stringify(requestedChanges.requestedAccessData));
transaction.update(documentRef, {access: requestedChanges.requestedAccessData});
await documentRef.update({access: requestedChanges.requestedAccessData});

break;

Expand All @@ -261,7 +250,7 @@ async function decreaseRights(
// TODO: possible problem with transaction...
// update accessData in document
console.log(documentRef.path + '(3): ' + JSON.stringify(requestedChanges.requestedAccessData));
transaction.update(documentRef, {access: requestedChanges.requestedAccessData});
await documentRef.update({access: requestedChanges.requestedAccessData});

break;

Expand All @@ -280,15 +269,13 @@ async function decreaseRights(
* @param documentRef
* @param requestedChanges
* @param context
* @param transaction
* @param onlyAccessNeeded
*
*/
async function elevateRelatedDocumentsRights(
documentRef: FirebaseFirestore.DocumentReference,
requestedChanges: AccessChange,
context: functions.https.CallableContext,
transaction: FirebaseFirestore.Transaction,
onlyAccessNeeded: boolean) {

const collectionName = documentRef.parent.id;
Expand All @@ -313,7 +300,7 @@ async function elevateRelatedDocumentsRights(

// update meals and recipes to min viewer
await Promise.all(mealRefs.docs.map(async (mealRef) =>
changeAccessDataWithTransaction(transaction, {
changeAccessDataWithTransaction({
upgradeOnly: true,
documentPath: 'meals/' + mealRef.id,
requestedAccessData: access
Expand Down Expand Up @@ -354,7 +341,7 @@ async function elevateRelatedDocumentsRights(

// changing the rights of a meal may not be possible
try {
await changeAccessDataWithTransaction(transaction,
await changeAccessDataWithTransaction(
JSON.parse(JSON.stringify(recipeChanges)), context, true, documentRef.id);
} catch (error) {
console.log(error);
Expand Down
2 changes: 1 addition & 1 deletion functions/src/exportCamp/createHTMLForExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const createHTML = (camp: ExportedCamp) => {
}

innerHTMLStr += '</table>';
const domElm = document.querySelector('.val-week-view-table') as Element;
const domElm = document.querySelector('.val-week-overview-table') as Element;
domElm.innerHTML = innerHTMLStr;
}

Expand Down
Loading

0 comments on commit 6ccd297

Please sign in to comment.