diff --git a/apps/dokploy/components/dashboard/projects/handle-project.tsx b/apps/dokploy/components/dashboard/projects/handle-project.tsx index 5b077771..fb2cbf67 100644 --- a/apps/dokploy/components/dashboard/projects/handle-project.tsx +++ b/apps/dokploy/components/dashboard/projects/handle-project.tsx @@ -123,26 +123,10 @@ export const HandleProject = ({ projectId }: Props) => { Update ) : ( - <> - - - + )} diff --git a/apps/dokploy/pages/index.tsx b/apps/dokploy/pages/index.tsx index 4c53f479..0fecc209 100644 --- a/apps/dokploy/pages/index.tsx +++ b/apps/dokploy/pages/index.tsx @@ -60,17 +60,18 @@ interface Props { IS_CLOUD: boolean; } export default function Home({ IS_CLOUD }: Props) { + const [isLoading, setIsLoading] = useState(false); + const [isError, setIsError] = useState(false); + const [error, setError] = useState(null); const [temp, setTemp] = useState({ is2FAEnabled: false, authId: "", }); - const { mutateAsync, isLoading, error, isError } = - api.auth.login.useMutation(); const router = useRouter(); const form = useForm({ defaultValues: { - email: "", - password: "", + email: "siumauricio@hotmail.com", + password: "Password123", }, resolver: zodResolver(loginSchema), }); @@ -80,6 +81,7 @@ export default function Home({ IS_CLOUD }: Props) { }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (values: Login) => { + setIsLoading(true); const { data, error } = await authClient.signIn.email({ email: values.email, password: values.password, @@ -92,15 +94,17 @@ export default function Home({ IS_CLOUD }: Props) { toast.success("Successfully signed in", { duration: 2000, }); - // router.push("/dashboard/projects"); + router.push("/dashboard/projects"); // } } else { + setIsError(true); + setError(error.message ?? "Error to signup"); toast.error("Error to sign up", { description: error.message, }); } - console.log(data, error); + setIsLoading(false); // await mutateAsync({ // email: values.email.toLowerCase(), // password: values.password, @@ -136,7 +140,7 @@ export default function Home({ IS_CLOUD }: Props) { {isError && ( - {error?.message} + {error} )} diff --git a/apps/dokploy/server/api/routers/admin.ts b/apps/dokploy/server/api/routers/admin.ts index 6a4764f5..0b232f6d 100644 --- a/apps/dokploy/server/api/routers/admin.ts +++ b/apps/dokploy/server/api/routers/admin.ts @@ -6,18 +6,15 @@ import { apiRemoveUser, apiUpdateAdmin, apiUpdateWebServerMonitoring, - user, } from "@/server/db/schema"; import { IS_CLOUD, createInvitation, - findAdminById, findUserByAuthId, findUserById, getUserByToken, - removeUserByAuthId, + removeUserById, setupWebMonitoring, - updateAdmin, updateAdminById, } from "@dokploy/server"; import { TRPCError } from "@trpc/server"; @@ -32,7 +29,7 @@ import { export const adminRouter = createTRPCRouter({ one: adminProcedure.query(async ({ ctx }) => { - const { sshPrivateKey, ...rest } = await findAdminById(ctx.user.adminId); + const { sshPrivateKey, ...rest } = await findUserById(ctx.user.id); return { haveSSH: !!sshPrivateKey, ...rest, @@ -47,15 +44,15 @@ export const adminRouter = createTRPCRouter({ message: "You are not allowed to update this admin", }); } - const { authId } = await findAdminById(ctx.user.adminId); + const { id } = await findUserById(ctx.user.id); // @ts-ignore - return updateAdmin(authId, input); + return updateAdmin(id, input); }), createUserInvitation: adminProcedure .input(apiCreateUserInvitation) .mutation(async ({ input, ctx }) => { try { - await createInvitation(input, ctx.user.adminId); + await createInvitation(input, ctx.user.id); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -69,15 +66,16 @@ export const adminRouter = createTRPCRouter({ .input(apiRemoveUser) .mutation(async ({ input, ctx }) => { try { - const user = await findUserByAuthId(input.authId); + const user = await findUserById(input.id); - if (user.adminId !== ctx.user.adminId) { + if (user.id !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to delete this user", }); } - return await removeUserByAuthId(input.authId); + + return await removeUserById(input.id); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -95,20 +93,20 @@ export const adminRouter = createTRPCRouter({ .input(apiAssignPermissions) .mutation(async ({ input, ctx }) => { try { - const user = await findUserById(input.userId); + const user = await findUserById(input.id); - if (user.adminId !== ctx.user.adminId) { + if (user.id !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to assign permissions", }); } - await db - .update(users) - .set({ - ...input, - }) - .where(eq(users.userId, input.userId)); + // await db + // .update(users) + // .set({ + // ...input, + // }) + // .where(eq(users.userId, input.userId)); } catch (error) { throw error; } @@ -124,50 +122,50 @@ export const adminRouter = createTRPCRouter({ message: "Feature disabled on cloud", }); } - const admin = await findAdminById(ctx.user.adminId); - if (admin.adminId !== ctx.user.adminId) { + const user = await findUserById(ctx.user.ownerId); + if (user.id !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to setup this server", }); } - await updateAdminById(admin.adminId, { - metricsConfig: { - server: { - type: "Dokploy", - refreshRate: input.metricsConfig.server.refreshRate, - port: input.metricsConfig.server.port, - token: input.metricsConfig.server.token, - cronJob: input.metricsConfig.server.cronJob, - urlCallback: input.metricsConfig.server.urlCallback, - retentionDays: input.metricsConfig.server.retentionDays, - thresholds: { - cpu: input.metricsConfig.server.thresholds.cpu, - memory: input.metricsConfig.server.thresholds.memory, - }, - }, - containers: { - refreshRate: input.metricsConfig.containers.refreshRate, - services: { - include: input.metricsConfig.containers.services.include || [], - exclude: input.metricsConfig.containers.services.exclude || [], - }, - }, - }, - }); - const currentServer = await setupWebMonitoring(admin.adminId); - return currentServer; + // await updateAdminById(admin.adminId, { + // metricsConfig: { + // server: { + // type: "Dokploy", + // refreshRate: input.metricsConfig.server.refreshRate, + // port: input.metricsConfig.server.port, + // token: input.metricsConfig.server.token, + // cronJob: input.metricsConfig.server.cronJob, + // urlCallback: input.metricsConfig.server.urlCallback, + // retentionDays: input.metricsConfig.server.retentionDays, + // thresholds: { + // cpu: input.metricsConfig.server.thresholds.cpu, + // memory: input.metricsConfig.server.thresholds.memory, + // }, + // }, + // containers: { + // refreshRate: input.metricsConfig.containers.refreshRate, + // services: { + // include: input.metricsConfig.containers.services.include || [], + // exclude: input.metricsConfig.containers.services.exclude || [], + // }, + // }, + // }, + // }); + // const currentServer = await setupWebMonitoring(admin.adminId); + // return currentServer; } catch (error) { throw error; } }), getMetricsToken: protectedProcedure.query(async ({ ctx }) => { - const admin = await findAdminById(ctx.user.adminId); + const user = await findUserById(ctx.user.ownerId); return { - serverIp: admin.serverIp, - enabledFeatures: admin.enablePaidFeatures, - metricsConfig: admin?.metricsConfig, + serverIp: user.serverIp, + enabledFeatures: user.enablePaidFeatures, + metricsConfig: user?.metricsConfig, }; }), diff --git a/apps/dokploy/server/api/routers/application.ts b/apps/dokploy/server/api/routers/application.ts index 13b7c80d..d07caa43 100644 --- a/apps/dokploy/server/api/routers/application.ts +++ b/apps/dokploy/server/api/routers/application.ts @@ -72,7 +72,7 @@ export const applicationRouter = createTRPCRouter({ } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -106,7 +106,7 @@ export const applicationRouter = createTRPCRouter({ ); } const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -119,7 +119,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiReloadApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to reload this application", @@ -153,7 +153,7 @@ export const applicationRouter = createTRPCRouter({ } const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this application", @@ -194,7 +194,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const service = await findApplicationById(input.applicationId); - if (service.project.adminId !== ctx.user.adminId) { + if (service.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this application", @@ -214,7 +214,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const service = await findApplicationById(input.applicationId); - if (service.project.adminId !== ctx.user.adminId) { + if (service.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to start this application", @@ -235,7 +235,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to redeploy this application", @@ -268,7 +268,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveEnvironmentVariables) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this environment", @@ -284,7 +284,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveBuildType) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this build type", @@ -305,7 +305,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveGithubProvider) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this github provider", @@ -327,7 +327,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveGitlabProvider) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this gitlab provider", @@ -351,7 +351,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveBitbucketProvider) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this bitbucket provider", @@ -373,7 +373,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveDockerProvider) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this docker provider", @@ -394,7 +394,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiSaveGitProvider) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this git provider", @@ -415,7 +415,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to mark this application as running", @@ -427,7 +427,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiUpdateApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this application", @@ -451,7 +451,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to refresh this application", @@ -466,7 +466,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this application", @@ -500,7 +500,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to clean this application", @@ -513,7 +513,7 @@ export const applicationRouter = createTRPCRouter({ .input(apiFindOneApplication) .query(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to read this application", @@ -548,7 +548,7 @@ export const applicationRouter = createTRPCRouter({ const app = await findApplicationById(input.applicationId as string); - if (app.project.adminId !== ctx.user.adminId) { + if (app.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this application", @@ -590,7 +590,7 @@ export const applicationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this application", diff --git a/apps/dokploy/server/api/routers/auth.ts b/apps/dokploy/server/api/routers/auth.ts index c1c2e0a7..cc88f2c3 100644 --- a/apps/dokploy/server/api/routers/auth.ts +++ b/apps/dokploy/server/api/routers/auth.ts @@ -21,7 +21,6 @@ import { lucia, luciaToken, removeAdminByAuthId, - removeUserByAuthId, sendDiscordNotification, sendEmailNotification, updateAuthById, diff --git a/apps/dokploy/server/api/routers/bitbucket.ts b/apps/dokploy/server/api/routers/bitbucket.ts index c66716d3..75513a80 100644 --- a/apps/dokploy/server/api/routers/bitbucket.ts +++ b/apps/dokploy/server/api/routers/bitbucket.ts @@ -23,7 +23,7 @@ export const bitbucketRouter = createTRPCRouter({ .input(apiCreateBitbucket) .mutation(async ({ input, ctx }) => { try { - return await createBitbucket(input, ctx.user.adminId); + return await createBitbucket(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -38,7 +38,7 @@ export const bitbucketRouter = createTRPCRouter({ const bitbucketProvider = await findBitbucketById(input.bitbucketId); if ( IS_CLOUD && - bitbucketProvider.gitProvider.adminId !== ctx.user.adminId + bitbucketProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -61,7 +61,7 @@ export const bitbucketRouter = createTRPCRouter({ if (IS_CLOUD) { // TODO: mAyBe a rEfaCtoR 🤫 result = result.filter( - (provider) => provider.gitProvider.adminId === ctx.user.adminId, + (provider) => provider.gitProvider.userId === ctx.user.ownerId, ); } return result; @@ -73,7 +73,7 @@ export const bitbucketRouter = createTRPCRouter({ const bitbucketProvider = await findBitbucketById(input.bitbucketId); if ( IS_CLOUD && - bitbucketProvider.gitProvider.adminId !== ctx.user.adminId + bitbucketProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -91,7 +91,7 @@ export const bitbucketRouter = createTRPCRouter({ ); if ( IS_CLOUD && - bitbucketProvider.gitProvider.adminId !== ctx.user.adminId + bitbucketProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -108,7 +108,7 @@ export const bitbucketRouter = createTRPCRouter({ const bitbucketProvider = await findBitbucketById(input.bitbucketId); if ( IS_CLOUD && - bitbucketProvider.gitProvider.adminId !== ctx.user.adminId + bitbucketProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -132,7 +132,7 @@ export const bitbucketRouter = createTRPCRouter({ const bitbucketProvider = await findBitbucketById(input.bitbucketId); if ( IS_CLOUD && - bitbucketProvider.gitProvider.adminId !== ctx.user.adminId + bitbucketProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -142,7 +142,7 @@ export const bitbucketRouter = createTRPCRouter({ } return await updateBitbucket(input.bitbucketId, { ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); }), }); diff --git a/apps/dokploy/server/api/routers/certificate.ts b/apps/dokploy/server/api/routers/certificate.ts index 0f8d6fd9..b1c6f5c2 100644 --- a/apps/dokploy/server/api/routers/certificate.ts +++ b/apps/dokploy/server/api/routers/certificate.ts @@ -25,14 +25,14 @@ export const certificateRouter = createTRPCRouter({ message: "Please set a server to create a certificate", }); } - return await createCertificate(input, ctx.user.adminId); + return await createCertificate(input, ctx.user.ownerId); }), one: adminProcedure .input(apiFindCertificate) .query(async ({ input, ctx }) => { const certificates = await findCertificateById(input.certificateId); - if (IS_CLOUD && certificates.adminId !== ctx.user.adminId) { + if (IS_CLOUD && certificates.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to access this certificate", @@ -44,7 +44,7 @@ export const certificateRouter = createTRPCRouter({ .input(apiFindCertificate) .mutation(async ({ input, ctx }) => { const certificates = await findCertificateById(input.certificateId); - if (IS_CLOUD && certificates.adminId !== ctx.user.adminId) { + if (IS_CLOUD && certificates.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to delete this certificate", @@ -56,7 +56,7 @@ export const certificateRouter = createTRPCRouter({ all: adminProcedure.query(async ({ ctx }) => { return await db.query.certificates.findMany({ // TODO: Remove this line when the cloud version is ready - ...(IS_CLOUD && { where: eq(certificates.adminId, ctx.user.adminId) }), + ...(IS_CLOUD && { where: eq(certificates.userId, ctx.user.ownerId) }), }); }), }); diff --git a/apps/dokploy/server/api/routers/compose.ts b/apps/dokploy/server/api/routers/compose.ts index 463d0398..5d5c1173 100644 --- a/apps/dokploy/server/api/routers/compose.ts +++ b/apps/dokploy/server/api/routers/compose.ts @@ -71,7 +71,7 @@ export const composeRouter = createTRPCRouter({ }); } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -97,7 +97,7 @@ export const composeRouter = createTRPCRouter({ } const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", @@ -110,7 +110,7 @@ export const composeRouter = createTRPCRouter({ .input(apiUpdateCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this compose", @@ -126,7 +126,7 @@ export const composeRouter = createTRPCRouter({ } const composeResult = await findComposeById(input.composeId); - if (composeResult.project.adminId !== ctx.user.adminId) { + if (composeResult.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this compose", @@ -157,7 +157,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFindCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to clean this compose", @@ -170,7 +170,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFetchServices) .query(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to load this compose", @@ -184,7 +184,7 @@ export const composeRouter = createTRPCRouter({ try { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to fetch this compose", @@ -209,7 +209,7 @@ export const composeRouter = createTRPCRouter({ .input(apiRandomizeCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to randomize this compose", @@ -221,7 +221,7 @@ export const composeRouter = createTRPCRouter({ .input(apiRandomizeCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to randomize this compose", @@ -236,7 +236,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFindCompose) .query(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to get this compose", @@ -254,7 +254,7 @@ export const composeRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this compose", @@ -287,7 +287,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFindCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to redeploy this compose", @@ -319,7 +319,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFindCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this compose", @@ -333,7 +333,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFindCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this compose", @@ -348,7 +348,7 @@ export const composeRouter = createTRPCRouter({ .query(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to get this compose", @@ -361,7 +361,7 @@ export const composeRouter = createTRPCRouter({ .input(apiFindCompose) .mutation(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to refresh this compose", diff --git a/apps/dokploy/server/api/routers/deployment.ts b/apps/dokploy/server/api/routers/deployment.ts index bf981c6d..b9daaf0e 100644 --- a/apps/dokploy/server/api/routers/deployment.ts +++ b/apps/dokploy/server/api/routers/deployment.ts @@ -19,7 +19,7 @@ export const deploymentRouter = createTRPCRouter({ .input(apiFindAllByApplication) .query(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -32,7 +32,7 @@ export const deploymentRouter = createTRPCRouter({ .input(apiFindAllByCompose) .query(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", @@ -44,7 +44,7 @@ export const deploymentRouter = createTRPCRouter({ .input(apiFindAllByServer) .query(async ({ input, ctx }) => { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this server", diff --git a/apps/dokploy/server/api/routers/destination.ts b/apps/dokploy/server/api/routers/destination.ts index d13928be..49edadf5 100644 --- a/apps/dokploy/server/api/routers/destination.ts +++ b/apps/dokploy/server/api/routers/destination.ts @@ -28,7 +28,7 @@ export const destinationRouter = createTRPCRouter({ .input(apiCreateDestination) .mutation(async ({ input, ctx }) => { try { - return await createDestintation(input, ctx.user.adminId); + return await createDestintation(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -84,7 +84,7 @@ export const destinationRouter = createTRPCRouter({ .input(apiFindOneDestination) .query(async ({ input, ctx }) => { const destination = await findDestinationById(input.destinationId); - if (destination.adminId !== ctx.user.adminId) { + if (destination.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to access this destination", @@ -94,7 +94,7 @@ export const destinationRouter = createTRPCRouter({ }), all: protectedProcedure.query(async ({ ctx }) => { return await db.query.destinations.findMany({ - where: eq(destinations.adminId, ctx.user.adminId), + where: eq(destinations.userId, ctx.user.ownerId), }); }), remove: adminProcedure @@ -103,7 +103,7 @@ export const destinationRouter = createTRPCRouter({ try { const destination = await findDestinationById(input.destinationId); - if (destination.adminId !== ctx.user.adminId) { + if (destination.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to delete this destination", @@ -111,7 +111,7 @@ export const destinationRouter = createTRPCRouter({ } return await removeDestinationById( input.destinationId, - ctx.user.adminId, + ctx.user.ownerId, ); } catch (error) { throw error; @@ -122,7 +122,7 @@ export const destinationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const destination = await findDestinationById(input.destinationId); - if (destination.adminId !== ctx.user.adminId) { + if (destination.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to update this destination", @@ -130,7 +130,7 @@ export const destinationRouter = createTRPCRouter({ } return await updateDestinationById(input.destinationId, { ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw error; diff --git a/apps/dokploy/server/api/routers/domain.ts b/apps/dokploy/server/api/routers/domain.ts index f122cf86..526a156b 100644 --- a/apps/dokploy/server/api/routers/domain.ts +++ b/apps/dokploy/server/api/routers/domain.ts @@ -30,7 +30,7 @@ export const domainRouter = createTRPCRouter({ try { if (input.domainType === "compose" && input.composeId) { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", @@ -38,7 +38,7 @@ export const domainRouter = createTRPCRouter({ } } else if (input.domainType === "application" && input.applicationId) { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -58,7 +58,7 @@ export const domainRouter = createTRPCRouter({ .input(apiFindOneApplication) .query(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -70,7 +70,7 @@ export const domainRouter = createTRPCRouter({ .input(apiFindCompose) .query(async ({ input, ctx }) => { const compose = await findComposeById(input.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", @@ -83,7 +83,7 @@ export const domainRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { return generateTraefikMeDomain( input.appName, - ctx.user.adminId, + ctx.user.ownerId, input.serverId, ); }), @@ -95,7 +95,7 @@ export const domainRouter = createTRPCRouter({ if (currentDomain.applicationId) { const newApp = await findApplicationById(currentDomain.applicationId); - if (newApp.project.adminId !== ctx.user.adminId) { + if (newApp.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -103,7 +103,7 @@ export const domainRouter = createTRPCRouter({ } } else if (currentDomain.composeId) { const newCompose = await findComposeById(currentDomain.composeId); - if (newCompose.project.adminId !== ctx.user.adminId) { + if (newCompose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", @@ -114,7 +114,7 @@ export const domainRouter = createTRPCRouter({ currentDomain.previewDeploymentId, ); if ( - newPreviewDeployment.application.project.adminId !== ctx.user.adminId + newPreviewDeployment.application.project.userId !== ctx.user.ownerId ) { throw new TRPCError({ code: "UNAUTHORIZED", @@ -143,7 +143,7 @@ export const domainRouter = createTRPCRouter({ const domain = await findDomainById(input.domainId); if (domain.applicationId) { const application = await findApplicationById(domain.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -151,7 +151,7 @@ export const domainRouter = createTRPCRouter({ } } else if (domain.composeId) { const compose = await findComposeById(domain.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", @@ -166,7 +166,7 @@ export const domainRouter = createTRPCRouter({ const domain = await findDomainById(input.domainId); if (domain.applicationId) { const application = await findApplicationById(domain.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -174,7 +174,7 @@ export const domainRouter = createTRPCRouter({ } } else if (domain.composeId) { const compose = await findComposeById(domain.composeId); - if (compose.project.adminId !== ctx.user.adminId) { + if (compose.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this compose", diff --git a/apps/dokploy/server/api/routers/git-provider.ts b/apps/dokploy/server/api/routers/git-provider.ts index abd93392..83e71dd8 100644 --- a/apps/dokploy/server/api/routers/git-provider.ts +++ b/apps/dokploy/server/api/routers/git-provider.ts @@ -18,7 +18,7 @@ export const gitProviderRouter = createTRPCRouter({ github: true, }, orderBy: desc(gitProvider.createdAt), - ...(IS_CLOUD && { where: eq(gitProvider.adminId, ctx.user.adminId) }), + ...(IS_CLOUD && { where: eq(gitProvider.userId, ctx.user.ownerId) }), //TODO: Remove this line when the cloud version is ready }); }), @@ -28,7 +28,7 @@ export const gitProviderRouter = createTRPCRouter({ try { const gitProvider = await findGitProviderById(input.gitProviderId); - if (IS_CLOUD && gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && gitProvider.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", diff --git a/apps/dokploy/server/api/routers/github.ts b/apps/dokploy/server/api/routers/github.ts index 56222577..62c21805 100644 --- a/apps/dokploy/server/api/routers/github.ts +++ b/apps/dokploy/server/api/routers/github.ts @@ -20,7 +20,7 @@ export const githubRouter = createTRPCRouter({ .input(apiFindOneGithub) .query(async ({ input, ctx }) => { const githubProvider = await findGithubById(input.githubId); - if (IS_CLOUD && githubProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && githubProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -33,7 +33,7 @@ export const githubRouter = createTRPCRouter({ .input(apiFindOneGithub) .query(async ({ input, ctx }) => { const githubProvider = await findGithubById(input.githubId); - if (IS_CLOUD && githubProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && githubProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -46,7 +46,7 @@ export const githubRouter = createTRPCRouter({ .input(apiFindGithubBranches) .query(async ({ input, ctx }) => { const githubProvider = await findGithubById(input.githubId || ""); - if (IS_CLOUD && githubProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && githubProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -65,7 +65,7 @@ export const githubRouter = createTRPCRouter({ if (IS_CLOUD) { // TODO: mAyBe a rEfaCtoR 🤫 result = result.filter( - (provider) => provider.gitProvider.adminId === ctx.user.adminId, + (provider) => provider.gitProvider.userId === ctx.user.ownerId, ); } @@ -90,7 +90,7 @@ export const githubRouter = createTRPCRouter({ const githubProvider = await findGithubById(input.githubId); if ( IS_CLOUD && - githubProvider.gitProvider.adminId !== ctx.user.adminId + githubProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -111,7 +111,7 @@ export const githubRouter = createTRPCRouter({ .input(apiUpdateGithub) .mutation(async ({ input, ctx }) => { const githubProvider = await findGithubById(input.githubId); - if (IS_CLOUD && githubProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && githubProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -120,7 +120,7 @@ export const githubRouter = createTRPCRouter({ } await updateGitProvider(input.gitProviderId, { name: input.name, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); }), }); diff --git a/apps/dokploy/server/api/routers/gitlab.ts b/apps/dokploy/server/api/routers/gitlab.ts index 6d35f4a2..d3ba5a44 100644 --- a/apps/dokploy/server/api/routers/gitlab.ts +++ b/apps/dokploy/server/api/routers/gitlab.ts @@ -26,7 +26,7 @@ export const gitlabRouter = createTRPCRouter({ .input(apiCreateGitlab) .mutation(async ({ input, ctx }) => { try { - return await createGitlab(input, ctx.user.adminId); + return await createGitlab(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -39,7 +39,7 @@ export const gitlabRouter = createTRPCRouter({ .input(apiFindOneGitlab) .query(async ({ input, ctx }) => { const gitlabProvider = await findGitlabById(input.gitlabId); - if (IS_CLOUD && gitlabProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && gitlabProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -58,7 +58,7 @@ export const gitlabRouter = createTRPCRouter({ if (IS_CLOUD) { // TODO: mAyBe a rEfaCtoR 🤫 result = result.filter( - (provider) => provider.gitProvider.adminId === ctx.user.adminId, + (provider) => provider.gitProvider.userId === ctx.user.ownerId, ); } const filtered = result @@ -78,7 +78,7 @@ export const gitlabRouter = createTRPCRouter({ .input(apiFindOneGitlab) .query(async ({ input, ctx }) => { const gitlabProvider = await findGitlabById(input.gitlabId); - if (IS_CLOUD && gitlabProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && gitlabProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -92,7 +92,7 @@ export const gitlabRouter = createTRPCRouter({ .input(apiFindGitlabBranches) .query(async ({ input, ctx }) => { const gitlabProvider = await findGitlabById(input.gitlabId || ""); - if (IS_CLOUD && gitlabProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && gitlabProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -108,7 +108,7 @@ export const gitlabRouter = createTRPCRouter({ const gitlabProvider = await findGitlabById(input.gitlabId || ""); if ( IS_CLOUD && - gitlabProvider.gitProvider.adminId !== ctx.user.adminId + gitlabProvider.gitProvider.userId !== ctx.user.ownerId ) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ @@ -130,7 +130,7 @@ export const gitlabRouter = createTRPCRouter({ .input(apiUpdateGitlab) .mutation(async ({ input, ctx }) => { const gitlabProvider = await findGitlabById(input.gitlabId); - if (IS_CLOUD && gitlabProvider.gitProvider.adminId !== ctx.user.adminId) { + if (IS_CLOUD && gitlabProvider.gitProvider.userId !== ctx.user.ownerId) { //TODO: Remove this line when the cloud version is ready throw new TRPCError({ code: "UNAUTHORIZED", @@ -140,7 +140,7 @@ export const gitlabRouter = createTRPCRouter({ if (input.name) { await updateGitProvider(input.gitProviderId, { name: input.name, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); await updateGitlab(input.gitlabId, { diff --git a/apps/dokploy/server/api/routers/mariadb.ts b/apps/dokploy/server/api/routers/mariadb.ts index 6e85d274..283455fa 100644 --- a/apps/dokploy/server/api/routers/mariadb.ts +++ b/apps/dokploy/server/api/routers/mariadb.ts @@ -49,7 +49,7 @@ export const mariadbRouter = createTRPCRouter({ } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -83,7 +83,7 @@ export const mariadbRouter = createTRPCRouter({ await checkServiceAccess(ctx.user.authId, input.mariadbId, "access"); } const mariadb = await findMariadbById(input.mariadbId); - if (mariadb.project.adminId !== ctx.user.adminId) { + if (mariadb.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this Mariadb", @@ -96,7 +96,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiFindOneMariaDB) .mutation(async ({ input, ctx }) => { const service = await findMariadbById(input.mariadbId); - if (service.project.adminId !== ctx.user.adminId) { + if (service.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to start this Mariadb", @@ -133,7 +133,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiSaveExternalPortMariaDB) .mutation(async ({ input, ctx }) => { const mongo = await findMariadbById(input.mariadbId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this external port", @@ -149,7 +149,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiDeployMariaDB) .mutation(async ({ input, ctx }) => { const mariadb = await findMariadbById(input.mariadbId); - if (mariadb.project.adminId !== ctx.user.adminId) { + if (mariadb.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this Mariadb", @@ -170,7 +170,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiDeployMariaDB) .subscription(async ({ input, ctx }) => { const mariadb = await findMariadbById(input.mariadbId); - if (mariadb.project.adminId !== ctx.user.adminId) { + if (mariadb.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this Mariadb", @@ -187,7 +187,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiChangeMariaDBStatus) .mutation(async ({ input, ctx }) => { const mongo = await findMariadbById(input.mariadbId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to change this Mariadb status", @@ -206,7 +206,7 @@ export const mariadbRouter = createTRPCRouter({ } const mongo = await findMariadbById(input.mariadbId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this Mariadb", @@ -232,7 +232,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiSaveEnvironmentVariablesMariaDB) .mutation(async ({ input, ctx }) => { const mariadb = await findMariadbById(input.mariadbId); - if (mariadb.project.adminId !== ctx.user.adminId) { + if (mariadb.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this environment", @@ -255,7 +255,7 @@ export const mariadbRouter = createTRPCRouter({ .input(apiResetMariadb) .mutation(async ({ input, ctx }) => { const mariadb = await findMariadbById(input.mariadbId); - if (mariadb.project.adminId !== ctx.user.adminId) { + if (mariadb.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to reload this Mariadb", @@ -285,7 +285,7 @@ export const mariadbRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { mariadbId, ...rest } = input; const mariadb = await findMariadbById(mariadbId); - if (mariadb.project.adminId !== ctx.user.adminId) { + if (mariadb.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this Mariadb", diff --git a/apps/dokploy/server/api/routers/mongo.ts b/apps/dokploy/server/api/routers/mongo.ts index 2bca3ec5..94a01272 100644 --- a/apps/dokploy/server/api/routers/mongo.ts +++ b/apps/dokploy/server/api/routers/mongo.ts @@ -48,7 +48,7 @@ export const mongoRouter = createTRPCRouter({ } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -87,7 +87,7 @@ export const mongoRouter = createTRPCRouter({ } const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this mongo", @@ -101,7 +101,7 @@ export const mongoRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const service = await findMongoById(input.mongoId); - if (service.project.adminId !== ctx.user.adminId) { + if (service.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to start this mongo", @@ -124,7 +124,7 @@ export const mongoRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this mongo", @@ -146,7 +146,7 @@ export const mongoRouter = createTRPCRouter({ .input(apiSaveExternalPortMongo) .mutation(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this external port", @@ -162,7 +162,7 @@ export const mongoRouter = createTRPCRouter({ .input(apiDeployMongo) .mutation(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this mongo", @@ -182,7 +182,7 @@ export const mongoRouter = createTRPCRouter({ .input(apiDeployMongo) .subscription(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this mongo", @@ -199,7 +199,7 @@ export const mongoRouter = createTRPCRouter({ .input(apiChangeMongoStatus) .mutation(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to change this mongo status", @@ -214,7 +214,7 @@ export const mongoRouter = createTRPCRouter({ .input(apiResetMongo) .mutation(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to reload this mongo", @@ -248,7 +248,7 @@ export const mongoRouter = createTRPCRouter({ const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this mongo", @@ -274,7 +274,7 @@ export const mongoRouter = createTRPCRouter({ .input(apiSaveEnvironmentVariablesMongo) .mutation(async ({ input, ctx }) => { const mongo = await findMongoById(input.mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this environment", @@ -298,7 +298,7 @@ export const mongoRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { mongoId, ...rest } = input; const mongo = await findMongoById(mongoId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this mongo", diff --git a/apps/dokploy/server/api/routers/mysql.ts b/apps/dokploy/server/api/routers/mysql.ts index 7ebf4623..efea7207 100644 --- a/apps/dokploy/server/api/routers/mysql.ts +++ b/apps/dokploy/server/api/routers/mysql.ts @@ -50,7 +50,7 @@ export const mysqlRouter = createTRPCRouter({ } 1; const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -89,7 +89,7 @@ export const mysqlRouter = createTRPCRouter({ await checkServiceAccess(ctx.user.authId, input.mysqlId, "access"); } const mysql = await findMySqlById(input.mysqlId); - if (mysql.project.adminId !== ctx.user.adminId) { + if (mysql.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this MySQL", @@ -102,7 +102,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiFindOneMySql) .mutation(async ({ input, ctx }) => { const service = await findMySqlById(input.mysqlId); - if (service.project.adminId !== ctx.user.adminId) { + if (service.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to start this MySQL", @@ -124,7 +124,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiFindOneMySql) .mutation(async ({ input, ctx }) => { const mongo = await findMySqlById(input.mysqlId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this MySQL", @@ -145,7 +145,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiSaveExternalPortMySql) .mutation(async ({ input, ctx }) => { const mongo = await findMySqlById(input.mysqlId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this external port", @@ -161,7 +161,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiDeployMySql) .mutation(async ({ input, ctx }) => { const mysql = await findMySqlById(input.mysqlId); - if (mysql.project.adminId !== ctx.user.adminId) { + if (mysql.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this MySQL", @@ -181,7 +181,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiDeployMySql) .subscription(async ({ input, ctx }) => { const mysql = await findMySqlById(input.mysqlId); - if (mysql.project.adminId !== ctx.user.adminId) { + if (mysql.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this MySQL", @@ -198,7 +198,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiChangeMySqlStatus) .mutation(async ({ input, ctx }) => { const mongo = await findMySqlById(input.mysqlId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to change this MySQL status", @@ -213,7 +213,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiResetMysql) .mutation(async ({ input, ctx }) => { const mysql = await findMySqlById(input.mysqlId); - if (mysql.project.adminId !== ctx.user.adminId) { + if (mysql.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to reload this MySQL", @@ -244,7 +244,7 @@ export const mysqlRouter = createTRPCRouter({ await checkServiceAccess(ctx.user.authId, input.mysqlId, "delete"); } const mongo = await findMySqlById(input.mysqlId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this MySQL", @@ -270,7 +270,7 @@ export const mysqlRouter = createTRPCRouter({ .input(apiSaveEnvironmentVariablesMySql) .mutation(async ({ input, ctx }) => { const mysql = await findMySqlById(input.mysqlId); - if (mysql.project.adminId !== ctx.user.adminId) { + if (mysql.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this environment", @@ -294,7 +294,7 @@ export const mysqlRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { mysqlId, ...rest } = input; const mysql = await findMySqlById(mysqlId); - if (mysql.project.adminId !== ctx.user.adminId) { + if (mysql.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this MySQL", diff --git a/apps/dokploy/server/api/routers/notification.ts b/apps/dokploy/server/api/routers/notification.ts index ea346973..6c2b469d 100644 --- a/apps/dokploy/server/api/routers/notification.ts +++ b/apps/dokploy/server/api/routers/notification.ts @@ -6,7 +6,6 @@ import { } from "@/server/api/trpc"; import { db } from "@/server/db"; import { - admins, apiCreateDiscord, apiCreateEmail, apiCreateGotify, @@ -25,6 +24,7 @@ import { apiUpdateTelegram, notifications, server, + users_temp, } from "@/server/db/schema"; import { IS_CLOUD, @@ -57,7 +57,7 @@ export const notificationRouter = createTRPCRouter({ .input(apiCreateSlack) .mutation(async ({ input, ctx }) => { try { - return await createSlackNotification(input, ctx.user.adminId); + return await createSlackNotification(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -71,7 +71,7 @@ export const notificationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -80,7 +80,7 @@ export const notificationRouter = createTRPCRouter({ } return await updateSlackNotification({ ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw error; @@ -107,7 +107,7 @@ export const notificationRouter = createTRPCRouter({ .input(apiCreateTelegram) .mutation(async ({ input, ctx }) => { try { - return await createTelegramNotification(input, ctx.user.adminId); + return await createTelegramNotification(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -122,7 +122,7 @@ export const notificationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -131,7 +131,7 @@ export const notificationRouter = createTRPCRouter({ } return await updateTelegramNotification({ ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw new TRPCError({ @@ -159,7 +159,7 @@ export const notificationRouter = createTRPCRouter({ .input(apiCreateDiscord) .mutation(async ({ input, ctx }) => { try { - return await createDiscordNotification(input, ctx.user.adminId); + return await createDiscordNotification(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -174,7 +174,7 @@ export const notificationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -183,7 +183,7 @@ export const notificationRouter = createTRPCRouter({ } return await updateDiscordNotification({ ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw new TRPCError({ @@ -220,7 +220,7 @@ export const notificationRouter = createTRPCRouter({ .input(apiCreateEmail) .mutation(async ({ input, ctx }) => { try { - return await createEmailNotification(input, ctx.user.adminId); + return await createEmailNotification(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -234,7 +234,7 @@ export const notificationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -243,7 +243,7 @@ export const notificationRouter = createTRPCRouter({ } return await updateEmailNotification({ ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw new TRPCError({ @@ -276,7 +276,7 @@ export const notificationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -295,7 +295,7 @@ export const notificationRouter = createTRPCRouter({ .input(apiFindOneNotification) .query(async ({ input, ctx }) => { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -314,7 +314,7 @@ export const notificationRouter = createTRPCRouter({ gotify: true, }, orderBy: desc(notifications.createdAt), - ...(IS_CLOUD && { where: eq(notifications.adminId, ctx.user.adminId) }), + ...(IS_CLOUD && { where: eq(notifications.userId, ctx.user.ownerId) }), // TODO: Remove this line when the cloud version is ready }); }), @@ -332,24 +332,24 @@ export const notificationRouter = createTRPCRouter({ ) .mutation(async ({ input }) => { try { - let adminId = ""; + let userId = ""; let ServerName = ""; if (input.ServerType === "Dokploy") { const result = await db .select() - .from(admins) + .from(users_temp) .where( - sql`${admins.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, + sql`${users_temp.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, ); - if (!result?.[0]?.adminId) { + if (!result?.[0]?.id) { throw new TRPCError({ code: "BAD_REQUEST", message: "Token not found", }); } - adminId = result?.[0]?.adminId; + userId = result?.[0]?.id; ServerName = "Dokploy"; } else { const result = await db @@ -359,18 +359,18 @@ export const notificationRouter = createTRPCRouter({ sql`${server.metricsConfig}::jsonb -> 'server' ->> 'token' = ${input.Token}`, ); - if (!result?.[0]?.adminId) { + if (!result?.[0]?.userId) { throw new TRPCError({ code: "BAD_REQUEST", message: "Token not found", }); } - adminId = result?.[0]?.adminId; + userId = result?.[0]?.userId; ServerName = "Remote"; } - await sendServerThresholdNotifications(adminId, { + await sendServerThresholdNotifications(userId, { ...input, ServerName, }); @@ -386,7 +386,7 @@ export const notificationRouter = createTRPCRouter({ .input(apiCreateGotify) .mutation(async ({ input, ctx }) => { try { - return await createGotifyNotification(input, ctx.user.adminId); + return await createGotifyNotification(input, ctx.user.ownerId); } catch (error) { throw new TRPCError({ code: "BAD_REQUEST", @@ -400,7 +400,7 @@ export const notificationRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const notification = await findNotificationById(input.notificationId); - if (IS_CLOUD && notification.adminId !== ctx.user.adminId) { + if (IS_CLOUD && notification.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this notification", @@ -408,7 +408,7 @@ export const notificationRouter = createTRPCRouter({ } return await updateGotifyNotification({ ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw error; diff --git a/apps/dokploy/server/api/routers/postgres.ts b/apps/dokploy/server/api/routers/postgres.ts index 92603a61..a7d0de39 100644 --- a/apps/dokploy/server/api/routers/postgres.ts +++ b/apps/dokploy/server/api/routers/postgres.ts @@ -56,7 +56,7 @@ export const postgresRouter = createTRPCRouter({ } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -95,7 +95,7 @@ export const postgresRouter = createTRPCRouter({ } const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this Postgres", @@ -109,7 +109,7 @@ export const postgresRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const service = await findPostgresById(input.postgresId); - if (service.project.adminId !== ctx.user.adminId) { + if (service.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to start this Postgres", @@ -131,7 +131,7 @@ export const postgresRouter = createTRPCRouter({ .input(apiFindOnePostgres) .mutation(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this Postgres", @@ -153,7 +153,7 @@ export const postgresRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this external port", @@ -169,7 +169,7 @@ export const postgresRouter = createTRPCRouter({ .input(apiDeployPostgres) .mutation(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this Postgres", @@ -190,7 +190,7 @@ export const postgresRouter = createTRPCRouter({ .input(apiDeployPostgres) .subscription(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this Postgres", @@ -207,7 +207,7 @@ export const postgresRouter = createTRPCRouter({ .input(apiChangePostgresStatus) .mutation(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to change this Postgres status", @@ -226,7 +226,7 @@ export const postgresRouter = createTRPCRouter({ } const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this Postgres", @@ -249,7 +249,7 @@ export const postgresRouter = createTRPCRouter({ .input(apiSaveEnvironmentVariablesPostgres) .mutation(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this environment", @@ -272,7 +272,7 @@ export const postgresRouter = createTRPCRouter({ .input(apiResetPostgres) .mutation(async ({ input, ctx }) => { const postgres = await findPostgresById(input.postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to reload this Postgres", @@ -302,7 +302,7 @@ export const postgresRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { postgresId, ...rest } = input; const postgres = await findPostgresById(postgresId); - if (postgres.project.adminId !== ctx.user.adminId) { + if (postgres.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this Postgres", diff --git a/apps/dokploy/server/api/routers/preview-deployment.ts b/apps/dokploy/server/api/routers/preview-deployment.ts index 74b8461a..482bb350 100644 --- a/apps/dokploy/server/api/routers/preview-deployment.ts +++ b/apps/dokploy/server/api/routers/preview-deployment.ts @@ -14,7 +14,7 @@ export const previewDeploymentRouter = createTRPCRouter({ .input(apiFindAllByApplication) .query(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -28,7 +28,7 @@ export const previewDeploymentRouter = createTRPCRouter({ const previewDeployment = await findPreviewDeploymentById( input.previewDeploymentId, ); - if (previewDeployment.application.project.adminId !== ctx.user.adminId) { + if (previewDeployment.application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this preview deployment", @@ -43,7 +43,7 @@ export const previewDeploymentRouter = createTRPCRouter({ const previewDeployment = await findPreviewDeploymentById( input.previewDeploymentId, ); - if (previewDeployment.application.project.adminId !== ctx.user.adminId) { + if (previewDeployment.application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this preview deployment", diff --git a/apps/dokploy/server/api/routers/project.ts b/apps/dokploy/server/api/routers/project.ts index c2852ff6..1d2d2036 100644 --- a/apps/dokploy/server/api/routers/project.ts +++ b/apps/dokploy/server/api/routers/project.ts @@ -75,7 +75,7 @@ export const projectRouter = createTRPCRouter({ const project = await db.query.projects.findFirst({ where: and( eq(projects.projectId, input.projectId), - eq(projects.adminId, ctx.user.adminId), + eq(projects.userId, ctx.user.ownerId), ), with: { compose: { @@ -115,7 +115,7 @@ export const projectRouter = createTRPCRouter({ } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -207,7 +207,7 @@ export const projectRouter = createTRPCRouter({ await checkProjectAccess(ctx.user.authId, "delete"); } const currentProject = await findProjectById(input.projectId); - if (currentProject.adminId !== ctx.user.adminId) { + if (currentProject.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this project", @@ -225,7 +225,7 @@ export const projectRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const currentProject = await findProjectById(input.projectId); - if (currentProject.adminId !== ctx.user.adminId) { + if (currentProject.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this project", diff --git a/apps/dokploy/server/api/routers/redirects.ts b/apps/dokploy/server/api/routers/redirects.ts index bcd7962a..1a8ba4cf 100644 --- a/apps/dokploy/server/api/routers/redirects.ts +++ b/apps/dokploy/server/api/routers/redirects.ts @@ -18,7 +18,7 @@ export const redirectsRouter = createTRPCRouter({ .input(apiCreateRedirect) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -31,7 +31,7 @@ export const redirectsRouter = createTRPCRouter({ .query(async ({ input, ctx }) => { const redirect = await findRedirectById(input.redirectId); const application = await findApplicationById(redirect.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -44,7 +44,7 @@ export const redirectsRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const redirect = await findRedirectById(input.redirectId); const application = await findApplicationById(redirect.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -57,7 +57,7 @@ export const redirectsRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const redirect = await findRedirectById(input.redirectId); const application = await findApplicationById(redirect.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", diff --git a/apps/dokploy/server/api/routers/redis.ts b/apps/dokploy/server/api/routers/redis.ts index 967fb51a..af1d4234 100644 --- a/apps/dokploy/server/api/routers/redis.ts +++ b/apps/dokploy/server/api/routers/redis.ts @@ -48,7 +48,7 @@ export const redisRouter = createTRPCRouter({ } const project = await findProjectById(input.projectId); - if (project.adminId !== ctx.user.adminId) { + if (project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this project", @@ -80,7 +80,7 @@ export const redisRouter = createTRPCRouter({ } const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this Redis", @@ -93,7 +93,7 @@ export const redisRouter = createTRPCRouter({ .input(apiFindOneRedis) .mutation(async ({ input, ctx }) => { const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to start this Redis", @@ -115,7 +115,7 @@ export const redisRouter = createTRPCRouter({ .input(apiResetRedis) .mutation(async ({ input, ctx }) => { const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to reload this Redis", @@ -145,7 +145,7 @@ export const redisRouter = createTRPCRouter({ .input(apiFindOneRedis) .mutation(async ({ input, ctx }) => { const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to stop this Redis", @@ -166,7 +166,7 @@ export const redisRouter = createTRPCRouter({ .input(apiSaveExternalPortRedis) .mutation(async ({ input, ctx }) => { const mongo = await findRedisById(input.redisId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this external port", @@ -182,7 +182,7 @@ export const redisRouter = createTRPCRouter({ .input(apiDeployRedis) .mutation(async ({ input, ctx }) => { const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this Redis", @@ -202,7 +202,7 @@ export const redisRouter = createTRPCRouter({ .input(apiDeployRedis) .subscription(async ({ input, ctx }) => { const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to deploy this Redis", @@ -218,7 +218,7 @@ export const redisRouter = createTRPCRouter({ .input(apiChangeRedisStatus) .mutation(async ({ input, ctx }) => { const mongo = await findRedisById(input.redisId); - if (mongo.project.adminId !== ctx.user.adminId) { + if (mongo.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to change this Redis status", @@ -238,7 +238,7 @@ export const redisRouter = createTRPCRouter({ const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this Redis", @@ -261,7 +261,7 @@ export const redisRouter = createTRPCRouter({ .input(apiSaveEnvironmentVariablesRedis) .mutation(async ({ input, ctx }) => { const redis = await findRedisById(input.redisId); - if (redis.project.adminId !== ctx.user.adminId) { + if (redis.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to save this environment", diff --git a/apps/dokploy/server/api/routers/registry.ts b/apps/dokploy/server/api/routers/registry.ts index f66ed4ae..76df5cd6 100644 --- a/apps/dokploy/server/api/routers/registry.ts +++ b/apps/dokploy/server/api/routers/registry.ts @@ -10,7 +10,7 @@ import { createRegistry, execAsync, execAsyncRemote, - findAllRegistryByAdminId, + findAllRegistryByUserId, findRegistryById, removeRegistry, updateRegistry, @@ -22,13 +22,13 @@ export const registryRouter = createTRPCRouter({ create: adminProcedure .input(apiCreateRegistry) .mutation(async ({ ctx, input }) => { - return await createRegistry(input, ctx.user.adminId); + return await createRegistry(input, ctx.user.ownerId); }), remove: adminProcedure .input(apiRemoveRegistry) .mutation(async ({ ctx, input }) => { const registry = await findRegistryById(input.registryId); - if (registry.adminId !== ctx.user.adminId) { + if (registry.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to delete this registry", @@ -41,7 +41,7 @@ export const registryRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { registryId, ...rest } = input; const registry = await findRegistryById(registryId); - if (registry.adminId !== ctx.user.adminId) { + if (registry.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to update this registry", @@ -61,13 +61,13 @@ export const registryRouter = createTRPCRouter({ return true; }), all: protectedProcedure.query(async ({ ctx }) => { - return await findAllRegistryByAdminId(ctx.user.adminId); + return await findAllRegistryByUserId(ctx.user.ownerId); }), one: adminProcedure .input(apiFindOneRegistry) .query(async ({ input, ctx }) => { const registry = await findRegistryById(input.registryId); - if (registry.adminId !== ctx.user.adminId) { + if (registry.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not allowed to access this registry", diff --git a/apps/dokploy/server/api/routers/security.ts b/apps/dokploy/server/api/routers/security.ts index 5318a293..ca4892d2 100644 --- a/apps/dokploy/server/api/routers/security.ts +++ b/apps/dokploy/server/api/routers/security.ts @@ -18,7 +18,7 @@ export const securityRouter = createTRPCRouter({ .input(apiCreateSecurity) .mutation(async ({ input, ctx }) => { const application = await findApplicationById(input.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -31,7 +31,7 @@ export const securityRouter = createTRPCRouter({ .query(async ({ input, ctx }) => { const security = await findSecurityById(input.securityId); const application = await findApplicationById(security.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -44,7 +44,7 @@ export const securityRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const security = await findSecurityById(input.securityId); const application = await findApplicationById(security.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", @@ -57,7 +57,7 @@ export const securityRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { const security = await findSecurityById(input.securityId); const application = await findApplicationById(security.applicationId); - if (application.project.adminId !== ctx.user.adminId) { + if (application.project.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this application", diff --git a/apps/dokploy/server/api/routers/server.ts b/apps/dokploy/server/api/routers/server.ts index 0094b675..8e63d7e6 100644 --- a/apps/dokploy/server/api/routers/server.ts +++ b/apps/dokploy/server/api/routers/server.ts @@ -21,9 +21,9 @@ import { createServer, defaultCommand, deleteServer, - findAdminById, findServerById, - findServersByAdminId, + findServersByUserId, + findUserById, getPublicIpWithFallback, haveActiveServices, removeDeploymentsByServerId, @@ -42,15 +42,15 @@ export const serverRouter = createTRPCRouter({ .input(apiCreateServer) .mutation(async ({ ctx, input }) => { try { - const admin = await findAdminById(ctx.user.adminId); - const servers = await findServersByAdminId(admin.adminId); - if (IS_CLOUD && servers.length >= admin.serversQuantity) { + const user = await findUserById(ctx.user.ownerId); + const servers = await findServersByUserId(user.id); + if (IS_CLOUD && servers.length >= user.serversQuantity) { throw new TRPCError({ code: "BAD_REQUEST", message: "You cannot create more servers", }); } - const project = await createServer(input, ctx.user.adminId); + const project = await createServer(input, ctx.user.ownerId); return project; } catch (error) { throw new TRPCError({ @@ -65,7 +65,7 @@ export const serverRouter = createTRPCRouter({ .input(apiFindOneServer) .query(async ({ input, ctx }) => { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to access this server", @@ -93,7 +93,7 @@ export const serverRouter = createTRPCRouter({ .leftJoin(mongo, eq(mongo.serverId, server.serverId)) .leftJoin(mysql, eq(mysql.serverId, server.serverId)) .leftJoin(postgres, eq(postgres.serverId, server.serverId)) - .where(eq(server.adminId, ctx.user.adminId)) + .where(eq(server.userId, ctx.user.ownerId)) .orderBy(desc(server.createdAt)) .groupBy(server.serverId); @@ -105,10 +105,10 @@ export const serverRouter = createTRPCRouter({ where: IS_CLOUD ? and( isNotNull(server.sshKeyId), - eq(server.adminId, ctx.user.adminId), + eq(server.userId, ctx.user.ownerId), eq(server.serverStatus, "active"), ) - : and(isNotNull(server.sshKeyId), eq(server.adminId, ctx.user.adminId)), + : and(isNotNull(server.sshKeyId), eq(server.userId, ctx.user.ownerId)), }); return result; }), @@ -117,7 +117,7 @@ export const serverRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to setup this server", @@ -142,7 +142,7 @@ export const serverRouter = createTRPCRouter({ .subscription(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to setup this server", @@ -162,7 +162,7 @@ export const serverRouter = createTRPCRouter({ .query(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to validate this server", @@ -204,7 +204,7 @@ export const serverRouter = createTRPCRouter({ .query(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to validate this server", @@ -254,7 +254,7 @@ export const serverRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to setup this server", @@ -296,7 +296,7 @@ export const serverRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to delete this server", @@ -315,12 +315,9 @@ export const serverRouter = createTRPCRouter({ await deleteServer(input.serverId); if (IS_CLOUD) { - const admin = await findAdminById(ctx.user.adminId); + const admin = await findUserById(ctx.user.ownerId); - await updateServersBasedOnQuantity( - admin.adminId, - admin.serversQuantity, - ); + await updateServersBasedOnQuantity(admin.id, admin.serversQuantity); } return currentServer; @@ -333,7 +330,7 @@ export const serverRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const server = await findServerById(input.serverId); - if (server.adminId !== ctx.user.adminId) { + if (server.userId !== ctx.user.ownerId) { throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized to update this server", diff --git a/apps/dokploy/server/api/routers/settings.ts b/apps/dokploy/server/api/routers/settings.ts index 116de6ad..6dda9383 100644 --- a/apps/dokploy/server/api/routers/settings.ts +++ b/apps/dokploy/server/api/routers/settings.ts @@ -47,7 +47,6 @@ import { startServiceRemote, stopService, stopServiceRemote, - updateAdmin, updateLetsEncryptEmail, updateServerById, updateServerTraefik, diff --git a/apps/dokploy/server/api/routers/ssh-key.ts b/apps/dokploy/server/api/routers/ssh-key.ts index fe2f24f9..4beb0bae 100644 --- a/apps/dokploy/server/api/routers/ssh-key.ts +++ b/apps/dokploy/server/api/routers/ssh-key.ts @@ -24,9 +24,10 @@ export const sshRouter = createTRPCRouter({ .input(apiCreateSshKey) .mutation(async ({ input, ctx }) => { try { + console.log(ctx.user.ownerId); await createSshKey({ ...input, - adminId: ctx.user.adminId, + userId: ctx.user.ownerId, }); } catch (error) { throw new TRPCError({ @@ -41,7 +42,7 @@ export const sshRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const sshKey = await findSSHKeyById(input.sshKeyId); - if (IS_CLOUD && sshKey.adminId !== ctx.user.adminId) { + if (IS_CLOUD && sshKey.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -59,7 +60,7 @@ export const sshRouter = createTRPCRouter({ .query(async ({ input, ctx }) => { const sshKey = await findSSHKeyById(input.sshKeyId); - if (IS_CLOUD && sshKey.adminId !== ctx.user.adminId) { + if (IS_CLOUD && sshKey.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", @@ -70,7 +71,7 @@ export const sshRouter = createTRPCRouter({ }), all: protectedProcedure.query(async ({ ctx }) => { return await db.query.sshKeys.findMany({ - ...(IS_CLOUD && { where: eq(sshKeys.adminId, ctx.user.adminId) }), + ...(IS_CLOUD && { where: eq(sshKeys.userId, ctx.user.ownerId) }), orderBy: desc(sshKeys.createdAt), }); // TODO: Remove this line when the cloud version is ready @@ -85,7 +86,7 @@ export const sshRouter = createTRPCRouter({ .mutation(async ({ input, ctx }) => { try { const sshKey = await findSSHKeyById(input.sshKeyId); - if (IS_CLOUD && sshKey.adminId !== ctx.user.adminId) { + if (IS_CLOUD && sshKey.userId !== ctx.user.ownerId) { // TODO: Remove isCloud in the next versions of dokploy throw new TRPCError({ code: "UNAUTHORIZED", diff --git a/apps/dokploy/server/api/routers/stripe.ts b/apps/dokploy/server/api/routers/stripe.ts index 7a8a537c..91fa44fd 100644 --- a/apps/dokploy/server/api/routers/stripe.ts +++ b/apps/dokploy/server/api/routers/stripe.ts @@ -1,9 +1,9 @@ import { WEBSITE_URL, getStripeItems } from "@/server/utils/stripe"; import { IS_CLOUD, - findAdminById, - findServersByAdminId, - updateAdmin, + findServersByUserId, + findUserById, + updateUser, } from "@dokploy/server"; import { TRPCError } from "@trpc/server"; import Stripe from "stripe"; @@ -12,8 +12,8 @@ import { adminProcedure, createTRPCRouter } from "../trpc"; export const stripeRouter = createTRPCRouter({ getProducts: adminProcedure.query(async ({ ctx }) => { - const admin = await findAdminById(ctx.user.adminId); - const stripeCustomerId = admin.stripeCustomerId; + const user = await findUserById(ctx.user.ownerId); + const stripeCustomerId = user.stripeCustomerId; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { apiVersion: "2024-09-30.acacia", @@ -56,15 +56,15 @@ export const stripeRouter = createTRPCRouter({ }); const items = getStripeItems(input.serverQuantity, input.isAnnual); - const admin = await findAdminById(ctx.user.adminId); + const user = await findUserById(ctx.user.ownerId); - let stripeCustomerId = admin.stripeCustomerId; + let stripeCustomerId = user.stripeCustomerId; if (stripeCustomerId) { const customer = await stripe.customers.retrieve(stripeCustomerId); if (customer.deleted) { - await updateAdmin(admin.authId, { + await updateUser(user.id, { stripeCustomerId: null, }); stripeCustomerId = null; @@ -78,7 +78,7 @@ export const stripeRouter = createTRPCRouter({ customer: stripeCustomerId, }), metadata: { - adminId: admin.adminId, + ownerId: user.id, }, allow_promotion_codes: true, success_url: `${WEBSITE_URL}/dashboard/settings/servers?success=true`, @@ -89,15 +89,15 @@ export const stripeRouter = createTRPCRouter({ }), createCustomerPortalSession: adminProcedure.mutation( async ({ ctx, input }) => { - const admin = await findAdminById(ctx.user.adminId); + const user = await findUserById(ctx.user.ownerId); - if (!admin.stripeCustomerId) { + if (!user.stripeCustomerId) { throw new TRPCError({ code: "BAD_REQUEST", message: "Stripe Customer ID not found", }); } - const stripeCustomerId = admin.stripeCustomerId; + const stripeCustomerId = user.stripeCustomerId; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { apiVersion: "2024-09-30.acacia", @@ -119,13 +119,14 @@ export const stripeRouter = createTRPCRouter({ ), canCreateMoreServers: adminProcedure.query(async ({ ctx }) => { - const admin = await findAdminById(ctx.user.adminId); - const servers = await findServersByAdminId(admin.adminId); + const user = await findUserById(ctx.user.ownerId); + console.log(user); + // const servers = await findServersByUserId(user.id); if (!IS_CLOUD) { return true; } - return servers.length < admin.serversQuantity; + return servers.length < user.serversQuantity; }), }); diff --git a/apps/dokploy/server/api/trpc.ts b/apps/dokploy/server/api/trpc.ts index 9579407b..9f373ad3 100644 --- a/apps/dokploy/server/api/trpc.ts +++ b/apps/dokploy/server/api/trpc.ts @@ -31,7 +31,7 @@ import { ZodError } from "zod"; */ interface CreateContextOptions { - user: (User & { authId: string; rol: "admin" | "user" }) | null; + user: (User & { rol: "admin" | "user"; ownerId: string }) | null; session: Session | null; req: CreateNextContextOptions["req"]; res: CreateNextContextOptions["res"]; @@ -81,10 +81,10 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => { session: session, ...((user && { user: { - authId: "Null", email: user.email, rol: user.role, id: user.id, + ownerId: user.ownerId, }, }) || { user: null, diff --git a/packages/server/src/db/schema/account.ts b/packages/server/src/db/schema/account.ts index 3e81fbf6..e7a263fa 100644 --- a/packages/server/src/db/schema/account.ts +++ b/packages/server/src/db/schema/account.ts @@ -1,6 +1,7 @@ import { boolean, pgTable, text, timestamp } from "drizzle-orm/pg-core"; import { nanoid } from "nanoid"; import { users_temp } from "./user"; +import { relations } from "drizzle-orm"; export const account = pgTable("account", { id: text("id") @@ -29,6 +30,13 @@ export const account = pgTable("account", { confirmationExpiresAt: text("confirmationExpiresAt"), }); +export const accountRelations = relations(account, ({ one }) => ({ + user: one(users_temp, { + fields: [account.userId], + references: [users_temp.id], + }), +})); + export const verification = pgTable("verification", { id: text("id").primaryKey(), identifier: text("identifier").notNull(), @@ -52,6 +60,13 @@ export const organization = pgTable("organization", { .references(() => users_temp.id), }); +export const organizationRelations = relations(organization, ({ one }) => ({ + owner: one(users_temp, { + fields: [organization.ownerId], + references: [users_temp.id], + }), +})); + export const member = pgTable("member", { id: text("id") .primaryKey() @@ -66,6 +81,17 @@ export const member = pgTable("member", { createdAt: timestamp("created_at").notNull(), }); +export const memberRelations = relations(member, ({ one }) => ({ + organization: one(organization, { + fields: [member.organizationId], + references: [organization.id], + }), + user: one(users_temp, { + fields: [member.userId], + references: [users_temp.id], + }), +})); + export const invitation = pgTable("invitation", { id: text("id").primaryKey(), organizationId: text("organization_id") @@ -79,3 +105,10 @@ export const invitation = pgTable("invitation", { .notNull() .references(() => users_temp.id), }); + +export const invitationRelations = relations(invitation, ({ one }) => ({ + organization: one(organization, { + fields: [invitation.organizationId], + references: [organization.id], + }), +})); diff --git a/packages/server/src/db/schema/bitbucket.ts b/packages/server/src/db/schema/bitbucket.ts index 393cb1e7..b8ecb668 100644 --- a/packages/server/src/db/schema/bitbucket.ts +++ b/packages/server/src/db/schema/bitbucket.ts @@ -61,5 +61,5 @@ export const apiUpdateBitbucket = createSchema.extend({ name: z.string().min(1), bitbucketUsername: z.string().optional(), bitbucketWorkspaceName: z.string().optional(), - adminId: z.string().optional(), + userId: z.string().optional(), }); diff --git a/packages/server/src/db/schema/notification.ts b/packages/server/src/db/schema/notification.ts index 462b6710..78596b0d 100644 --- a/packages/server/src/db/schema/notification.ts +++ b/packages/server/src/db/schema/notification.ts @@ -3,7 +3,6 @@ import { boolean, integer, pgEnum, pgTable, text } from "drizzle-orm/pg-core"; import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; -import { admins } from "./admin"; import { users_temp } from "./user"; // import { user } from "./user"; @@ -153,7 +152,7 @@ export const apiCreateSlack = notificationsSchema export const apiUpdateSlack = apiCreateSlack.partial().extend({ notificationId: z.string().min(1), slackId: z.string(), - adminId: z.string().optional(), + userId: z.string().optional(), }); export const apiTestSlackConnection = apiCreateSlack.pick({ @@ -180,7 +179,7 @@ export const apiCreateTelegram = notificationsSchema export const apiUpdateTelegram = apiCreateTelegram.partial().extend({ notificationId: z.string().min(1), telegramId: z.string().min(1), - adminId: z.string().optional(), + userId: z.string().optional(), }); export const apiTestTelegramConnection = apiCreateTelegram.pick({ @@ -207,7 +206,7 @@ export const apiCreateDiscord = notificationsSchema export const apiUpdateDiscord = apiCreateDiscord.partial().extend({ notificationId: z.string().min(1), discordId: z.string().min(1), - adminId: z.string().optional(), + userId: z.string().optional(), }); export const apiTestDiscordConnection = apiCreateDiscord @@ -241,7 +240,7 @@ export const apiCreateEmail = notificationsSchema export const apiUpdateEmail = apiCreateEmail.partial().extend({ notificationId: z.string().min(1), emailId: z.string().min(1), - adminId: z.string().optional(), + userId: z.string().optional(), }); export const apiTestEmailConnection = apiCreateEmail.pick({ @@ -273,7 +272,7 @@ export const apiCreateGotify = notificationsSchema export const apiUpdateGotify = apiCreateGotify.partial().extend({ notificationId: z.string().min(1), gotifyId: z.string().min(1), - adminId: z.string().optional(), + userId: z.string().optional(), }); export const apiTestGotifyConnection = apiCreateGotify diff --git a/packages/server/src/db/schema/user.ts b/packages/server/src/db/schema/user.ts index d754687c..0a353513 100644 --- a/packages/server/src/db/schema/user.ts +++ b/packages/server/src/db/schema/user.ts @@ -196,8 +196,8 @@ export const usersRelations = relations(users, ({ one }) => ({ // }), })); -const createSchema = createInsertSchema(users, { - userId: z.string().min(1), +const createSchema = createInsertSchema(users_temp, { + id: z.string().min(1), // authId: z.string().min(1), token: z.string().min(1), isRegistered: z.boolean().optional(), @@ -218,7 +218,7 @@ export const apiCreateUserInvitation = createSchema.pick({}).extend({ export const apiRemoveUser = createSchema .pick({ - // authId: true, + id: true, }) .required(); @@ -230,7 +230,7 @@ export const apiFindOneToken = createSchema export const apiAssignPermissions = createSchema .pick({ - userId: true, + id: true, canCreateProjects: true, canCreateServices: true, canDeleteProjects: true, @@ -247,7 +247,7 @@ export const apiAssignPermissions = createSchema export const apiFindOneUser = createSchema .pick({ - userId: true, + id: true, }) .required(); diff --git a/packages/server/src/lib/auth.ts b/packages/server/src/lib/auth.ts index 878313ae..48e11f21 100644 --- a/packages/server/src/lib/auth.ts +++ b/packages/server/src/lib/auth.ts @@ -44,6 +44,9 @@ export const auth = betterAuth({ role: { type: "string", }, + ownerId: { + type: "string", + }, }, }, plugins: [organization()], @@ -56,7 +59,20 @@ export const validateRequest = async (request: IncomingMessage) => { }), }); - console.log(session); + if (session?.user.role === "user") { + const owner = await db.query.member.findFirst({ + where: eq(schema.member.userId, session.user.id), + with: { + organization: true, + }, + }); + + if (owner) { + session.user.ownerId = owner.organization.ownerId; + } + } else { + session.user.ownerId = session?.user.id; + } if (!session?.session || !session.user) { return { diff --git a/packages/server/src/services/admin.ts b/packages/server/src/services/admin.ts index 5155231b..c4b7650b 100644 --- a/packages/server/src/services/admin.ts +++ b/packages/server/src/services/admin.ts @@ -11,7 +11,7 @@ import * as bcrypt from "bcrypt"; import { eq } from "drizzle-orm"; import { IS_CLOUD } from "../constants"; -export type Admin = typeof admins.$inferSelect; +export type Admin = typeof users_temp.$inferSelect; export const createInvitation = async ( input: typeof apiCreateUserInvitation._type, adminId: string, @@ -48,33 +48,30 @@ export const createInvitation = async ( }); }; -export const findAdminById = async (adminId: string) => { - const admin = await db.query.admins.findFirst({ - where: eq(admins.adminId, adminId), +export const findUserById = async (userId: string) => { + const user = await db.query.users_temp.findFirst({ + where: eq(users_temp.id, userId), }); - if (!admin) { + if (!user) { throw new TRPCError({ code: "NOT_FOUND", - message: "Admin not found", + message: "User not found", }); } - return admin; + return user; }; -export const updateAdmin = async ( - authId: string, - adminData: Partial, -) => { - const admin = await db - .update(admins) +export const updateUser = async (userId: string, userData: Partial) => { + const user = await db + .update(users_temp) .set({ - ...adminData, + ...userData, }) - .where(eq(admins.authId, authId)) + .where(eq(users_temp.id, userId)) .returning() .then((res) => res[0]); - return admin; + return user; }; export const updateAdminById = async ( @@ -93,6 +90,13 @@ export const updateAdminById = async ( return admin; }; +export const findAdminById = async (userId: string) => { + const admin = await db.query.admins.findFirst({ + where: eq(admins.userId, userId), + }); + return admin; +}; + export const isAdminPresent = async () => { const admin = await db.query.user.findFirst({ where: eq(user.role, "admin"), @@ -154,10 +158,10 @@ export const getUserByToken = async (token: string) => { }; }; -export const removeUserByAuthId = async (authId: string) => { +export const removeUserById = async (userId: string) => { await db - .delete(auth) - .where(eq(auth.id, authId)) + .delete(users_temp) + .where(eq(users_temp.id, userId)) .returning() .then((res) => res[0]); }; @@ -170,7 +174,7 @@ export const removeAdminByAuthId = async (authId: string) => { const users = admin.users; for (const user of users) { - await removeUserByAuthId(user.authId); + await removeUserById(user.id); } // Then delete the auth record which will cascade delete the admin return await db diff --git a/packages/server/src/services/bitbucket.ts b/packages/server/src/services/bitbucket.ts index 21807156..4ce4a7b0 100644 --- a/packages/server/src/services/bitbucket.ts +++ b/packages/server/src/services/bitbucket.ts @@ -12,14 +12,14 @@ export type Bitbucket = typeof bitbucket.$inferSelect; export const createBitbucket = async ( input: typeof apiCreateBitbucket._type, - adminId: string, + userId: string, ) => { return await db.transaction(async (tx) => { const newGitProvider = await tx .insert(gitProvider) .values({ providerType: "bitbucket", - adminId: adminId, + userId: userId, name: input.name, }) .returning() @@ -74,12 +74,12 @@ export const updateBitbucket = async ( .where(eq(bitbucket.bitbucketId, bitbucketId)) .returning(); - if (input.name || input.adminId) { + if (input.name || input.userId) { await tx .update(gitProvider) .set({ name: input.name, - adminId: input.adminId, + userId: input.userId, }) .where(eq(gitProvider.gitProviderId, input.gitProviderId)) .returning(); diff --git a/packages/server/src/services/github.ts b/packages/server/src/services/github.ts index a5d9d863..b23edf20 100644 --- a/packages/server/src/services/github.ts +++ b/packages/server/src/services/github.ts @@ -12,14 +12,14 @@ import { updatePreviewDeployment } from "./preview-deployment"; export type Github = typeof github.$inferSelect; export const createGithub = async ( input: typeof apiCreateGithub._type, - adminId: string, + userId: string, ) => { return await db.transaction(async (tx) => { const newGitProvider = await tx .insert(gitProvider) .values({ providerType: "github", - adminId: adminId, + userId: userId, name: input.name, }) .returning() diff --git a/packages/server/src/services/notification.ts b/packages/server/src/services/notification.ts index 8b17da36..03f6bd09 100644 --- a/packages/server/src/services/notification.ts +++ b/packages/server/src/services/notification.ts @@ -24,7 +24,7 @@ export type Notification = typeof notifications.$inferSelect; export const createSlackNotification = async ( input: typeof apiCreateSlack._type, - adminId: string, + userId: string, ) => { await db.transaction(async (tx) => { const newSlack = await tx @@ -54,7 +54,7 @@ export const createSlackNotification = async ( dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, notificationType: "slack", - adminId: adminId, + userId: userId, serverThreshold: input.serverThreshold, }) .returning() @@ -84,7 +84,7 @@ export const updateSlackNotification = async ( databaseBackup: input.databaseBackup, dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, - adminId: input.adminId, + userId: input.userId, serverThreshold: input.serverThreshold, }) .where(eq(notifications.notificationId, input.notificationId)) @@ -114,7 +114,7 @@ export const updateSlackNotification = async ( export const createTelegramNotification = async ( input: typeof apiCreateTelegram._type, - adminId: string, + userId: string, ) => { await db.transaction(async (tx) => { const newTelegram = await tx @@ -144,7 +144,7 @@ export const createTelegramNotification = async ( dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, notificationType: "telegram", - adminId: adminId, + userId: userId, serverThreshold: input.serverThreshold, }) .returning() @@ -174,7 +174,7 @@ export const updateTelegramNotification = async ( databaseBackup: input.databaseBackup, dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, - adminId: input.adminId, + userId: input.userId, serverThreshold: input.serverThreshold, }) .where(eq(notifications.notificationId, input.notificationId)) @@ -204,7 +204,7 @@ export const updateTelegramNotification = async ( export const createDiscordNotification = async ( input: typeof apiCreateDiscord._type, - adminId: string, + userId: string, ) => { await db.transaction(async (tx) => { const newDiscord = await tx @@ -234,7 +234,7 @@ export const createDiscordNotification = async ( dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, notificationType: "discord", - adminId: adminId, + userId: userId, serverThreshold: input.serverThreshold, }) .returning() @@ -264,7 +264,7 @@ export const updateDiscordNotification = async ( databaseBackup: input.databaseBackup, dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, - adminId: input.adminId, + userId: input.userId, serverThreshold: input.serverThreshold, }) .where(eq(notifications.notificationId, input.notificationId)) @@ -294,7 +294,7 @@ export const updateDiscordNotification = async ( export const createEmailNotification = async ( input: typeof apiCreateEmail._type, - adminId: string, + userId: string, ) => { await db.transaction(async (tx) => { const newEmail = await tx @@ -328,7 +328,7 @@ export const createEmailNotification = async ( dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, notificationType: "email", - adminId: adminId, + userId: userId, serverThreshold: input.serverThreshold, }) .returning() @@ -358,7 +358,7 @@ export const updateEmailNotification = async ( databaseBackup: input.databaseBackup, dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, - adminId: input.adminId, + userId: input.userId, serverThreshold: input.serverThreshold, }) .where(eq(notifications.notificationId, input.notificationId)) @@ -392,7 +392,7 @@ export const updateEmailNotification = async ( export const createGotifyNotification = async ( input: typeof apiCreateGotify._type, - adminId: string, + userId: string, ) => { await db.transaction(async (tx) => { const newGotify = await tx @@ -424,7 +424,7 @@ export const createGotifyNotification = async ( dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, notificationType: "gotify", - adminId: adminId, + userId: userId, }) .returning() .then((value) => value[0]); @@ -453,7 +453,7 @@ export const updateGotifyNotification = async ( databaseBackup: input.databaseBackup, dokployRestart: input.dokployRestart, dockerCleanup: input.dockerCleanup, - adminId: input.adminId, + userId: input.userId, }) .where(eq(notifications.notificationId, input.notificationId)) .returning() diff --git a/packages/server/src/services/registry.ts b/packages/server/src/services/registry.ts index 2bcf3a4a..b92eabdb 100644 --- a/packages/server/src/services/registry.ts +++ b/packages/server/src/services/registry.ts @@ -12,14 +12,14 @@ export type Registry = typeof registry.$inferSelect; export const createRegistry = async ( input: typeof apiCreateRegistry._type, - adminId: string, + userId: string, ) => { return await db.transaction(async (tx) => { const newRegistry = await tx .insert(registry) .values({ ...input, - adminId: adminId, + userId: userId, }) .returning() .then((value) => value[0]); @@ -135,9 +135,9 @@ export const findRegistryById = async (registryId: string) => { return registryResponse; }; -export const findAllRegistryByAdminId = async (adminId: string) => { +export const findAllRegistryByUserId = async (userId: string) => { const registryResponse = await db.query.registry.findMany({ - where: eq(registry.adminId, adminId), + where: eq(registry.userId, userId), }); return registryResponse; }; diff --git a/packages/server/src/services/server.ts b/packages/server/src/services/server.ts index 081b19fa..6aaa8bec 100644 --- a/packages/server/src/services/server.ts +++ b/packages/server/src/services/server.ts @@ -7,13 +7,13 @@ export type Server = typeof server.$inferSelect; export const createServer = async ( input: typeof apiCreateServer._type, - adminId: string, + userId: string, ) => { const newServer = await db .insert(server) .values({ ...input, - adminId: adminId, + userId: userId, }) .returning() .then((value) => value[0]); @@ -45,9 +45,9 @@ export const findServerById = async (serverId: string) => { return currentServer; }; -export const findServersByAdminId = async (adminId: string) => { +export const findServersByUserId = async (userId: string) => { const servers = await db.query.server.findMany({ - where: eq(server.adminId, adminId), + where: eq(server.userId, userId), orderBy: desc(server.createdAt), });