mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-27 18:15: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.
98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
import {
|
|
findGithubById,
|
|
getGithubBranches,
|
|
getGithubRepositories,
|
|
haveGithubRequirements,
|
|
updateGithub,
|
|
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 {
|
|
apiFindGithubBranches,
|
|
apiFindOneGithub,
|
|
apiUpdateGithub,
|
|
} from "@/server/db/schema";
|
|
|
|
export const githubRouter = createTRPCRouter({
|
|
one: protectedProcedure.input(apiFindOneGithub).query(async ({ input }) => {
|
|
return await findGithubById(input.githubId);
|
|
}),
|
|
getGithubRepositories: protectedProcedure
|
|
.input(apiFindOneGithub)
|
|
.query(async ({ input }) => {
|
|
return await getGithubRepositories(input.githubId);
|
|
}),
|
|
getGithubBranches: protectedProcedure
|
|
.input(apiFindGithubBranches)
|
|
.query(async ({ input }) => {
|
|
return await getGithubBranches(input);
|
|
}),
|
|
githubProviders: protectedProcedure.query(async ({ ctx }) => {
|
|
let result = await db.query.github.findMany({
|
|
with: {
|
|
gitProvider: true,
|
|
},
|
|
});
|
|
|
|
result = result.filter(
|
|
(provider) =>
|
|
provider.gitProvider.organizationId ===
|
|
ctx.session.activeOrganizationId &&
|
|
provider.gitProvider.userId === ctx.session.userId,
|
|
);
|
|
|
|
const filtered = result
|
|
.filter((provider) => haveGithubRequirements(provider))
|
|
.map((provider) => {
|
|
return {
|
|
githubId: provider.githubId,
|
|
gitProvider: {
|
|
...provider.gitProvider,
|
|
},
|
|
};
|
|
});
|
|
|
|
return filtered;
|
|
}),
|
|
|
|
testConnection: protectedProcedure
|
|
.input(apiFindOneGithub)
|
|
.mutation(async ({ input }) => {
|
|
try {
|
|
const result = await getGithubRepositories(input.githubId);
|
|
return `Found ${result.length} repositories`;
|
|
} catch (err) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: err instanceof Error ? err?.message : `Error: ${err}`,
|
|
});
|
|
}
|
|
}),
|
|
update: withPermission("gitProviders", "create")
|
|
.input(apiUpdateGithub)
|
|
.mutation(async ({ input, ctx }) => {
|
|
await updateGitProvider(input.gitProviderId, {
|
|
name: input.name,
|
|
organizationId: ctx.session.activeOrganizationId,
|
|
});
|
|
|
|
await updateGithub(input.githubId, {
|
|
...input,
|
|
});
|
|
|
|
await audit(ctx, {
|
|
action: "update",
|
|
resourceType: "gitProvider",
|
|
resourceId: input.gitProviderId,
|
|
resourceName: input.name,
|
|
});
|
|
}),
|
|
});
|