mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: update
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
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")
|
||||
|
||||
@@ -13,6 +13,7 @@ import { z } from "zod";
|
||||
import { admins } from "./admin";
|
||||
import { auth } from "./auth";
|
||||
import { certificateType } from "./shared";
|
||||
import { account } from "./account";
|
||||
/**
|
||||
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
|
||||
* database instance for multiple projects.
|
||||
@@ -185,10 +186,14 @@ export const users_temp = pgTable("user_temp", {
|
||||
serversQuantity: integer("serversQuantity").notNull().default(0),
|
||||
});
|
||||
|
||||
export const usersRelations = relations(users, ({ one }) => ({
|
||||
auth: one(auth, {
|
||||
fields: [users.authId],
|
||||
references: [auth.id],
|
||||
export const usersRelations = relations(users_temp, ({ one }) => ({
|
||||
// auth: one(auth, {
|
||||
// fields: [users.authId],
|
||||
// references: [auth.id],
|
||||
// }),
|
||||
account: one(account, {
|
||||
fields: [users_temp.id],
|
||||
references: [account.userId],
|
||||
}),
|
||||
// admin: one(admins, {
|
||||
// fields: [users.adminId],
|
||||
|
||||
@@ -51,6 +51,9 @@ export const createInvitation = async (
|
||||
export const findUserById = async (userId: string) => {
|
||||
const user = await db.query.users_temp.findFirst({
|
||||
where: eq(users_temp.id, userId),
|
||||
// with: {
|
||||
// account: true,
|
||||
// },
|
||||
});
|
||||
if (!user) {
|
||||
throw new TRPCError({
|
||||
|
||||
@@ -15,6 +15,7 @@ import encode from "hi-base32";
|
||||
import { TOTP } from "otpauth";
|
||||
import QRCode from "qrcode";
|
||||
import { IS_CLOUD } from "../constants";
|
||||
import { findUserById } from "./admin";
|
||||
|
||||
export type Auth = typeof auth.$inferSelect;
|
||||
|
||||
@@ -131,14 +132,14 @@ export const updateAuthById = async (
|
||||
return result[0];
|
||||
};
|
||||
|
||||
export const generate2FASecret = async (authId: string) => {
|
||||
const auth = await findAuthById(authId);
|
||||
export const generate2FASecret = async (userId: string) => {
|
||||
const user = await findUserById(userId);
|
||||
|
||||
const base32_secret = generateBase32Secret();
|
||||
|
||||
const totp = new TOTP({
|
||||
issuer: "Dokploy",
|
||||
label: `${auth?.email}`,
|
||||
label: `${user?.email}`,
|
||||
algorithm: "SHA1",
|
||||
digits: 6,
|
||||
secret: base32_secret,
|
||||
|
||||
@@ -14,6 +14,7 @@ export const createServer = async (
|
||||
.values({
|
||||
...input,
|
||||
userId: userId,
|
||||
createdAt: new Date().toISOString(),
|
||||
})
|
||||
.returning()
|
||||
.then((value) => value[0]);
|
||||
|
||||
@@ -2,21 +2,22 @@ import { db } from "@dokploy/server/db";
|
||||
import type { users_temp } from "@dokploy/server/db/schema";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { findUserById } from "./admin";
|
||||
|
||||
export type User = typeof users_temp.$inferSelect;
|
||||
|
||||
export const findUserById = async (userId: string) => {
|
||||
const userR = await db.query.user.findFirst({
|
||||
where: eq(user.userId, userId),
|
||||
});
|
||||
if (!userR) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "User not found",
|
||||
});
|
||||
}
|
||||
return user;
|
||||
};
|
||||
// export const findUserById = async (userId: string) => {
|
||||
// // const userR = await db.query.user.findFirst({
|
||||
// // where: eq(user.userId, userId),
|
||||
// // });
|
||||
// // if (!userR) {
|
||||
// // throw new TRPCError({
|
||||
// // code: "NOT_FOUND",
|
||||
// // message: "User not found",
|
||||
// // });
|
||||
// // }
|
||||
// // return user;
|
||||
// };
|
||||
|
||||
export const findUserByAuthId = async (authId: string) => {
|
||||
const userR = await db.query.user.findFirst({
|
||||
@@ -46,33 +47,32 @@ export const findUsers = async (adminId: string) => {
|
||||
return currentUsers;
|
||||
};
|
||||
|
||||
export const addNewProject = async (authId: string, projectId: string) => {
|
||||
const userR = await findUserByAuthId(authId);
|
||||
export const addNewProject = async (userId: string, projectId: string) => {
|
||||
const userR = await findUserById(userId);
|
||||
|
||||
await db
|
||||
.update(user)
|
||||
.set({
|
||||
accessedProjects: [...userR.accessedProjects, projectId],
|
||||
})
|
||||
.where(eq(user.authId, authId));
|
||||
// await db
|
||||
// .update(user)
|
||||
// .set({
|
||||
// accessedProjects: [...userR.accessedProjects, projectId],
|
||||
// })
|
||||
// .where(eq(user.authId, authId));
|
||||
};
|
||||
|
||||
export const addNewService = async (authId: string, serviceId: string) => {
|
||||
const userR = await findUserByAuthId(authId);
|
||||
await db
|
||||
.update(user)
|
||||
.set({
|
||||
accessedServices: [...userR.accessedServices, serviceId],
|
||||
})
|
||||
.where(eq(user.authId, authId));
|
||||
export const addNewService = async (userId: string, serviceId: string) => {
|
||||
const userR = await findUserById(userId);
|
||||
// await db
|
||||
// .update(user)
|
||||
// .set({
|
||||
// accessedServices: [...userR.accessedServices, serviceId],
|
||||
// })
|
||||
// .where(eq(user.userId, userId));
|
||||
};
|
||||
|
||||
export const canPerformCreationService = async (
|
||||
userId: string,
|
||||
projectId: string,
|
||||
) => {
|
||||
const { accessedProjects, canCreateServices } =
|
||||
await findUserByAuthId(userId);
|
||||
const { accessedProjects, canCreateServices } = await findUserById(userId);
|
||||
const haveAccessToProject = accessedProjects.includes(projectId);
|
||||
|
||||
if (canCreateServices && haveAccessToProject) {
|
||||
@@ -86,7 +86,7 @@ export const canPerformAccessService = async (
|
||||
userId: string,
|
||||
serviceId: string,
|
||||
) => {
|
||||
const { accessedServices } = await findUserByAuthId(userId);
|
||||
const { accessedServices } = await findUserById(userId);
|
||||
const haveAccessToService = accessedServices.includes(serviceId);
|
||||
|
||||
if (haveAccessToService) {
|
||||
@@ -97,11 +97,10 @@ export const canPerformAccessService = async (
|
||||
};
|
||||
|
||||
export const canPeformDeleteService = async (
|
||||
authId: string,
|
||||
userId: string,
|
||||
serviceId: string,
|
||||
) => {
|
||||
const { accessedServices, canDeleteServices } =
|
||||
await findUserByAuthId(authId);
|
||||
const { accessedServices, canDeleteServices } = await findUserById(userId);
|
||||
const haveAccessToService = accessedServices.includes(serviceId);
|
||||
|
||||
if (canDeleteServices && haveAccessToService) {
|
||||
@@ -111,8 +110,8 @@ export const canPeformDeleteService = async (
|
||||
return false;
|
||||
};
|
||||
|
||||
export const canPerformCreationProject = async (authId: string) => {
|
||||
const { canCreateProjects } = await findUserByAuthId(authId);
|
||||
export const canPerformCreationProject = async (userId: string) => {
|
||||
const { canCreateProjects } = await findUserById(userId);
|
||||
|
||||
if (canCreateProjects) {
|
||||
return true;
|
||||
@@ -121,8 +120,8 @@ export const canPerformCreationProject = async (authId: string) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
export const canPerformDeleteProject = async (authId: string) => {
|
||||
const { canDeleteProjects } = await findUserByAuthId(authId);
|
||||
export const canPerformDeleteProject = async (userId: string) => {
|
||||
const { canDeleteProjects } = await findUserById(userId);
|
||||
|
||||
if (canDeleteProjects) {
|
||||
return true;
|
||||
@@ -132,10 +131,10 @@ export const canPerformDeleteProject = async (authId: string) => {
|
||||
};
|
||||
|
||||
export const canPerformAccessProject = async (
|
||||
authId: string,
|
||||
userId: string,
|
||||
projectId: string,
|
||||
) => {
|
||||
const { accessedProjects } = await findUserByAuthId(authId);
|
||||
const { accessedProjects } = await findUserById(userId);
|
||||
|
||||
const haveAccessToProject = accessedProjects.includes(projectId);
|
||||
|
||||
@@ -145,26 +144,26 @@ export const canPerformAccessProject = async (
|
||||
return false;
|
||||
};
|
||||
|
||||
export const canAccessToTraefikFiles = async (authId: string) => {
|
||||
const { canAccessToTraefikFiles } = await findUserByAuthId(authId);
|
||||
export const canAccessToTraefikFiles = async (userId: string) => {
|
||||
const { canAccessToTraefikFiles } = await findUserById(userId);
|
||||
return canAccessToTraefikFiles;
|
||||
};
|
||||
|
||||
export const checkServiceAccess = async (
|
||||
authId: string,
|
||||
userId: string,
|
||||
serviceId: string,
|
||||
action = "access" as "access" | "create" | "delete",
|
||||
) => {
|
||||
let hasPermission = false;
|
||||
switch (action) {
|
||||
case "create":
|
||||
hasPermission = await canPerformCreationService(authId, serviceId);
|
||||
hasPermission = await canPerformCreationService(userId, serviceId);
|
||||
break;
|
||||
case "access":
|
||||
hasPermission = await canPerformAccessService(authId, serviceId);
|
||||
hasPermission = await canPerformAccessService(userId, serviceId);
|
||||
break;
|
||||
case "delete":
|
||||
hasPermission = await canPeformDeleteService(authId, serviceId);
|
||||
hasPermission = await canPeformDeleteService(userId, serviceId);
|
||||
break;
|
||||
default:
|
||||
hasPermission = false;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { findServerById } from "@dokploy/server/services/server";
|
||||
import type { ContainerCreateOptions } from "dockerode";
|
||||
import { IS_CLOUD } from "../constants";
|
||||
import { findAdminById } from "../services/admin";
|
||||
import { findAdminById, findUserById } from "../services/admin";
|
||||
import { getDokployImageTag } from "../services/settings";
|
||||
import { pullImage, pullRemoteImage } from "../utils/docker/utils";
|
||||
import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
|
||||
@@ -80,8 +80,8 @@ export const setupMonitoring = async (serverId: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const setupWebMonitoring = async (adminId: string) => {
|
||||
const admin = await findAdminById(adminId);
|
||||
export const setupWebMonitoring = async (userId: string) => {
|
||||
const user = await findUserById(userId);
|
||||
|
||||
const containerName = "dokploy-monitoring";
|
||||
let imageName = "dokploy/monitoring:latest";
|
||||
@@ -96,7 +96,7 @@ export const setupWebMonitoring = async (adminId: string) => {
|
||||
|
||||
const settings: ContainerCreateOptions = {
|
||||
name: containerName,
|
||||
Env: [`METRICS_CONFIG=${JSON.stringify(admin?.metricsConfig)}`],
|
||||
Env: [`METRICS_CONFIG=${JSON.stringify(user?.metricsConfig)}`],
|
||||
Image: imageName,
|
||||
HostConfig: {
|
||||
// Memory: 100 * 1024 * 1024, // 100MB en bytes
|
||||
@@ -104,9 +104,9 @@ export const setupWebMonitoring = async (adminId: string) => {
|
||||
// CapAdd: ["NET_ADMIN", "SYS_ADMIN"],
|
||||
// Privileged: true,
|
||||
PortBindings: {
|
||||
[`${admin.metricsConfig.server.port}/tcp`]: [
|
||||
[`${user?.metricsConfig?.server?.port}/tcp`]: [
|
||||
{
|
||||
HostPort: admin.metricsConfig.server.port.toString(),
|
||||
HostPort: user?.metricsConfig?.server?.port.toString(),
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -120,7 +120,7 @@ export const setupWebMonitoring = async (adminId: string) => {
|
||||
// NetworkMode: "host",
|
||||
},
|
||||
ExposedPorts: {
|
||||
[`${admin.metricsConfig.server.port}/tcp`]: {},
|
||||
[`${user?.metricsConfig?.server?.port}/tcp`]: {},
|
||||
},
|
||||
};
|
||||
const docker = await getRemoteDocker();
|
||||
|
||||
Reference in New Issue
Block a user