Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions functions/src/common/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,31 @@ function getRole(
return acl && user.email ? acl[user.email] : null;
}

export function hasOrganizerRole(
user: DecodedIdToken,
survey: DocumentSnapshot
): boolean {
const role = getRole(user, survey);
return !!role && [Pb.Role.SURVEY_ORGANIZER].includes(role);
}

export function canExport(
user: DecodedIdToken,
survey: DocumentSnapshot
): boolean {
const generalAccess = survey.get(s.generalAccess);
if (
[Pb.Survey.GeneralAccess.PUBLIC, Pb.Survey.GeneralAccess.UNLISTED].includes(
generalAccess
)
)
return true;
return !!getRole(user, survey);
}

export function canImport(
user: DecodedIdToken,
survey: DocumentSnapshot
): boolean {
const role = getRole(user, survey);
return !!role && [Pb.Role.SURVEY_ORGANIZER].includes(role);
return hasOrganizerRole(user, survey);
}
2 changes: 1 addition & 1 deletion functions/src/common/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
type pseudoGeoJsonGeometry = {
type: string;
coordinates: any;

Check warning on line 34 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
};

/**
Expand Down Expand Up @@ -185,7 +185,7 @@
async fetchLoisSubmissions(
surveyId: string,
jobId: string,
ownerId: string | undefined,
ownerId: string | null,
pageSize: number
) {
const loisQuery = this.db_
Expand Down Expand Up @@ -238,7 +238,7 @@
await loiRef.update({[l.properties]: loiDoc[l.properties]});
}

static toFirestoreMap(geometry: any) {

Check warning on line 241 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
return Object.fromEntries(
Object.entries(geometry).map(([key, value]) => [
key,
Expand All @@ -247,7 +247,7 @@
);
}

static toFirestoreValue(value: any): any {

Check warning on line 250 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type

Check warning on line 250 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
if (value === null) {
return null;
}
Expand Down Expand Up @@ -275,7 +275,7 @@
*
* @returns GeoJSON geometry object (with geometry as list of lists)
*/
static fromFirestoreMap(geoJsonGeometry: any): any {

Check warning on line 278 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type

Check warning on line 278 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
const geometryObject = geoJsonGeometry as pseudoGeoJsonGeometry;
if (!geometryObject) {
throw new Error(
Expand All @@ -290,7 +290,7 @@
return geometryObject;
}

static fromFirestoreValue(coordinates: any) {

Check warning on line 293 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
if (coordinates instanceof GeoPoint) {
// Note: GeoJSON coordinates are in lng-lat order.
return [coordinates.longitude, coordinates.latitude];
Expand All @@ -299,7 +299,7 @@
if (typeof coordinates !== 'object') {
return coordinates;
}
const result = new Array<any>(coordinates.length);

Check warning on line 302 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type

Object.entries(coordinates).map(([i, nestedValue]) => {
const index = Number.parseInt(i);
Expand Down
27 changes: 27 additions & 0 deletions functions/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@
* limitations under the License.
*/

import {GroundProtos} from '@ground/proto';

import Pb = GroundProtos.ground.v1beta1;

/**
* Checks if a Location of Interest (LOI) is accessible to a given user.
*
* An LOI is accessible if:
* - Its source is `IMPORTED`.
* - Its `ownerId` passed to the function is `null`.
* - Its `ownerId` matches the LOI's owner.
*
* @param loi The Location of Interest object to check.
* @param ownerId The ID of the user requesting access. Pass `null` to bypass the ownership check.
* @returns True if the LOI is accessible to the user, false otherwise.
*/
export function isAccessibleLoi(
loi: Pb.ILocationOfInterest,
ownerId: string | null
) {
return (
loi.source === Pb.LocationOfInterest.Source.IMPORTED ||
ownerId === null ||
loi.ownerId === ownerId
);
}

export function stringFormat(s: string, ...args: any[]): string {
return s.replace(/\{(\d+)\}/g, (_, index) => args[index] || `{${index}}`);
}
52 changes: 27 additions & 25 deletions functions/src/export-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import * as functions from 'firebase-functions';
import * as csv from '@fast-csv/format';
import {canExport, canImport} from './common/auth';
import {canExport, hasOrganizerRole} from './common/auth';
import {isAccessibleLoi} from './common/utils';
import {geojsonToWKT} from '@terraformer/wkt';
import {getDatastore} from './common/context';
import * as HttpStatus from 'http-status-codes';
Expand All @@ -42,6 +43,7 @@ export async function exportCsvHandler(
const {uid: userId} = user;
const surveyId = req.query.survey as string;
const jobId = req.query.job as string;

const surveyDoc = await db.fetchSurvey(surveyId);
if (!surveyDoc.exists) {
res.status(HttpStatus.NOT_FOUND).send('Survey not found');
Expand All @@ -51,7 +53,13 @@ export async function exportCsvHandler(
res.status(HttpStatus.FORBIDDEN).send('Permission denied');
return;
}
const ownerId = canImport(user, surveyDoc) ? undefined : userId;
const survey = toMessage(surveyDoc.data()!, Pb.Survey);
if (survey instanceof Error) {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.send('Unsupported or corrupt survey');
return;
}

const jobDoc = await db.fetchJob(surveyId, jobId);
if (!jobDoc.exists || !jobDoc.data()) {
Expand All @@ -67,11 +75,17 @@ export async function exportCsvHandler(
}
const {name: jobName} = job;

const survey = toMessage(surveyDoc.data()!, Pb.Survey);
const isOrganizer = hasOrganizerRole(user, surveyDoc);

const canViewAll =
isOrganizer ||
survey.dataVisibility === Pb.Survey.DataVisibility.ALL_SURVEY_PARTICIPANTS;

const ownerIdFilter = canViewAll ? null : userId;

const tasks = job.tasks.sort((a, b) => a.index! - b.index!);
const snapshot = await db.fetchLocationsOfInterest(surveyId, jobId);
const loiProperties = createProperySetFromSnapshot(snapshot, survey, ownerId);
const loiProperties = createProperySetFromSnapshot(snapshot, ownerIdFilter);
const headers = getHeaders(tasks, loiProperties);

res.type('text/csv');
Expand All @@ -89,14 +103,19 @@ export async function exportCsvHandler(
});
csvStream.pipe(res);

const rows = await db.fetchLoisSubmissions(surveyId, jobId, ownerId, 50);
const rows = await db.fetchLoisSubmissions(
surveyId,
jobId,
ownerIdFilter,
50
);

for await (const row of rows) {
try {
const [loiDoc, submissionDoc] = row;
const loi = toMessage(loiDoc.data(), Pb.LocationOfInterest);
if (loi instanceof Error) throw loi;
if (isAccessibleLoi(survey, loi, ownerId) && submissionDoc) {
if (isAccessibleLoi(loi, ownerIdFilter) && submissionDoc) {
const submission = toMessage(submissionDoc.data(), Pb.Submission);
if (submission instanceof Error) throw submission;
writeRow(csvStream, loiProperties, tasks, loi, submission);
Expand Down Expand Up @@ -179,22 +198,6 @@ function toWkt(geometry: Pb.IGeometry): string {
return geojsonToWKT(toGeoJsonGeometry(geometry));
}

/**
* Checks if a Location of Interest (LOI) is accessible to a given user.
*/
function isAccessibleLoi(
survey: Pb.ISurvey,
loi: Pb.ILocationOfInterest,
ownerId?: string
) {
if (
survey.dataVisibility === Pb.Survey.DataVisibility.ALL_SURVEY_PARTICIPANTS
)
return true;
const isFieldData = loi.source === Pb.LocationOfInterest.Source.FIELD_DATA;
return ownerId ? isFieldData && loi.ownerId === ownerId : true;
}

/**
* Returns the string or number representation of a specific task element result.
*/
Expand Down Expand Up @@ -290,14 +293,13 @@ function getFileName(jobName: string | null) {

function createProperySetFromSnapshot(
snapshot: QuerySnapshot,
survey: Pb.ISurvey,
ownerId?: string
ownerId: string | null
): Set<string> {
const allKeys = new Set<string>();
snapshot.forEach(doc => {
const loi = toMessage(doc.data(), Pb.LocationOfInterest);
if (loi instanceof Error) return;
if (!isAccessibleLoi(survey, loi, ownerId)) return;
if (!isAccessibleLoi(loi, ownerId)) return;
const properties = loi.properties;
for (const key of Object.keys(properties || {})) {
allKeys.add(key);
Expand Down
38 changes: 18 additions & 20 deletions functions/src/export-geojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

import * as functions from 'firebase-functions';
import {Map} from 'immutable';
import {canExport, canImport} from './common/auth';
import {canExport, hasOrganizerRole} from './common/auth';
import {getDatastore} from './common/context';
import {isAccessibleLoi} from './common/utils';
import * as HttpStatus from 'http-status-codes';
import {DecodedIdToken} from 'firebase-admin/auth';
import {toMessage} from '@ground/lib';
Expand All @@ -38,6 +39,7 @@ export async function exportGeojsonHandler(
const {uid: userId} = user;
const surveyId = req.query.survey as string;
const jobId = req.query.job as string;

const surveyDoc = await db.fetchSurvey(surveyId);
if (!surveyDoc.exists) {
res.status(HttpStatus.NOT_FOUND).send('Survey not found');
Expand All @@ -47,7 +49,13 @@ export async function exportGeojsonHandler(
res.status(HttpStatus.FORBIDDEN).send('Permission denied');
return;
}
const ownerId = canImport(user, surveyDoc) ? undefined : userId;
const survey = toMessage(surveyDoc.data()!, Pb.Survey);
if (survey instanceof Error) {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.send('Unsupported or corrupt survey');
return;
}

const jobDoc = await db.fetchJob(surveyId, jobId);
if (!jobDoc.exists || !jobDoc.data()) {
Expand All @@ -63,7 +71,13 @@ export async function exportGeojsonHandler(
}
const {name: jobName} = job;

const survey = toMessage(surveyDoc.data()!, Pb.Survey);
const isOrganizer = hasOrganizerRole(user, surveyDoc);

const canViewAll =
isOrganizer ||
survey.dataVisibility === Pb.Survey.DataVisibility.ALL_SURVEY_PARTICIPANTS;

const ownerIdFilter = canViewAll ? null : userId;

res.type('application/json');
res.setHeader(
Expand All @@ -83,7 +97,7 @@ export async function exportGeojsonHandler(
try {
const loi = toMessage(row.data(), Pb.LocationOfInterest);
if (loi instanceof Error) throw loi;
if (isAccessibleLoi(survey, loi, ownerId)) {
if (isAccessibleLoi(loi, ownerIdFilter)) {
const feature = buildFeature(loi);
if (!feature) continue;

Expand Down Expand Up @@ -119,22 +133,6 @@ function buildFeature(loi: Pb.LocationOfInterest) {
};
}

/**
* Checks if a Location of Interest (LOI) is accessible to a given user.
*/
function isAccessibleLoi(
survey: Pb.ISurvey,
loi: Pb.ILocationOfInterest,
ownerId?: string
) {
if (
survey.dataVisibility === Pb.Survey.DataVisibility.ALL_SURVEY_PARTICIPANTS
)
return true;
const isFieldData = loi.source === Pb.LocationOfInterest.Source.FIELD_DATA;
return ownerId ? isFieldData && loi.ownerId === ownerId : true;
}

/**
* Returns the file name in lowercase (replacing any special characters with '-') for csv export
*/
Expand Down
Loading