Skip to content

Commit

Permalink
refactor: add validation to prevent create compose if not have permis…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
Siumauricio committed Jun 2, 2024
1 parent 00656be commit f54fff8
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions server/api/routers/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
updateCompose,
} from "../services/compose";
import { createTRPCRouter, protectedProcedure } from "../trpc";
import { checkServiceAccess } from "../services/user";
import { addNewService, checkServiceAccess } from "../services/user";
import {
cleanQueuesByCompose,
type DeploymentJob,
Expand Down Expand Up @@ -45,8 +45,23 @@ import { templates } from "@/templates/templates";
export const composeRouter = createTRPCRouter({
create: protectedProcedure
.input(apiCreateCompose)
.mutation(async ({ input }) => {
return createCompose(input);
.mutation(async ({ ctx, input }) => {
try {
if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const newService = await createCompose(input);

if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newService.composeId);
}
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the compose",
cause: error,
});
}
}),

one: protectedProcedure
Expand Down Expand Up @@ -194,7 +209,10 @@ export const composeRouter = createTRPCRouter({
}),
deployTemplate: protectedProcedure
.input(apiCreateComposeByTemplate)
.mutation(async ({ input }) => {
.mutation(async ({ ctx, input }) => {
if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const composeFile = await readComposeFile(input.id);

const generate = await loadTemplateModule(input.id as TemplatesKeys);
Expand Down Expand Up @@ -225,6 +243,10 @@ export const composeRouter = createTRPCRouter({
sourceType: "raw",
});

if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, compose.composeId);
}

if (mounts && mounts?.length > 0) {
for (const mount of mounts) {
await createMount({
Expand Down

0 comments on commit f54fff8

Please sign in to comment.