mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat: GitHub and GitLab provider integration with user association
- Added userId to the GitHub and GitLab provider setup to associate providers with the user who created them. - Updated redirect URL in GitHub provider to include userId for better tracking. - Modified API handlers and service functions to accommodate userId in provider creation and validation.
This commit is contained in:
@@ -18,6 +18,7 @@ import { useEffect, useState } from "react";
|
|||||||
export const AddGithubProvider = () => {
|
export const AddGithubProvider = () => {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const { data: activeOrganization } = authClient.useActiveOrganization();
|
const { data: activeOrganization } = authClient.useActiveOrganization();
|
||||||
|
const { data: session } = authClient.useSession();
|
||||||
const { data } = api.user.get.useQuery();
|
const { data } = api.user.get.useQuery();
|
||||||
const [manifest, setManifest] = useState("");
|
const [manifest, setManifest] = useState("");
|
||||||
const [isOrganization, setIsOrganization] = useState(false);
|
const [isOrganization, setIsOrganization] = useState(false);
|
||||||
@@ -27,7 +28,7 @@ export const AddGithubProvider = () => {
|
|||||||
const url = document.location.origin;
|
const url = document.location.origin;
|
||||||
const manifest = JSON.stringify(
|
const manifest = JSON.stringify(
|
||||||
{
|
{
|
||||||
redirect_url: `${origin}/api/providers/github/setup?organizationId=${activeOrganization?.id}`,
|
redirect_url: `${origin}/api/providers/github/setup?organizationId=${activeOrganization?.id}&userId=${session?.user?.id}`,
|
||||||
name: `Dokploy-${format(new Date(), "yyyy-MM-dd")}`,
|
name: `Dokploy-${format(new Date(), "yyyy-MM-dd")}`,
|
||||||
url: origin,
|
url: origin,
|
||||||
hook_attributes: {
|
hook_attributes: {
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ type Query = {
|
|||||||
state: string;
|
state: string;
|
||||||
installation_id: string;
|
installation_id: string;
|
||||||
setup_action: string;
|
setup_action: string;
|
||||||
|
userId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
) {
|
) {
|
||||||
const { code, state, installation_id }: Query = req.query as Query;
|
const { code, state, installation_id, userId }: Query = req.query as Query;
|
||||||
|
|
||||||
if (!code) {
|
if (!code) {
|
||||||
return res.status(400).json({ error: "Missing code parameter" });
|
return res.status(400).json({ error: "Missing code parameter" });
|
||||||
@@ -44,6 +45,7 @@ export default async function handler(
|
|||||||
githubPrivateKey: data.pem,
|
githubPrivateKey: data.pem,
|
||||||
},
|
},
|
||||||
value as string,
|
value as string,
|
||||||
|
userId,
|
||||||
);
|
);
|
||||||
} else if (action === "gh_setup") {
|
} else if (action === "gh_setup") {
|
||||||
await db
|
await db
|
||||||
|
|||||||
@@ -25,7 +25,11 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
.input(apiCreateGitlab)
|
.input(apiCreateGitlab)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
return await createGitlab(input, ctx.session.activeOrganizationId);
|
return await createGitlab(
|
||||||
|
input,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
ctx.session.userId,
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "BAD_REQUEST",
|
code: "BAD_REQUEST",
|
||||||
@@ -40,7 +44,8 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
const gitlabProvider = await findGitlabById(input.gitlabId);
|
const gitlabProvider = await findGitlabById(input.gitlabId);
|
||||||
if (
|
if (
|
||||||
gitlabProvider.gitProvider.organizationId !==
|
gitlabProvider.gitProvider.organizationId !==
|
||||||
ctx.session.activeOrganizationId
|
ctx.session.activeOrganizationId &&
|
||||||
|
gitlabProvider.gitProvider.userId !== ctx.session.userId
|
||||||
) {
|
) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "UNAUTHORIZED",
|
code: "UNAUTHORIZED",
|
||||||
@@ -56,11 +61,13 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
result = result.filter(
|
result = result.filter((provider) => {
|
||||||
(provider) =>
|
return (
|
||||||
provider.gitProvider.organizationId ===
|
provider.gitProvider.organizationId ===
|
||||||
ctx.session.activeOrganizationId,
|
ctx.session.activeOrganizationId &&
|
||||||
);
|
provider.gitProvider.userId === ctx.session.userId
|
||||||
|
);
|
||||||
|
});
|
||||||
const filtered = result
|
const filtered = result
|
||||||
.filter((provider) => haveGitlabRequirements(provider))
|
.filter((provider) => haveGitlabRequirements(provider))
|
||||||
.map((provider) => {
|
.map((provider) => {
|
||||||
@@ -80,7 +87,8 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
const gitlabProvider = await findGitlabById(input.gitlabId);
|
const gitlabProvider = await findGitlabById(input.gitlabId);
|
||||||
if (
|
if (
|
||||||
gitlabProvider.gitProvider.organizationId !==
|
gitlabProvider.gitProvider.organizationId !==
|
||||||
ctx.session.activeOrganizationId
|
ctx.session.activeOrganizationId &&
|
||||||
|
gitlabProvider.gitProvider.userId !== ctx.session.userId
|
||||||
) {
|
) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "UNAUTHORIZED",
|
code: "UNAUTHORIZED",
|
||||||
@@ -96,7 +104,8 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
const gitlabProvider = await findGitlabById(input.gitlabId || "");
|
const gitlabProvider = await findGitlabById(input.gitlabId || "");
|
||||||
if (
|
if (
|
||||||
gitlabProvider.gitProvider.organizationId !==
|
gitlabProvider.gitProvider.organizationId !==
|
||||||
ctx.session.activeOrganizationId
|
ctx.session.activeOrganizationId &&
|
||||||
|
gitlabProvider.gitProvider.userId !== ctx.session.userId
|
||||||
) {
|
) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "UNAUTHORIZED",
|
code: "UNAUTHORIZED",
|
||||||
@@ -112,7 +121,8 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
const gitlabProvider = await findGitlabById(input.gitlabId || "");
|
const gitlabProvider = await findGitlabById(input.gitlabId || "");
|
||||||
if (
|
if (
|
||||||
gitlabProvider.gitProvider.organizationId !==
|
gitlabProvider.gitProvider.organizationId !==
|
||||||
ctx.session.activeOrganizationId
|
ctx.session.activeOrganizationId &&
|
||||||
|
gitlabProvider.gitProvider.userId !== ctx.session.userId
|
||||||
) {
|
) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "UNAUTHORIZED",
|
code: "UNAUTHORIZED",
|
||||||
@@ -135,7 +145,8 @@ export const gitlabRouter = createTRPCRouter({
|
|||||||
const gitlabProvider = await findGitlabById(input.gitlabId);
|
const gitlabProvider = await findGitlabById(input.gitlabId);
|
||||||
if (
|
if (
|
||||||
gitlabProvider.gitProvider.organizationId !==
|
gitlabProvider.gitProvider.organizationId !==
|
||||||
ctx.session.activeOrganizationId
|
ctx.session.activeOrganizationId &&
|
||||||
|
gitlabProvider.gitProvider.userId !== ctx.session.userId
|
||||||
) {
|
) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "UNAUTHORIZED",
|
code: "UNAUTHORIZED",
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export type Github = typeof github.$inferSelect;
|
|||||||
export const createGithub = async (
|
export const createGithub = async (
|
||||||
input: typeof apiCreateGithub._type,
|
input: typeof apiCreateGithub._type,
|
||||||
organizationId: string,
|
organizationId: string,
|
||||||
|
userId: string,
|
||||||
) => {
|
) => {
|
||||||
return await db.transaction(async (tx) => {
|
return await db.transaction(async (tx) => {
|
||||||
const newGitProvider = await tx
|
const newGitProvider = await tx
|
||||||
@@ -21,6 +22,7 @@ export const createGithub = async (
|
|||||||
providerType: "github",
|
providerType: "github",
|
||||||
organizationId: organizationId,
|
organizationId: organizationId,
|
||||||
name: input.name,
|
name: input.name,
|
||||||
|
userId: userId,
|
||||||
})
|
})
|
||||||
.returning()
|
.returning()
|
||||||
.then((response) => response[0]);
|
.then((response) => response[0]);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export type Gitlab = typeof gitlab.$inferSelect;
|
|||||||
export const createGitlab = async (
|
export const createGitlab = async (
|
||||||
input: typeof apiCreateGitlab._type,
|
input: typeof apiCreateGitlab._type,
|
||||||
organizationId: string,
|
organizationId: string,
|
||||||
|
userId: string,
|
||||||
) => {
|
) => {
|
||||||
return await db.transaction(async (tx) => {
|
return await db.transaction(async (tx) => {
|
||||||
const newGitProvider = await tx
|
const newGitProvider = await tx
|
||||||
@@ -20,6 +21,7 @@ export const createGitlab = async (
|
|||||||
providerType: "gitlab",
|
providerType: "gitlab",
|
||||||
organizationId: organizationId,
|
organizationId: organizationId,
|
||||||
name: input.name,
|
name: input.name,
|
||||||
|
userId: userId,
|
||||||
})
|
})
|
||||||
.returning()
|
.returning()
|
||||||
.then((response) => response[0]);
|
.then((response) => response[0]);
|
||||||
|
|||||||
Reference in New Issue
Block a user