Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare namespace App {
username?: string;
email?: string | null;
roles?: string[];
emailDomain?: string | null;
};
}
}
6 changes: 4 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { config as authConfig } from "../auth.config";
import { isFeatureEnabled } from "./lib/featureflag";
import aclMapping from "./acl-mapping.json";

const protectedPaths = ["/api/admin/services/benchmarks.json", "/dashboard"];
const protectedPaths = ["/api/admin/services/", "/dashboard"];

/**
* Check if the request is for an API endpoint
Expand Down Expand Up @@ -52,14 +52,16 @@ export const onRequest = defineMiddleware(async (context, next) => {
const session = await getSession(context.request, authConfig);

if (session?.user) {
const emailDomain = `@${session.user.email?.split("@").pop()}`;

context.locals.user = {
name: session.user.name,
username: session.user.username,
email: session.user.email,
roles: session.user.roles || [],
emailDomain,
};

const emailDomain = `@${context.locals.user.email?.split("@").pop()}`;
if (context.locals.user.roles?.includes("administrator") || aclMapping.acl.admin.includes(emailDomain)) {
return next();
}
Expand Down
11 changes: 10 additions & 1 deletion src/pages/api/admin/services/[id]/benchmarks.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PARQUET_MONTH_COVERAGE,
getUrlsFromRequest,
} from "@/lib/parquet-datasource";
import aclMapping from "@/acl-mapping.json";

/**
* @openapi
Expand Down Expand Up @@ -92,7 +93,7 @@ import {
* - Benchmark
* - Scenario
*/
export const GET: APIRoute = async ({ params, request }) => {
export const GET: APIRoute = async ({ params, request, locals }) => {
const scenario = params.id;

if (!scenario) {
Expand All @@ -102,6 +103,14 @@ export const GET: APIRoute = async ({ params, request }) => {
);
}

// @ts-expect-error
if (!aclMapping.records[scenario]?.includes(locals.user?.emailDomain)) {
return new Response(
JSON.stringify({ message: "Scenario not found." }),
{ status: 404, headers: { "Content-Type": "application/json" } },
);
}

try {
const urlResponse = await getUrlsFromRequest(request);
if (urlResponse instanceof Response) {
Expand Down
9 changes: 7 additions & 2 deletions src/pages/api/admin/services/benchmarks.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PARQUET_MONTH_COVERAGE,
getUrlsFromRequest,
} from "@/lib/parquet-datasource";
import aclMapping from "@/acl-mapping.json";

/**
* @openapi
Expand Down Expand Up @@ -71,7 +72,7 @@ import {
* - Admin
* - Benchmark
*/
export const GET: APIRoute = async ({ request }) => {
export const GET: APIRoute = async ({ request, locals }) => {
try {
const urlResponse = await getUrlsFromRequest(request);
if (urlResponse instanceof Response) {
Expand Down Expand Up @@ -102,7 +103,11 @@ export const GET: APIRoute = async ({ request }) => {
ORDER BY "scenario_id";
`;

const data = (await executeQuery(query)) as BenchmarkSummary[];
let data = (await executeQuery(query)) as BenchmarkSummary[];
data = data.filter((benchmark) => {
// @ts-expect-error
return aclMapping.records[benchmark.scenario_id]?.includes(locals.user?.emailDomain)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Find a way to make this rule reusable

});

return Response.json(data);
} catch (error) {
Expand Down
Loading