Skip to content

Commit

Permalink
chore: Safely handle user IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Sep 19, 2023
1 parent edd7327 commit ea011d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions api.planx.uk/editor/copyFlow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from "express";
import { makeUniqueFlow, getFlowData, insertFlow } from "../helpers";
import { Flow } from "../types";
import { userContext } from "../modules/auth/middleware";

const copyFlow = async (
req: Request,
Expand All @@ -25,13 +26,15 @@ const copyFlow = async (
const shouldInsert = (req.body?.insert as boolean) || false;
if (shouldInsert) {
const newSlug = flow.slug + "-copy";
const creatorId = parseInt(req.user!.sub!, 10);
const creatorId = userContext.getStore()?.user?.sub;
if (!creatorId) throw Error("User details missing from request");

// Insert the flow and an associated operation
await insertFlow(
flow.team_id,
newSlug,
uniqueFlowData,
creatorId,
parseInt(creatorId),
req.params.flowId,
);
}
Expand Down
6 changes: 5 additions & 1 deletion api.planx.uk/editor/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dataMerged, getMostRecentPublishedFlow } from "../helpers";
import { gql } from "graphql-request";
import intersection from "lodash/intersection";
import { ComponentType } from "@opensystemslab/planx-core/types";
import { userContext } from "../modules/auth/middleware";

const validateAndDiffFlow = async (
req: Request,
Expand Down Expand Up @@ -73,6 +74,9 @@ const publishFlow = async (
const mostRecent = await getMostRecentPublishedFlow(req.params.flowId);
const delta = jsondiffpatch.diff(mostRecent, flattenedFlow);

const userId = userContext.getStore()?.user?.sub;
if (!userId) throw Error("User details missing from request");

if (delta) {
const response = await adminClient.request(
gql`
Expand Down Expand Up @@ -101,7 +105,7 @@ const publishFlow = async (
{
data: flattenedFlow,
flow_id: req.params.flowId,
publisher_id: parseInt(req.user!.sub!, 10),
publisher_id: parseInt(userId),
summary: req.query?.summary || null,
},
);
Expand Down

0 comments on commit ea011d1

Please sign in to comment.