mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-15 20:25:23 +02:00
- Introduced new test files for permission checks, including `check-permission.test.ts`, `enterprise-only-resources.test.ts`, `resolve-permissions.test.ts`, and `service-access.test.ts`. - Implemented permission checks in various components to ensure actions are gated by user permissions, including `ShowTraefikConfig`, `UpdateTraefikConfig`, `ShowVolumes`, `ShowDomains`, and others. - Enhanced the logic for displaying UI elements based on user permissions, ensuring that only authorized users can access or modify resources.
135 lines
3.1 KiB
TypeScript
135 lines
3.1 KiB
TypeScript
import {
|
|
createGitlab,
|
|
findGitlabById,
|
|
getGitlabBranches,
|
|
getGitlabRepositories,
|
|
haveGitlabRequirements,
|
|
testGitlabConnection,
|
|
updateGitlab,
|
|
updateGitProvider,
|
|
} from "@dokploy/server";
|
|
import { db } from "@dokploy/server/db";
|
|
import { TRPCError } from "@trpc/server";
|
|
import {
|
|
createTRPCRouter,
|
|
protectedProcedure,
|
|
withPermission,
|
|
} from "@/server/api/trpc";
|
|
import { audit } from "@/server/api/utils/audit";
|
|
import {
|
|
apiCreateGitlab,
|
|
apiFindGitlabBranches,
|
|
apiFindOneGitlab,
|
|
apiGitlabTestConnection,
|
|
apiUpdateGitlab,
|
|
} from "@/server/db/schema";
|
|
|
|
export const gitlabRouter = createTRPCRouter({
|
|
create: withPermission("gitProviders", "create")
|
|
.input(apiCreateGitlab)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const result = await createGitlab(
|
|
input,
|
|
ctx.session.activeOrganizationId,
|
|
ctx.session.userId,
|
|
);
|
|
|
|
await audit(ctx, {
|
|
action: "create",
|
|
resourceType: "gitProvider",
|
|
resourceName: input.name,
|
|
});
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Error creating this Gitlab provider",
|
|
cause: error,
|
|
});
|
|
}
|
|
}),
|
|
one: protectedProcedure.input(apiFindOneGitlab).query(async ({ input }) => {
|
|
return await findGitlabById(input.gitlabId);
|
|
}),
|
|
gitlabProviders: protectedProcedure.query(async ({ ctx }) => {
|
|
let result = await db.query.gitlab.findMany({
|
|
with: {
|
|
gitProvider: true,
|
|
},
|
|
});
|
|
|
|
result = result.filter((provider) => {
|
|
return (
|
|
provider.gitProvider.organizationId ===
|
|
ctx.session.activeOrganizationId &&
|
|
provider.gitProvider.userId === ctx.session.userId
|
|
);
|
|
});
|
|
const filtered = result
|
|
.filter((provider) => haveGitlabRequirements(provider))
|
|
.map((provider) => {
|
|
return {
|
|
gitlabId: provider.gitlabId,
|
|
gitProvider: {
|
|
...provider.gitProvider,
|
|
},
|
|
gitlabUrl: provider.gitlabUrl,
|
|
};
|
|
});
|
|
|
|
return filtered;
|
|
}),
|
|
getGitlabRepositories: protectedProcedure
|
|
.input(apiFindOneGitlab)
|
|
.query(async ({ input }) => {
|
|
return await getGitlabRepositories(input.gitlabId);
|
|
}),
|
|
|
|
getGitlabBranches: protectedProcedure
|
|
.input(apiFindGitlabBranches)
|
|
.query(async ({ input }) => {
|
|
return await getGitlabBranches(input);
|
|
}),
|
|
testConnection: protectedProcedure
|
|
.input(apiGitlabTestConnection)
|
|
.mutation(async ({ input }) => {
|
|
try {
|
|
const result = await testGitlabConnection(input);
|
|
|
|
return `Found ${result} repositories`;
|
|
} catch (error) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: error instanceof Error ? error?.message : `Error: ${error}`,
|
|
});
|
|
}
|
|
}),
|
|
update: withPermission("gitProviders", "create")
|
|
.input(apiUpdateGitlab)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.name) {
|
|
await updateGitProvider(input.gitProviderId, {
|
|
name: input.name,
|
|
organizationId: ctx.session.activeOrganizationId,
|
|
});
|
|
|
|
await updateGitlab(input.gitlabId, {
|
|
...input,
|
|
});
|
|
} else {
|
|
await updateGitlab(input.gitlabId, {
|
|
...input,
|
|
});
|
|
}
|
|
|
|
await audit(ctx, {
|
|
action: "update",
|
|
resourceType: "gitProvider",
|
|
resourceId: input.gitProviderId,
|
|
resourceName: input.name,
|
|
});
|
|
}),
|
|
});
|