Skip to content

Commit

Permalink
amend to base
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Jan 18, 2023
1 parent a2e374f commit 1881af0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
18 changes: 4 additions & 14 deletions packages/api/src/controllers/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { Router } from "express";
import _ from "lodash";
import { v4 as uuid, validate as validateUuid } from "uuid";
import { db } from "../store";
import {
NotFoundError,
ForbiddenError,
BadRequestError,
} from "../store/errors";
import { NotFoundError, BadRequestError } from "../store/errors";
import {
makeNextHREF,
parseFilters,
Expand All @@ -19,7 +15,7 @@ import { authorizer, validatePost } from "../middleware";
import { WithID } from "../store/types";

import experimentApis from "./experiment/index";
import { isExperimentSubject } from "../store/experiment-table";
import { ensureExperimentSubject } from "../store/experiment-table";

async function toUserId(emailOrId: string) {
let user: User;
Expand All @@ -42,10 +38,7 @@ const app = Router();

const experimentSubjectsOnly =
(experiment: string) => async (req, res, next) => {
const isSubject = await isExperimentSubject(experiment, req.user?.id);
if (!isSubject) {
throw new ForbiddenError("user is not an experiment subject");
}
await ensureExperimentSubject(experiment, req.user?.id);
return next();
};

Expand Down Expand Up @@ -75,10 +68,7 @@ app.get("/check/:experiment", authorizer({}), async (req, res) => {
}

const { experiment: experimentQuery } = req.params;
const isSubject = await isExperimentSubject(experimentQuery, user.id);
if (!isSubject) {
throw new ForbiddenError("user is not an experiment subject");
}
await ensureExperimentSubject(experimentQuery, user.id);
res.status(204).end();
});

Expand Down
13 changes: 12 additions & 1 deletion packages/api/src/store/experiment-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sql from "sql-template-strings";

import { Experiment } from "../schema/types";
import db from "./db";
import { NotFoundError } from "./errors";
import { ForbiddenError, NotFoundError } from "./errors";
import Table from "./table";
import { WithID } from "./types";

Expand All @@ -11,6 +11,17 @@ export async function isExperimentSubject(experiment: string, userId: string) {
return audienceUserIds.includes(userId);
}

export async function ensureExperimentSubject(
experiment: string,
userId: string
) {
if (!(await isExperimentSubject(experiment, userId))) {
throw new ForbiddenError(
`user is not a subject of experiment: ${experiment}`
);
}
}

export default class ExperimentTable extends Table<WithID<Experiment>> {
async listUserExperiments(
userId: string,
Expand Down

0 comments on commit 1881af0

Please sign in to comment.