Merge pull request #2499 from Dokploy/324-environmentfoldergroup-features-on-projects

324 environmentfoldergroup features on projects
This commit is contained in:
Mauricio Siu
2025-09-05 00:25:34 -06:00
committed by GitHub
108 changed files with 24712 additions and 2120 deletions
+12 -4
View File
@@ -56,13 +56,21 @@ const baseApp: ApplicationNested = {
previewPort: 3000, previewPort: 3000,
previewLimit: 0, previewLimit: 0,
previewWildcard: "", previewWildcard: "",
project: { environment: {
env: "", env: "",
organizationId: "", environmentId: "",
name: "", name: "",
description: "",
createdAt: "", createdAt: "",
description: "",
projectId: "", projectId: "",
project: {
env: "",
organizationId: "",
name: "",
description: "",
createdAt: "",
projectId: "",
},
}, },
buildArgs: null, buildArgs: null,
buildPath: "/", buildPath: "/",
@@ -92,6 +100,7 @@ const baseApp: ApplicationNested = {
dockerfile: null, dockerfile: null,
dockerImage: null, dockerImage: null,
dropBuildPath: null, dropBuildPath: null,
environmentId: "",
enabled: null, enabled: null,
env: null, env: null,
healthCheckSwarm: null, healthCheckSwarm: null,
@@ -106,7 +115,6 @@ const baseApp: ApplicationNested = {
password: null, password: null,
placementSwarm: null, placementSwarm: null,
ports: [], ports: [],
projectId: "",
publishDirectory: null, publishDirectory: null,
isStaticSpa: null, isStaticSpa: null,
redirects: [], redirects: [],
+335
View File
@@ -0,0 +1,335 @@
import { prepareEnvironmentVariables } from "@dokploy/server/index";
import { describe, expect, it } from "vitest";
const projectEnv = `
ENVIRONMENT=staging
DATABASE_URL=postgres://postgres:postgres@localhost:5432/project_db
PORT=3000
`;
const environmentEnv = `
NODE_ENV=development
API_URL=https://api.dev.example.com
REDIS_URL=redis://localhost:6379
DATABASE_NAME=dev_database
SECRET_KEY=env-secret-123
`;
describe("prepareEnvironmentVariables (environment variables)", () => {
it("resolves environment variables correctly", () => {
const serviceWithEnvVars = `
NODE_ENV=\${{environment.NODE_ENV}}
API_URL=\${{environment.API_URL}}
SERVICE_PORT=4000
`;
const resolved = prepareEnvironmentVariables(
serviceWithEnvVars,
"",
environmentEnv,
);
expect(resolved).toEqual([
"NODE_ENV=development",
"API_URL=https://api.dev.example.com",
"SERVICE_PORT=4000",
]);
});
it("resolves both project and environment variables", () => {
const serviceWithBoth = `
ENVIRONMENT=\${{project.ENVIRONMENT}}
NODE_ENV=\${{environment.NODE_ENV}}
API_URL=\${{environment.API_URL}}
DATABASE_URL=\${{project.DATABASE_URL}}
SERVICE_PORT=4000
`;
const resolved = prepareEnvironmentVariables(
serviceWithBoth,
projectEnv,
environmentEnv,
);
expect(resolved).toEqual([
"ENVIRONMENT=staging",
"NODE_ENV=development",
"API_URL=https://api.dev.example.com",
"DATABASE_URL=postgres://postgres:postgres@localhost:5432/project_db",
"SERVICE_PORT=4000",
]);
});
it("handles undefined environment variables", () => {
const serviceWithUndefined = `
UNDEFINED_VAR=\${{environment.UNDEFINED_VAR}}
`;
expect(() =>
prepareEnvironmentVariables(serviceWithUndefined, "", environmentEnv),
).toThrow("Invalid environment variable: environment.UNDEFINED_VAR");
});
it("allows service variables to override environment variables", () => {
const serviceOverrideEnv = `
NODE_ENV=production
API_URL=\${{environment.API_URL}}
`;
const resolved = prepareEnvironmentVariables(
serviceOverrideEnv,
"",
environmentEnv,
);
expect(resolved).toEqual([
"NODE_ENV=production", // Overrides environment variable
"API_URL=https://api.dev.example.com",
]);
});
it("resolves complex references with project, environment, and service variables", () => {
const complexServiceEnv = `
FULL_DATABASE_URL=\${{project.DATABASE_URL}}/\${{environment.DATABASE_NAME}}
API_ENDPOINT=\${{environment.API_URL}}/\${{project.ENVIRONMENT}}/api
SERVICE_NAME=my-service
COMPLEX_VAR=\${{SERVICE_NAME}}-\${{environment.NODE_ENV}}-\${{project.ENVIRONMENT}}
`;
const resolved = prepareEnvironmentVariables(
complexServiceEnv,
projectEnv,
environmentEnv,
);
expect(resolved).toEqual([
"FULL_DATABASE_URL=postgres://postgres:postgres@localhost:5432/project_db/dev_database",
"API_ENDPOINT=https://api.dev.example.com/staging/api",
"SERVICE_NAME=my-service",
"COMPLEX_VAR=my-service-development-staging",
]);
});
it("handles environment variables with special characters", () => {
const specialEnvVars = `
SPECIAL_URL=https://special.com
COMPLEX_KEY="key-with-@#$%^&*()"
JWT_SECRET="secret-with-spaces and symbols!@#"
`;
const serviceWithSpecial = `
FULL_URL=\${{environment.SPECIAL_URL}}/path?key=\${{environment.COMPLEX_KEY}}
AUTH_SECRET=\${{environment.JWT_SECRET}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithSpecial,
"",
specialEnvVars,
);
expect(resolved).toEqual([
"FULL_URL=https://special.com/path?key=key-with-@#$%^&*()",
"AUTH_SECRET=secret-with-spaces and symbols!@#",
]);
});
it("maintains precedence: service > environment > project", () => {
const conflictingProjectEnv = `
NODE_ENV=production-project
API_URL=https://project.api.com
DATABASE_NAME=project_db
`;
const conflictingEnvironmentEnv = `
NODE_ENV=development-environment
API_URL=https://environment.api.com
DATABASE_NAME=env_db
`;
const serviceWithConflicts = `
NODE_ENV=service-override
PROJECT_ENV=\${{project.NODE_ENV}}
ENV_VAR=\${{environment.API_URL}}
DB_NAME=\${{environment.DATABASE_NAME}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithConflicts,
conflictingProjectEnv,
conflictingEnvironmentEnv,
);
expect(resolved).toEqual([
"NODE_ENV=service-override", // Service wins
"PROJECT_ENV=production-project", // Project reference
"ENV_VAR=https://environment.api.com", // Environment reference
"DB_NAME=env_db", // Environment reference
]);
});
it("handles empty environment variables", () => {
const serviceWithEmpty = `
SERVICE_VAR=test
PROJECT_VAR=\${{project.ENVIRONMENT}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithEmpty,
projectEnv,
"",
);
expect(resolved).toEqual(["SERVICE_VAR=test", "PROJECT_VAR=staging"]);
});
it("handles mixed quotes and environment variables", () => {
const envWithQuotes = `
QUOTED_VAR="development"
SINGLE_QUOTED='https://api.dev.example.com'
MIXED_VAR="value with 'single' quotes"
`;
const serviceWithQuotes = `
NODE_ENV=\${{environment.QUOTED_VAR}}
API_URL=\${{environment.SINGLE_QUOTED}}
COMPLEX="Prefix-\${{environment.MIXED_VAR}}-Suffix"
`;
const resolved = prepareEnvironmentVariables(
serviceWithQuotes,
"",
envWithQuotes,
);
expect(resolved).toEqual([
"NODE_ENV=development",
"API_URL=https://api.dev.example.com",
"COMPLEX=Prefix-value with 'single' quotes-Suffix",
]);
});
it("resolves multiple environment references in single value", () => {
const multiRefEnv = `
HOST=localhost
PORT=5432
USERNAME=postgres
PASSWORD=secret123
`;
const serviceWithMultiRefs = `
DATABASE_URL=postgresql://\${{environment.USERNAME}}:\${{environment.PASSWORD}}@\${{environment.HOST}}:\${{environment.PORT}}/mydb
CONNECTION_STRING=\${{environment.HOST}}:\${{environment.PORT}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithMultiRefs,
"",
multiRefEnv,
);
expect(resolved).toEqual([
"DATABASE_URL=postgresql://postgres:secret123@localhost:5432/mydb",
"CONNECTION_STRING=localhost:5432",
]);
});
it("handles nested references with environment and project variables", () => {
const nestedProjectEnv = `
BASE_DOMAIN=example.com
PROTOCOL=https
`;
const nestedEnvironmentEnv = `
SUBDOMAIN=api.dev
PATH_PREFIX=/v1
`;
const serviceWithNested = `
FULL_URL=\${{project.PROTOCOL}}://\${{environment.SUBDOMAIN}}.\${{project.BASE_DOMAIN}}\${{environment.PATH_PREFIX}}/endpoint
API_BASE=\${{project.PROTOCOL}}://\${{environment.SUBDOMAIN}}.\${{project.BASE_DOMAIN}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithNested,
nestedProjectEnv,
nestedEnvironmentEnv,
);
expect(resolved).toEqual([
"FULL_URL=https://api.dev.example.com/v1/endpoint",
"API_BASE=https://api.dev.example.com",
]);
});
it("throws error for malformed environment variable references", () => {
const serviceWithMalformed = `
MALFORMED1=\${{environment.}}
MALFORMED2=\${{environment}}
VALID=\${{environment.NODE_ENV}}
`;
// Should throw error for empty variable name after environment.
expect(() =>
prepareEnvironmentVariables(serviceWithMalformed, "", environmentEnv),
).toThrow("Invalid environment variable: environment.");
});
it("handles environment variables with numeric values", () => {
const numericEnv = `
PORT=8080
TIMEOUT=30
RETRY_COUNT=3
PERCENTAGE=99.5
`;
const serviceWithNumeric = `
SERVER_PORT=\${{environment.PORT}}
REQUEST_TIMEOUT=\${{environment.TIMEOUT}}
MAX_RETRIES=\${{environment.RETRY_COUNT}}
SUCCESS_RATE=\${{environment.PERCENTAGE}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithNumeric,
"",
numericEnv,
);
expect(resolved).toEqual([
"SERVER_PORT=8080",
"REQUEST_TIMEOUT=30",
"MAX_RETRIES=3",
"SUCCESS_RATE=99.5",
]);
});
it("handles boolean-like environment variables", () => {
const booleanEnv = `
DEBUG=true
ENABLED=false
PRODUCTION=1
DEVELOPMENT=0
`;
const serviceWithBoolean = `
DEBUG_MODE=\${{environment.DEBUG}}
FEATURE_ENABLED=\${{environment.ENABLED}}
IS_PROD=\${{environment.PRODUCTION}}
IS_DEV=\${{environment.DEVELOPMENT}}
`;
const resolved = prepareEnvironmentVariables(
serviceWithBoolean,
"",
booleanEnv,
);
expect(resolved).toEqual([
"DEBUG_MODE=true",
"FEATURE_ENABLED=false",
"IS_PROD=1",
"IS_DEV=0",
]);
});
});
+12 -4
View File
@@ -36,13 +36,22 @@ const baseApp: ApplicationNested = {
previewLimit: 0, previewLimit: 0,
previewCustomCertResolver: null, previewCustomCertResolver: null,
previewWildcard: "", previewWildcard: "",
project: { environmentId: "",
environment: {
env: "", env: "",
organizationId: "", environmentId: "",
name: "", name: "",
description: "",
createdAt: "", createdAt: "",
description: "",
projectId: "", projectId: "",
project: {
env: "",
organizationId: "",
name: "",
description: "",
createdAt: "",
projectId: "",
},
}, },
buildPath: "/", buildPath: "/",
gitlabPathNamespace: "", gitlabPathNamespace: "",
@@ -85,7 +94,6 @@ const baseApp: ApplicationNested = {
password: null, password: null,
placementSwarm: null, placementSwarm: null,
ports: [], ports: [],
projectId: "",
publishDirectory: null, publishDirectory: null,
isStaticSpa: null, isStaticSpa: null,
redirects: [], redirects: [],
@@ -69,7 +69,7 @@ export const ShowGeneralApplication = ({ applicationId }: Props) => {
toast.success("Application deployed successfully"); toast.success("Application deployed successfully");
refetch(); refetch();
router.push( router.push(
`/dashboard/project/${data?.projectId}/services/application/${applicationId}?tab=deployments`, `/dashboard/project/${data?.environment.projectId}/environment/${data?.environmentId}/services/application/${applicationId}?tab=deployments`,
); );
}) })
.catch(() => { .catch(() => {
@@ -101,7 +101,9 @@ export const DeleteService = ({ id, type }: Props) => {
deleteVolumes, deleteVolumes,
}) })
.then((result) => { .then((result) => {
push(`/dashboard/project/${result?.projectId}`); push(
`/dashboard/project/${result?.environment?.projectId}/environment/${result?.environment?.environmentId}`,
);
toast.success("deleted successfully"); toast.success("deleted successfully");
setIsOpen(false); setIsOpen(false);
}) })
@@ -47,7 +47,7 @@ export const ComposeActions = ({ composeId }: Props) => {
toast.success("Compose deployed successfully"); toast.success("Compose deployed successfully");
refetch(); refetch();
router.push( router.push(
`/dashboard/project/${data?.project.projectId}/services/compose/${composeId}?tab=deployments`, `/dashboard/project/${data?.environment.projectId}/environment/${data?.environmentId}/services/compose/${composeId}?tab=deployments`,
); );
}) })
.catch(() => { .catch(() => {
@@ -1,10 +1,10 @@
import { TemplateGenerator } from "@/components/dashboard/project/ai/template-generator"; import { TemplateGenerator } from "@/components/dashboard/project/ai/template-generator";
interface Props { interface Props {
projectId: string; environmentId: string;
projectName?: string; projectName?: string;
} }
export const AddAiAssistant = ({ projectId }: Props) => { export const AddAiAssistant = ({ environmentId }: Props) => {
return <TemplateGenerator projectId={projectId} />; return <TemplateGenerator environmentId={environmentId} />;
}; };
@@ -64,11 +64,11 @@ const AddTemplateSchema = z.object({
type AddTemplate = z.infer<typeof AddTemplateSchema>; type AddTemplate = z.infer<typeof AddTemplateSchema>;
interface Props { interface Props {
projectId: string; environmentId: string;
projectName?: string; projectName?: string;
} }
export const AddApplication = ({ projectId, projectName }: Props) => { export const AddApplication = ({ environmentId, projectName }: Props) => {
const utils = api.useUtils(); const utils = api.useUtils();
const { data: isCloud } = api.settings.isCloud.useQuery(); const { data: isCloud } = api.settings.isCloud.useQuery();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
@@ -94,15 +94,15 @@ export const AddApplication = ({ projectId, projectName }: Props) => {
name: data.name, name: data.name,
appName: data.appName, appName: data.appName,
description: data.description, description: data.description,
projectId, environmentId,
serverId: data.serverId, serverId: data.serverId,
}) })
.then(async () => { .then(async () => {
toast.success("Service Created"); toast.success("Service Created");
form.reset(); form.reset();
setVisible(false); setVisible(false);
await utils.project.one.invalidate({ await utils.environment.one.invalidate({
projectId, environmentId,
}); });
}) })
.catch(() => { .catch(() => {
@@ -65,11 +65,11 @@ const AddComposeSchema = z.object({
type AddCompose = z.infer<typeof AddComposeSchema>; type AddCompose = z.infer<typeof AddComposeSchema>;
interface Props { interface Props {
projectId: string; environmentId: string;
projectName?: string; projectName?: string;
} }
export const AddCompose = ({ projectId, projectName }: Props) => { export const AddCompose = ({ environmentId, projectName }: Props) => {
const utils = api.useUtils(); const utils = api.useUtils();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
const slug = slugify(projectName); const slug = slugify(projectName);
@@ -78,6 +78,9 @@ export const AddCompose = ({ projectId, projectName }: Props) => {
const { mutateAsync, isLoading, error, isError } = const { mutateAsync, isLoading, error, isError } =
api.compose.create.useMutation(); api.compose.create.useMutation();
// Get environment data to extract projectId
const { data: environment } = api.environment.one.useQuery({ environmentId });
const hasServers = servers && servers.length > 0; const hasServers = servers && servers.length > 0;
const form = useForm<AddCompose>({ const form = useForm<AddCompose>({
@@ -98,7 +101,7 @@ export const AddCompose = ({ projectId, projectName }: Props) => {
await mutateAsync({ await mutateAsync({
name: data.name, name: data.name,
description: data.description, description: data.description,
projectId, environmentId,
composeType: data.composeType, composeType: data.composeType,
appName: data.appName, appName: data.appName,
serverId: data.serverId, serverId: data.serverId,
@@ -106,8 +109,9 @@ export const AddCompose = ({ projectId, projectName }: Props) => {
.then(async () => { .then(async () => {
toast.success("Compose Created"); toast.success("Compose Created");
setVisible(false); setVisible(false);
await utils.project.one.invalidate({ // Invalidate the project query to refresh the environment data
projectId, await utils.environment.one.invalidate({
environmentId,
}); });
}) })
.catch(() => { .catch(() => {
@@ -170,11 +170,11 @@ const databasesMap = {
type AddDatabase = z.infer<typeof mySchema>; type AddDatabase = z.infer<typeof mySchema>;
interface Props { interface Props {
projectId: string; environmentId: string;
projectName?: string; projectName?: string;
} }
export const AddDatabase = ({ projectId, projectName }: Props) => { export const AddDatabase = ({ environmentId, projectName }: Props) => {
const utils = api.useUtils(); const utils = api.useUtils();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
const slug = slugify(projectName); const slug = slugify(projectName);
@@ -185,6 +185,9 @@ export const AddDatabase = ({ projectId, projectName }: Props) => {
const mariadbMutation = api.mariadb.create.useMutation(); const mariadbMutation = api.mariadb.create.useMutation();
const mysqlMutation = api.mysql.create.useMutation(); const mysqlMutation = api.mysql.create.useMutation();
// Get environment data to extract projectId
const { data: environment } = api.environment.one.useQuery({ environmentId });
const hasServers = servers && servers.length > 0; const hasServers = servers && servers.length > 0;
const form = useForm<AddDatabase>({ const form = useForm<AddDatabase>({
@@ -219,7 +222,8 @@ export const AddDatabase = ({ projectId, projectName }: Props) => {
name: data.name, name: data.name,
appName: data.appName, appName: data.appName,
dockerImage: defaultDockerImage, dockerImage: defaultDockerImage,
projectId, projectId: environment?.projectId || "",
environmentId,
serverId: data.serverId, serverId: data.serverId,
description: data.description, description: data.description,
}; };
@@ -248,7 +252,6 @@ export const AddDatabase = ({ projectId, projectName }: Props) => {
...commonParams, ...commonParams,
databasePassword: data.databasePassword, databasePassword: data.databasePassword,
serverId: data.serverId, serverId: data.serverId,
projectId,
}); });
} else if (data.type === "mariadb") { } else if (data.type === "mariadb") {
promise = mariadbMutation.mutateAsync({ promise = mariadbMutation.mutateAsync({
@@ -287,8 +290,9 @@ export const AddDatabase = ({ projectId, projectName }: Props) => {
databaseUser: "", databaseUser: "",
}); });
setVisible(false); setVisible(false);
await utils.project.one.invalidate({ // Invalidate the project query to refresh the environment data
projectId, await utils.environment.one.invalidate({
environmentId,
}); });
}) })
.catch(() => { .catch(() => {
@@ -73,11 +73,11 @@ import { api } from "@/utils/api";
const TEMPLATE_BASE_URL_KEY = "dokploy_template_base_url"; const TEMPLATE_BASE_URL_KEY = "dokploy_template_base_url";
interface Props { interface Props {
projectId: string; environmentId: string;
baseUrl?: string; baseUrl?: string;
} }
export const AddTemplate = ({ projectId, baseUrl }: Props) => { export const AddTemplate = ({ environmentId, baseUrl }: Props) => {
const [query, setQuery] = useState(""); const [query, setQuery] = useState("");
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [viewMode, setViewMode] = useState<"detailed" | "icon">("detailed"); const [viewMode, setViewMode] = useState<"detailed" | "icon">("detailed");
@@ -91,6 +91,9 @@ export const AddTemplate = ({ projectId, baseUrl }: Props) => {
return undefined; return undefined;
}); });
// Get environment data to extract projectId
const { data: environment } = api.environment.one.useQuery({ environmentId });
// Save to localStorage when customBaseUrl changes // Save to localStorage when customBaseUrl changes
useEffect(() => { useEffect(() => {
if (customBaseUrl) { if (customBaseUrl) {
@@ -490,7 +493,7 @@ export const AddTemplate = ({ projectId, baseUrl }: Props) => {
disabled={isLoading} disabled={isLoading}
onClick={async () => { onClick={async () => {
const promise = mutateAsync({ const promise = mutateAsync({
projectId, environmentId,
serverId: serverId || undefined, serverId: serverId || undefined,
id: template.id, id: template.id,
baseUrl: customBaseUrl, baseUrl: customBaseUrl,
@@ -498,8 +501,9 @@ export const AddTemplate = ({ projectId, baseUrl }: Props) => {
toast.promise(promise, { toast.promise(promise, {
loading: "Setting up...", loading: "Setting up...",
success: () => { success: () => {
utils.project.one.invalidate({ // Invalidate the project query to refresh the environment data
projectId, utils.environment.one.invalidate({
environmentId,
}); });
setOpen(false); setOpen(false);
return `${template.name} template created successfully`; return `${template.name} template created successfully`;
@@ -0,0 +1,446 @@
import type { findEnvironmentsByProjectId } from "@dokploy/server";
import {
ChevronDownIcon,
PencilIcon,
PlusIcon,
Terminal,
TrashIcon,
} from "lucide-react";
import { useRouter } from "next/router";
import { useState } from "react";
import { toast } from "sonner";
import { EnvironmentVariables } from "@/components/dashboard/project/environment-variables";
import { AlertBlock } from "@/components/shared/alert-block";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { api } from "@/utils/api";
type Environment = Awaited<
ReturnType<typeof findEnvironmentsByProjectId>
>[number];
interface AdvancedEnvironmentSelectorProps {
projectId: string;
currentEnvironmentId?: string;
}
export const AdvancedEnvironmentSelector = ({
projectId,
currentEnvironmentId,
}: AdvancedEnvironmentSelectorProps) => {
const router = useRouter();
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false);
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
const [selectedEnvironment, setSelectedEnvironment] =
useState<Environment | null>(null);
const { data: environments } = api.environment.byProjectId.useQuery(
{ projectId: projectId },
{
enabled: !!projectId,
},
);
// Form states
const [name, setName] = useState("");
const [description, setDescription] = useState("");
// API mutations
const { data: environment } = api.environment.one.useQuery(
{ environmentId: currentEnvironmentId || "" },
{
enabled: !!currentEnvironmentId,
},
);
const haveServices =
selectedEnvironment &&
((selectedEnvironment?.mariadb?.length || 0) > 0 ||
(selectedEnvironment?.mongo?.length || 0) > 0 ||
(selectedEnvironment?.mysql?.length || 0) > 0 ||
(selectedEnvironment?.postgres?.length || 0) > 0 ||
(selectedEnvironment?.redis?.length || 0) > 0 ||
(selectedEnvironment?.applications?.length || 0) > 0 ||
(selectedEnvironment?.compose?.length || 0) > 0);
const createEnvironment = api.environment.create.useMutation();
const updateEnvironment = api.environment.update.useMutation();
const deleteEnvironment = api.environment.remove.useMutation();
const duplicateEnvironment = api.environment.duplicate.useMutation();
// Refetch project data
const utils = api.useUtils();
const handleCreateEnvironment = async () => {
try {
await createEnvironment.mutateAsync({
projectId,
name: name.trim(),
description: description.trim() || null,
});
toast.success("Environment created successfully");
utils.environment.byProjectId.invalidate({ projectId });
setIsCreateDialogOpen(false);
setName("");
setDescription("");
} catch (error) {
toast.error("Failed to create environment");
}
};
const handleUpdateEnvironment = async () => {
if (!selectedEnvironment) return;
try {
await updateEnvironment.mutateAsync({
environmentId: selectedEnvironment.environmentId,
name: name.trim(),
description: description.trim() || null,
});
toast.success("Environment updated successfully");
utils.environment.byProjectId.invalidate({ projectId });
setIsEditDialogOpen(false);
setSelectedEnvironment(null);
setName("");
setDescription("");
} catch (error) {
toast.error("Failed to update environment");
}
};
const handleDeleteEnvironment = async () => {
if (!selectedEnvironment) return;
try {
await deleteEnvironment.mutateAsync({
environmentId: selectedEnvironment.environmentId,
});
toast.success("Environment deleted successfully");
utils.environment.byProjectId.invalidate({ projectId });
setIsDeleteDialogOpen(false);
setSelectedEnvironment(null);
// Redirect to production if we deleted the current environment
if (selectedEnvironment.environmentId === currentEnvironmentId) {
const productionEnv = environments?.find(
(env) => env.name === "production",
);
if (productionEnv) {
router.push(
`/dashboard/project/${projectId}/environment/${productionEnv.environmentId}`,
);
}
}
} catch (error) {
toast.error("Failed to delete environment");
}
};
const handleDuplicateEnvironment = async (environment: Environment) => {
try {
const result = await duplicateEnvironment.mutateAsync({
environmentId: environment.environmentId,
name: `${environment.name}-copy`,
description: environment.description,
});
toast.success("Environment duplicated successfully");
utils.project.one.invalidate({ projectId });
// Navigate to the new duplicated environment
router.push(
`/dashboard/project/${projectId}/environment/${result.environmentId}`,
);
} catch (error) {
toast.error("Failed to duplicate environment");
}
};
const openEditDialog = (environment: Environment) => {
setSelectedEnvironment(environment);
setName(environment.name);
setDescription(environment.description || "");
setIsEditDialogOpen(true);
};
const openDeleteDialog = (environment: Environment) => {
setSelectedEnvironment(environment);
setIsDeleteDialogOpen(true);
};
const currentEnv = environments?.find(
(env) => env.environmentId === currentEnvironmentId,
);
return (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-auto p-2 font-normal">
<div className="flex items-center gap-1">
<span className="text-muted-foreground">/</span>
<span>{currentEnv?.name || "Select Environment"}</span>
<ChevronDownIcon className="h-4 w-4 text-muted-foreground" />
</div>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-[300px]" align="start">
<DropdownMenuLabel>Environments</DropdownMenuLabel>
<DropdownMenuSeparator />
{environments?.map((environment) => {
const servicesCount =
environment.mariadb.length +
environment.mongo.length +
environment.mysql.length +
environment.postgres.length +
environment.redis.length +
environment.applications.length +
environment.compose.length;
return (
<div
key={environment.environmentId}
className="flex items-center"
>
<DropdownMenuItem
className="flex-1 cursor-pointer"
onClick={() => {
router.push(
`/dashboard/project/${projectId}/environment/${environment.environmentId}`,
);
}}
>
<div className="flex items-center justify-between w-full">
<span>
{environment.name} ({servicesCount})
</span>
{environment.environmentId === currentEnvironmentId && (
<div className="w-2 h-2 bg-blue-500 rounded-full" />
)}
</div>
</DropdownMenuItem>
{/* Action buttons for non-production environments */}
<EnvironmentVariables environmentId={environment.environmentId}>
<Button
variant="ghost"
size="sm"
className="h-6 w-6 p-0"
onClick={(e) => {
e.stopPropagation();
}}
>
<Terminal className="h-3 w-3" />
</Button>
</EnvironmentVariables>
{environment.name !== "production" && (
<div className="flex items-center gap-1 px-2">
<Button
variant="ghost"
size="sm"
className="h-6 w-6 p-0"
onClick={(e) => {
e.stopPropagation();
openEditDialog(environment);
}}
>
<PencilIcon className="h-3 w-3" />
</Button>
<Button
variant="ghost"
size="sm"
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
onClick={(e) => {
e.stopPropagation();
openDeleteDialog(environment);
}}
>
<TrashIcon className="h-3 w-3" />
</Button>
</div>
)}
</div>
);
})}
<DropdownMenuSeparator />
<DropdownMenuItem
className="cursor-pointer"
onClick={() => setIsCreateDialogOpen(true)}
>
<PlusIcon className="h-4 w-4 mr-2" />
Create Environment
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Create Environment</DialogTitle>
<DialogDescription>
Create a new environment for your project.
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div className="space-y-1">
<Label htmlFor="name">Name</Label>
<Input
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Environment name"
/>
</div>
<div className="space-y-1">
<Label htmlFor="description">Description (optional)</Label>
<Textarea
id="description"
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Environment description"
/>
</div>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => {
setIsCreateDialogOpen(false);
setName("");
setDescription("");
}}
>
Cancel
</Button>
<Button
onClick={handleCreateEnvironment}
disabled={!name.trim() || createEnvironment.isLoading}
>
{createEnvironment.isLoading ? "Creating..." : "Create"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{/* Edit Environment Dialog */}
<Dialog open={isEditDialogOpen} onOpenChange={setIsEditDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Edit Environment</DialogTitle>
<DialogDescription>
Update the environment details.
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div className="space-y-1">
<Label htmlFor="edit-name">Name</Label>
<Input
id="edit-name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Environment name"
/>
</div>
<div className="space-y-1">
<Label htmlFor="edit-description">Description (optional)</Label>
<Textarea
id="edit-description"
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Environment description"
/>
</div>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => {
setIsEditDialogOpen(false);
setSelectedEnvironment(null);
setName("");
setDescription("");
}}
>
Cancel
</Button>
<Button
onClick={handleUpdateEnvironment}
disabled={!name.trim() || updateEnvironment.isLoading}
>
{updateEnvironment.isLoading ? "Updating..." : "Update"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{/* Delete Environment Dialog */}
<Dialog open={isDeleteDialogOpen} onOpenChange={setIsDeleteDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Delete Environment</DialogTitle>
<DialogDescription>
Are you sure you want to delete the environment "
{selectedEnvironment?.name}"? This action cannot be undone and
will also delete all services in this environment.
</DialogDescription>
</DialogHeader>
{haveServices && (
<AlertBlock type="warning">
This environment have active services, please delete them first.
</AlertBlock>
)}
<DialogFooter>
<Button
variant="outline"
onClick={() => {
setIsDeleteDialogOpen(false);
setSelectedEnvironment(null);
}}
>
Cancel
</Button>
<Button
variant="destructive"
onClick={handleDeleteEnvironment}
disabled={
deleteEnvironment.isLoading ||
haveServices ||
!selectedEnvironment
}
>
{deleteEnvironment.isLoading ? "Deleting..." : "Delete"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
};
@@ -90,11 +90,11 @@ export const { useStepper, steps, Scoped } = defineStepper(
); );
interface Props { interface Props {
projectId: string; environmentId: string;
projectName?: string; projectName?: string;
} }
export const TemplateGenerator = ({ projectId }: Props) => { export const TemplateGenerator = ({ environmentId }: Props) => {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const stepper = useStepper(); const stepper = useStepper();
const { data: aiSettings } = api.ai.getAll.useQuery(); const { data: aiSettings } = api.ai.getAll.useQuery();
@@ -121,7 +121,7 @@ export const TemplateGenerator = ({ projectId }: Props) => {
const onSubmit = async () => { const onSubmit = async () => {
await mutateAsync({ await mutateAsync({
projectId, environmentId: environmentId,
id: templateInfo.details?.id || "", id: templateInfo.details?.id || "",
name: templateInfo?.details?.name || "", name: templateInfo?.details?.name || "",
description: templateInfo?.details?.shortDescription || "", description: templateInfo?.details?.shortDescription || "",
@@ -138,8 +138,9 @@ export const TemplateGenerator = ({ projectId }: Props) => {
.then(async () => { .then(async () => {
toast.success("Compose Created"); toast.success("Compose Created");
setOpen(false); setOpen(false);
await utils.project.one.invalidate({ // Invalidate the project query to refresh the environment data
projectId, await utils.environment.one.invalidate({
environmentId,
}); });
}) })
.catch(() => { .catch(() => {
@@ -15,6 +15,13 @@ import {
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
export type Services = { export type Services = {
@@ -36,23 +43,35 @@ export type Services = {
}; };
interface DuplicateProjectProps { interface DuplicateProjectProps {
projectId: string; environmentId: string;
services: Services[]; services: Services[];
selectedServiceIds: string[]; selectedServiceIds: string[];
} }
export const DuplicateProject = ({ export const DuplicateProject = ({
projectId, environmentId,
services, services,
selectedServiceIds, selectedServiceIds,
}: DuplicateProjectProps) => { }: DuplicateProjectProps) => {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [name, setName] = useState(""); const [name, setName] = useState("");
const [description, setDescription] = useState(""); const [description, setDescription] = useState("");
const [duplicateType, setDuplicateType] = useState("new-project"); // "new-project" or "same-project" const [duplicateType, setDuplicateType] = useState("new-project"); // "new-project" or "existing-environment"
const [selectedTargetProject, setSelectedTargetProject] =
useState<string>("");
const [selectedTargetEnvironment, setSelectedTargetEnvironment] =
useState<string>("");
const utils = api.useUtils(); const utils = api.useUtils();
const router = useRouter(); const router = useRouter();
// Queries for project and environment selection
const { data: allProjects } = api.project.all.useQuery();
const { data: selectedProjectEnvironments } =
api.environment.byProjectId.useQuery(
{ projectId: selectedTargetProject },
{ enabled: !!selectedTargetProject },
);
const selectedServices = services.filter((service) => const selectedServices = services.filter((service) =>
selectedServiceIds.includes(service.id), selectedServiceIds.includes(service.id),
); );
@@ -68,7 +87,9 @@ export const DuplicateProject = ({
); );
setOpen(false); setOpen(false);
if (duplicateType === "new-project") { if (duplicateType === "new-project") {
router.push(`/dashboard/project/${newProject.projectId}`); router.push(
`/dashboard/project/${newProject?.projectId}/environment/${newProject?.environmentId}`,
);
} }
}, },
onError: (error) => { onError: (error) => {
@@ -82,8 +103,20 @@ export const DuplicateProject = ({
return; return;
} }
if (duplicateType === "existing-environment") {
if (!selectedTargetProject) {
toast.error("Please select a target project");
return;
}
if (!selectedTargetEnvironment) {
toast.error("Please select a target environment");
return;
}
}
// TODO: Update duplicate API to support targetProjectId and targetEnvironmentId
await duplicateProject({ await duplicateProject({
sourceProjectId: projectId, sourceEnvironmentId: selectedTargetEnvironment,
name, name,
description, description,
includeServices: true, includeServices: true,
@@ -91,7 +124,7 @@ export const DuplicateProject = ({
id: service.id, id: service.id,
type: service.type, type: service.type,
})), })),
duplicateInSameProject: duplicateType === "same-project", duplicateInSameProject: duplicateType === "existing-environment",
}); });
}; };
@@ -105,6 +138,8 @@ export const DuplicateProject = ({
setName(""); setName("");
setDescription(""); setDescription("");
setDuplicateType("new-project"); setDuplicateType("new-project");
setSelectedTargetProject("");
setSelectedTargetEnvironment("");
} }
}} }}
> >
@@ -127,7 +162,14 @@ export const DuplicateProject = ({
<Label>Duplicate to</Label> <Label>Duplicate to</Label>
<RadioGroup <RadioGroup
value={duplicateType} value={duplicateType}
onValueChange={setDuplicateType} onValueChange={(value) => {
setDuplicateType(value);
// Reset selections when changing type
if (value !== "existing-environment") {
setSelectedTargetProject("");
setSelectedTargetEnvironment("");
}
}}
className="grid gap-2" className="grid gap-2"
> >
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
@@ -135,8 +177,13 @@ export const DuplicateProject = ({
<Label htmlFor="new-project">New project</Label> <Label htmlFor="new-project">New project</Label>
</div> </div>
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<RadioGroupItem value="same-project" id="same-project" /> <RadioGroupItem
<Label htmlFor="same-project">Same project</Label> value="existing-environment"
id="existing-environment"
/>
<Label htmlFor="existing-environment">
Existing environment
</Label>
</div> </div>
</RadioGroup> </RadioGroup>
</div> </div>
@@ -165,6 +212,74 @@ export const DuplicateProject = ({
</> </>
)} )}
{duplicateType === "existing-environment" && (
<>
{allProjects?.filter((p) => p.projectId !== environmentId)
.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-2 py-4 text-center">
<p className="text-sm text-muted-foreground">
No other projects available. Create a new project first.
</p>
</div>
) : (
<>
{/* Step 1: Select Project */}
<div className="grid gap-2">
<Label>Target Project</Label>
<Select
value={selectedTargetProject}
onValueChange={(value) => {
setSelectedTargetProject(value);
setSelectedTargetEnvironment(""); // Reset environment when project changes
}}
>
<SelectTrigger>
<SelectValue placeholder="Select target project" />
</SelectTrigger>
<SelectContent>
{allProjects
?.filter((p) => p.projectId !== environmentId)
.map((project) => (
<SelectItem
key={project.projectId}
value={project.projectId}
>
{project.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Step 2: Select Environment (only show if project is selected) */}
{selectedTargetProject && (
<div className="grid gap-2">
<Label>Target Environment</Label>
<Select
value={selectedTargetEnvironment}
onValueChange={setSelectedTargetEnvironment}
>
<SelectTrigger>
<SelectValue placeholder="Select target environment" />
</SelectTrigger>
<SelectContent>
{selectedProjectEnvironments?.map((env) => (
<SelectItem
key={env.environmentId}
value={env.environmentId}
>
{env.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)}
</>
)}
</>
)}
<div className="grid gap-2"> <div className="grid gap-2">
<Label>Selected services to duplicate</Label> <Label>Selected services to duplicate</Label>
<div className="space-y-2 max-h-[200px] overflow-y-auto border rounded-md p-4"> <div className="space-y-2 max-h-[200px] overflow-y-auto border rounded-md p-4">
@@ -187,18 +302,26 @@ export const DuplicateProject = ({
> >
Cancel Cancel
</Button> </Button>
<Button onClick={handleDuplicate} disabled={isLoading}> <Button
onClick={handleDuplicate}
disabled={
isLoading ||
(duplicateType === "new-project" && !name) ||
(duplicateType === "existing-environment" &&
(!selectedTargetProject || !selectedTargetEnvironment))
}
>
{isLoading ? ( {isLoading ? (
<> <>
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> <Loader2 className="mr-2 h-4 w-4 animate-spin" />
{duplicateType === "new-project" {duplicateType === "new-project"
? "Duplicating project..." ? "Duplicating to new project..."
: "Duplicating services..."} : "Duplicating to environment..."}
</> </>
) : duplicateType === "new-project" ? ( ) : duplicateType === "new-project" ? (
"Duplicate project" "Duplicate to new project"
) : ( ) : (
"Duplicate services" "Duplicate to environment"
)} )}
</Button> </Button>
</DialogFooter> </DialogFooter>
@@ -0,0 +1,157 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { Terminal } from "lucide-react";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import { AlertBlock } from "@/components/shared/alert-block";
import { CodeEditor } from "@/components/shared/code-editor";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { api } from "@/utils/api";
const updateEnvironmentSchema = z.object({
env: z.string().optional(),
});
type UpdateEnvironment = z.infer<typeof updateEnvironmentSchema>;
interface Props {
environmentId: string;
children?: React.ReactNode;
}
export const EnvironmentVariables = ({ environmentId, children }: Props) => {
const [isOpen, setIsOpen] = useState(false);
const utils = api.useUtils();
const { mutateAsync, error, isError, isLoading } =
api.environment.update.useMutation();
const { data } = api.environment.one.useQuery(
{
environmentId,
},
{
enabled: !!environmentId,
},
);
const form = useForm<UpdateEnvironment>({
defaultValues: {
env: data?.env ?? "",
},
resolver: zodResolver(updateEnvironmentSchema),
});
useEffect(() => {
if (data) {
form.reset({
env: data.env ?? "",
});
}
}, [data, form, form.reset]);
const onSubmit = async (formData: UpdateEnvironment) => {
await mutateAsync({
env: formData.env || "",
environmentId: environmentId,
})
.then(() => {
toast.success("Environment variables updated successfully");
utils.environment.one.invalidate({ environmentId });
})
.catch(() => {
toast.error("Error updating the environment variables");
})
.finally(() => {});
};
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
{children ?? (
<DropdownMenuItem
className="w-full cursor-pointer space-x-3"
onSelect={(e) => e.preventDefault()}
>
<Terminal className="size-4" />
<span>Environment Variables</span>
</DropdownMenuItem>
)}
</DialogTrigger>
<DialogContent className="sm:max-w-6xl">
<DialogHeader>
<DialogTitle>Environment Variables</DialogTitle>
<DialogDescription>
Update the environment variables that are accessible to all services
in this environment.
</DialogDescription>
</DialogHeader>
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
<AlertBlock type="info">
Use this syntax to reference environment-level variables in your
service environments:{" "}
<code>API_URL=${"{{environment.API_URL}}"}</code>
</AlertBlock>
<div className="grid gap-4">
<div className="grid items-center gap-4">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="grid w-full gap-4 "
>
<FormField
control={form.control}
name="env"
render={({ field }) => (
<FormItem>
<FormLabel>Environment variables</FormLabel>
<FormControl>
<CodeEditor
lineWrapping
language="properties"
wrapperClassName="h-[35rem] font-mono"
placeholder={`NODE_ENV=development
DATABASE_URL=postgresql://localhost:5432/mydb
API_KEY=your-api-key-here
`}
{...field}
/>
</FormControl>
<pre>
<FormMessage />
</pre>
</FormItem>
)}
/>
<DialogFooter>
<Button isLoading={isLoading} type="submit">
Update
</Button>
</DialogFooter>
</form>
</Form>
</div>
</div>
</DialogContent>
</Dialog>
);
};
@@ -101,7 +101,18 @@ export const HandleProject = ({ projectId }: Props) => {
toast.success(projectId ? "Project Updated" : "Project Created"); toast.success(projectId ? "Project Updated" : "Project Created");
setIsOpen(false); setIsOpen(false);
if (!projectId) { if (!projectId) {
router.push(`/dashboard/project/${data?.projectId}`); const projectIdToUse =
data && "project" in data ? data.project.projectId : undefined;
const environmentIdToUse =
data && "environment" in data
? data.environment.environmentId
: undefined;
if (environmentIdToUse && projectIdToUse) {
router.push(
`/dashboard/project/${projectIdToUse}/environment/${environmentIdToUse}`,
);
}
} else { } else {
refetch(); refetch();
} }
@@ -96,22 +96,8 @@ export const ShowProjects = () => {
new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(); new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
break; break;
case "services": { case "services": {
const aTotalServices = const aTotalServices = a.environments.length;
a.mariadb.length + const bTotalServices = b.environments.length;
a.mongo.length +
a.mysql.length +
a.postgres.length +
a.redis.length +
a.applications.length +
a.compose.length;
const bTotalServices =
b.mariadb.length +
b.mongo.length +
b.mysql.length +
b.postgres.length +
b.redis.length +
b.applications.length +
b.compose.length;
comparison = aTotalServices - bTotalServices; comparison = aTotalServices - bTotalServices;
break; break;
} }
@@ -201,23 +187,40 @@ export const ShowProjects = () => {
)} )}
<div className="w-full grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 3xl:grid-cols-5 flex-wrap gap-5"> <div className="w-full grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 3xl:grid-cols-5 flex-wrap gap-5">
{filteredProjects?.map((project) => { {filteredProjects?.map((project) => {
const emptyServices = const emptyServices = project?.environments
project?.mariadb.length === 0 && .map(
project?.mongo.length === 0 && (env) =>
project?.mysql.length === 0 && env.applications.length === 0 &&
project?.postgres.length === 0 && env.mariadb.length === 0 &&
project?.redis.length === 0 && env.mongo.length === 0 &&
project?.applications.length === 0 && env.mysql.length === 0 &&
project?.compose.length === 0; env.postgres.length === 0 &&
env.redis.length === 0 &&
env.applications.length === 0 &&
env.compose.length === 0,
)
.every(Boolean);
const totalServices = const totalServices = project?.environments
project?.mariadb.length + .map(
project?.mongo.length + (env) =>
project?.mysql.length + env.mariadb.length +
project?.postgres.length + env.mongo.length +
project?.redis.length + env.mysql.length +
project?.applications.length + env.postgres.length +
project?.compose.length; env.redis.length +
env.applications.length +
env.compose.length,
)
.reduce((acc, curr) => acc + curr, 0);
const haveServicesWithDomains = project?.environments
.map(
(env) =>
env.applications.length > 0 ||
env.compose.length > 0,
)
.some(Boolean);
return ( return (
<div <div
@@ -225,11 +228,10 @@ export const ShowProjects = () => {
className="w-full lg:max-w-md" className="w-full lg:max-w-md"
> >
<Link <Link
href={`/dashboard/project/${project.projectId}`} href={`/dashboard/project/${project.projectId}/environment/${project?.environments?.[0]?.environmentId}`}
> >
<Card className="group relative w-full h-full bg-transparent transition-colors hover:bg-border"> <Card className="group relative w-full h-full bg-transparent transition-colors hover:bg-border">
{project.applications.length > 0 || {haveServicesWithDomains ? (
project.compose.length > 0 ? (
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
<Button <Button
@@ -244,44 +246,51 @@ export const ShowProjects = () => {
className="w-[200px] space-y-2 overflow-y-auto max-h-[400px]" className="w-[200px] space-y-2 overflow-y-auto max-h-[400px]"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}
> >
{project.applications.length > 0 && ( {project.environments.some(
(env) => env.applications.length > 0,
) && (
<DropdownMenuGroup> <DropdownMenuGroup>
<DropdownMenuLabel> <DropdownMenuLabel>
Applications Applications
</DropdownMenuLabel> </DropdownMenuLabel>
{project.applications.map((app) => ( {project.environments.map((env) =>
<div key={app.applicationId}> env.applications.map((app) => (
<DropdownMenuSeparator /> <div key={app.applicationId}>
<DropdownMenuGroup>
<DropdownMenuLabel className="font-normal capitalize text-xs flex items-center justify-between">
{app.name}
<StatusTooltip
status={app.applicationStatus}
/>
</DropdownMenuLabel>
<DropdownMenuSeparator /> <DropdownMenuSeparator />
{app.domains.map((domain) => ( <DropdownMenuGroup>
<DropdownMenuItem <DropdownMenuLabel className="font-normal capitalize text-xs flex items-center justify-between">
key={domain.domainId} {app.name}
asChild <StatusTooltip
> status={
<Link app.applicationStatus
className="space-x-4 text-xs cursor-pointer justify-between" }
target="_blank" />
href={`${domain.https ? "https" : "http"}://${domain.host}${domain.path}`} </DropdownMenuLabel>
<DropdownMenuSeparator />
{app.domains.map((domain) => (
<DropdownMenuItem
key={domain.domainId}
asChild
> >
<span className="truncate"> <Link
{domain.host} className="space-x-4 text-xs cursor-pointer justify-between"
</span> target="_blank"
<ExternalLinkIcon className="size-4 shrink-0" /> href={`${domain.https ? "https" : "http"}://${domain.host}${domain.path}`}
</Link> >
</DropdownMenuItem> <span className="truncate">
))} {domain.host}
</DropdownMenuGroup> </span>
</div> <ExternalLinkIcon className="size-4 shrink-0" />
))} </Link>
</DropdownMenuItem>
))}
</DropdownMenuGroup>
</div>
)),
)}
</DropdownMenuGroup> </DropdownMenuGroup>
)} )}
{/*
{project.compose.length > 0 && ( {project.compose.length > 0 && (
<DropdownMenuGroup> <DropdownMenuGroup>
<DropdownMenuLabel> <DropdownMenuLabel>
@@ -319,7 +328,7 @@ export const ShowProjects = () => {
</div> </div>
))} ))}
</DropdownMenuGroup> </DropdownMenuGroup>
)} )} */}
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
) : null} ) : null}
@@ -20,10 +20,10 @@ import {
CommandSeparator, CommandSeparator,
} from "@/components/ui/command"; } from "@/components/ui/command";
import { authClient } from "@/lib/auth-client"; import { authClient } from "@/lib/auth-client";
import { // import {
extractServices, // extractServices,
type Services, // type Services,
} from "@/pages/dashboard/project/[projectId]"; // } from "@/pages/dashboard/project/[projectId]";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
import { StatusTooltip } from "../shared/status-tooltip"; import { StatusTooltip } from "../shared/status-tooltip";
@@ -51,7 +51,7 @@ export const SearchCommand = () => {
return ( return (
<div> <div>
<CommandDialog open={open} onOpenChange={setOpen}> {/* <CommandDialog open={open} onOpenChange={setOpen}>
<CommandInput <CommandInput
placeholder={"Search projects or settings"} placeholder={"Search projects or settings"}
value={search} value={search}
@@ -87,7 +87,7 @@ export const SearchCommand = () => {
key={application.id} key={application.id}
onSelect={() => { onSelect={() => {
router.push( router.push(
`/dashboard/project/${project.projectId}/services/${application.type}/${application.id}`, `/dashboard/project/${project.projectId}/environment/${application.environmentId}/services/${application.type}/${application.id}`,
); );
setOpen(false); setOpen(false);
}} }}
@@ -181,7 +181,7 @@ export const SearchCommand = () => {
</CommandItem> </CommandItem>
</CommandGroup> </CommandGroup>
</CommandList> </CommandList>
</CommandDialog> </CommandDialog> */}
</div> </div>
); );
}; };
@@ -35,9 +35,9 @@ import {
PopoverContent, PopoverContent,
PopoverTrigger, PopoverTrigger,
} from "@/components/ui/popover"; } from "@/components/ui/popover";
import { extractServices } from "@/pages/dashboard/project/[projectId]";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
import { useUrl } from "@/utils/hooks/use-url"; import { useUrl } from "@/utils/hooks/use-url";
import { extractServices } from "../users/add-permissions";
interface Props { interface Props {
serverId?: string; serverId?: string;
@@ -95,11 +95,13 @@ export const SetupMonitoring = ({ serverId }: Props) => {
const { data: projects } = api.project.all.useQuery(); const { data: projects } = api.project.all.useQuery();
const extractServicesFromProjects = (projects: any[] | undefined) => { const extractServicesFromProjects = () => {
if (!projects) return []; if (!projects) return [];
const allServices = projects.flatMap((project) => { const allServices = projects.flatMap((project) => {
const services = extractServices(project); const services = project.environments.flatMap((env) =>
extractServices(env),
);
return serverId return serverId
? services ? services
.filter((service) => service.serverId === serverId) .filter((service) => service.serverId === serverId)
@@ -110,7 +112,7 @@ export const SetupMonitoring = ({ serverId }: Props) => {
return [...new Set(allServices)]; return [...new Set(allServices)];
}; };
const services = extractServicesFromProjects(projects); const services = extractServicesFromProjects();
const form = useForm<Schema>({ const form = useForm<Schema>({
resolver: zodResolver(Schema), resolver: zodResolver(Schema),
@@ -1,3 +1,4 @@
import type { findEnvironmentById } from "@dokploy/server/index";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useEffect } from "react"; import { useEffect } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
@@ -26,11 +27,135 @@ import {
FormMessage, FormMessage,
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Switch } from "@/components/ui/switch"; import { Switch } from "@/components/ui/switch";
import { extractServices } from "@/pages/dashboard/project/[projectId]";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
type Environment = Omit<
Awaited<ReturnType<typeof findEnvironmentById>>,
"project"
>;
export type Services = {
appName: string;
serverId?: string | null;
name: string;
type:
| "mariadb"
| "application"
| "postgres"
| "mysql"
| "mongo"
| "redis"
| "compose";
description?: string | null;
id: string;
createdAt: string;
status?: "idle" | "running" | "done" | "error";
};
export const extractServices = (data: Environment | undefined) => {
const applications: Services[] =
data?.applications.map((item) => ({
appName: item.appName,
name: item.name,
type: "application",
id: item.applicationId,
createdAt: item.createdAt,
status: item.applicationStatus,
description: item.description,
serverId: item.serverId,
})) || [];
const mariadb: Services[] =
data?.mariadb.map((item) => ({
appName: item.appName,
name: item.name,
type: "mariadb",
id: item.mariadbId,
createdAt: item.createdAt,
status: item.applicationStatus,
description: item.description,
serverId: item.serverId,
})) || [];
const postgres: Services[] =
data?.postgres.map((item) => ({
appName: item.appName,
name: item.name,
type: "postgres",
id: item.postgresId,
createdAt: item.createdAt,
status: item.applicationStatus,
description: item.description,
serverId: item.serverId,
})) || [];
const mongo: Services[] =
data?.mongo.map((item) => ({
appName: item.appName,
name: item.name,
type: "mongo",
id: item.mongoId,
createdAt: item.createdAt,
status: item.applicationStatus,
description: item.description,
serverId: item.serverId,
})) || [];
const redis: Services[] =
data?.redis.map((item) => ({
appName: item.appName,
name: item.name,
type: "redis",
id: item.redisId,
createdAt: item.createdAt,
status: item.applicationStatus,
description: item.description,
serverId: item.serverId,
})) || [];
const mysql: Services[] =
data?.mysql.map((item) => ({
appName: item.appName,
name: item.name,
type: "mysql",
id: item.mysqlId,
createdAt: item.createdAt,
status: item.applicationStatus,
description: item.description,
serverId: item.serverId,
})) || [];
const compose: Services[] =
data?.compose.map((item) => ({
appName: item.appName,
name: item.name,
type: "compose",
id: item.composeId,
createdAt: item.createdAt,
status: item.composeStatus,
description: item.description,
serverId: item.serverId,
})) || [];
applications.push(
...mysql,
...redis,
...mongo,
...postgres,
...mariadb,
...compose,
);
applications.sort((a, b) => {
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
});
return applications;
};
const addPermissions = z.object({ const addPermissions = z.object({
accessedProjects: z.array(z.string()).optional(), accessedProjects: z.array(z.string()).optional(),
accessedEnvironments: z.array(z.string()).optional(),
accessedServices: z.array(z.string()).optional(), accessedServices: z.array(z.string()).optional(),
canCreateProjects: z.boolean().optional().default(false), canCreateProjects: z.boolean().optional().default(false),
canCreateServices: z.boolean().optional().default(false), canCreateServices: z.boolean().optional().default(false),
@@ -76,6 +201,7 @@ export const AddUserPermissions = ({ userId }: Props) => {
if (data) { if (data) {
form.reset({ form.reset({
accessedProjects: data.accessedProjects || [], accessedProjects: data.accessedProjects || [],
accessedEnvironments: data.accessedEnvironments || [],
accessedServices: data.accessedServices || [], accessedServices: data.accessedServices || [],
canCreateProjects: data.canCreateProjects, canCreateProjects: data.canCreateProjects,
canCreateServices: data.canCreateServices, canCreateServices: data.canCreateServices,
@@ -99,6 +225,7 @@ export const AddUserPermissions = ({ userId }: Props) => {
canDeleteProjects: data.canDeleteProjects, canDeleteProjects: data.canDeleteProjects,
canAccessToTraefikFiles: data.canAccessToTraefikFiles, canAccessToTraefikFiles: data.canAccessToTraefikFiles,
accessedProjects: data.accessedProjects || [], accessedProjects: data.accessedProjects || [],
accessedEnvironments: data.accessedEnvironments || [],
accessedServices: data.accessedServices || [], accessedServices: data.accessedServices || [],
canAccessToDocker: data.canAccessToDocker, canAccessToDocker: data.canAccessToDocker,
canAccessToAPI: data.canAccessToAPI, canAccessToAPI: data.canAccessToAPI,
@@ -332,89 +459,317 @@ export const AddUserPermissions = ({ userId }: Props) => {
No projects found No projects found
</p> </p>
)} )}
<div className="grid md:grid-cols-2 gap-4"> <div className="grid md:grid-cols-1 gap-4">
{projects?.map((item, index) => { {projects?.map((project, projectIndex) => {
const applications = extractServices(item);
return ( return (
<FormField <FormField
key={`project-${index}`} key={`project-${projectIndex}`}
control={form.control} control={form.control}
name="accessedProjects" name="accessedProjects"
render={({ field }) => { render={({ field }) => {
return ( return (
<FormItem <FormItem
key={item.projectId} key={project.projectId}
className="flex flex-col items-start space-x-4 rounded-lg p-4 border" className="flex flex-col items-start rounded-lg p-4 border"
> >
<div className="flex flex-row gap-4"> {/* Project Header */}
<div className="flex flex-row gap-4 items-center w-full">
<FormControl> <FormControl>
<Checkbox <Checkbox
checked={field.value?.includes( checked={field.value?.includes(
item.projectId, project.projectId,
)} )}
onCheckedChange={(checked) => { onCheckedChange={(checked) => {
return checked if (checked) {
? field.onChange([ // Add the project
...(field.value || []), field.onChange([
item.projectId, ...(field.value || []),
]) project.projectId,
: field.onChange( ]);
field.value?.filter( } else {
(value) => // Remove the project
value !== item.projectId, field.onChange(
), field.value?.filter(
(value) =>
value !== project.projectId,
),
);
// Also remove all environments and services from this project
const currentEnvs =
form.getValues(
"accessedEnvironments",
) || [];
const currentServices =
form.getValues(
"accessedServices",
) || [];
// Get all environment IDs from this project
const projectEnvIds =
project.environments.map(
(env) => env.environmentId,
); );
// Get all service IDs from this project
const projectServiceIds =
project.environments.flatMap(
(env) =>
extractServices(env).map(
(service) => service.id,
),
);
// Remove environments and services from this project
form.setValue(
"accessedEnvironments",
currentEnvs.filter(
(envId) =>
!projectEnvIds.includes(envId),
),
);
form.setValue(
"accessedServices",
currentServices.filter(
(serviceId) =>
!projectServiceIds.includes(
serviceId,
),
),
);
}
}} }}
/> />
</FormControl> </FormControl>
<FormLabel className="text-sm font-medium text-primary"> <FormLabel className="text-base font-semibold text-primary">
{item.name} {project.name}
</FormLabel> </FormLabel>
</div> </div>
{applications.length === 0 && (
<p className="text-sm text-muted-foreground"> {/* Environments */}
No services found <div className="ml-6 w-full space-y-3">
</p> {project.environments.length === 0 && (
)} <p className="text-sm text-muted-foreground">
{applications?.map((item, index) => ( No environments found
<FormField </p>
key={`project-${index}`} )}
control={form.control} {project.environments.map(
name="accessedServices" (environment, envIndex) => {
render={({ field }) => { const services =
extractServices(environment);
return ( return (
<FormItem <div
key={item.id} key={`env-${envIndex}`}
className="flex flex-row items-start space-x-3 space-y-0" className="border-l-2 border-muted pl-4"
> >
<FormControl> {/* Environment Header with Checkbox */}
<Checkbox <FormField
checked={field.value?.includes( key={`env-${envIndex}`}
item.id, control={form.control}
)} name="accessedEnvironments"
onCheckedChange={(checked) => { render={({ field: envField }) => (
return checked <FormItem className="flex flex-row items-center space-x-3 space-y-0 mb-2">
? field.onChange([ <FormControl>
...(field.value || []), <Checkbox
item.id, checked={envField.value?.includes(
]) environment.environmentId,
: field.onChange( )}
field.value?.filter( onCheckedChange={(
(value) => checked,
value !== item.id, ) => {
), if (checked) {
// Add the environment
envField.onChange([
...(envField.value ||
[]),
environment.environmentId,
]);
// Auto-select the project if not already selected
const currentProjects =
form.getValues(
"accessedProjects",
) || [];
if (
!currentProjects.includes(
project.projectId,
)
) {
form.setValue(
"accessedProjects",
[
...currentProjects,
project.projectId,
],
);
}
} else {
// Remove the environment
envField.onChange(
envField.value?.filter(
(value) =>
value !==
environment.environmentId,
),
);
// Also remove all services from this environment
const currentServices =
form.getValues(
"accessedServices",
) || [];
const environmentServiceIds =
services.map(
(service) =>
service.id,
);
form.setValue(
"accessedServices",
currentServices.filter(
(serviceId) =>
!environmentServiceIds.includes(
serviceId,
),
),
);
}
}}
/>
</FormControl>
<div className="flex items-center gap-2">
<div className="w-2 h-2 bg-blue-500 rounded-full" />
<FormLabel className="text-sm font-medium text-foreground cursor-pointer">
{environment.name}
</FormLabel>
<span className="text-xs text-muted-foreground">
({services.length} services)
</span>
</div>
</FormItem>
)}
/>
{/* Services */}
<div className="ml-4 space-y-2">
{services.length === 0 && (
<p className="text-xs text-muted-foreground">
No services found
</p>
)}
{services.map(
(service, serviceIndex) => (
<FormField
key={`service-${serviceIndex}`}
control={form.control}
name="accessedServices"
render={({
field: serviceField,
}) => {
return (
<FormItem
key={service.id}
className="flex flex-row items-center space-x-3 space-y-0"
>
<FormControl>
<Checkbox
checked={serviceField.value?.includes(
service.id,
)}
onCheckedChange={(
checked,
) => {
if (checked) {
// Add the service
serviceField.onChange(
[
...(serviceField.value ||
[]),
service.id,
],
);
// Auto-select the environment if not already selected
const currentEnvs =
form.getValues(
"accessedEnvironments",
) || [];
if (
!currentEnvs.includes(
environment.environmentId,
)
) {
form.setValue(
"accessedEnvironments",
[
...currentEnvs,
environment.environmentId,
],
);
}
// Auto-select the project if not already selected
const currentProjects =
form.getValues(
"accessedProjects",
) || [];
if (
!currentProjects.includes(
project.projectId,
)
) {
form.setValue(
"accessedProjects",
[
...currentProjects,
project.projectId,
],
);
}
} else {
// Remove the service
serviceField.onChange(
serviceField.value?.filter(
(value) =>
value !==
service.id,
),
);
}
}}
/>
</FormControl>
<div className="flex items-center gap-2">
<div
className={`w-1.5 h-1.5 rounded-full ${
service.type ===
"application"
? "bg-green-500"
: service.type ===
"compose"
? "bg-purple-500"
: "bg-orange-500"
}`}
/>
<FormLabel className="text-sm text-muted-foreground cursor-pointer">
{service.name}
</FormLabel>
<span className="text-xs text-muted-foreground/70 capitalize">
({service.type})
</span>
</div>
</FormItem>
); );
}} }}
/> />
</FormControl> ),
<FormLabel className="text-sm text-muted-foreground"> )}
{item.name} </div>
</FormLabel> </div>
</FormItem>
); );
}} },
/> )}
))} </div>
</FormItem> </FormItem>
); );
}} }}
@@ -13,7 +13,7 @@ import { SidebarTrigger } from "@/components/ui/sidebar";
interface Props { interface Props {
list: { list: {
name: string; name: string;
href: string; href?: string;
}[]; }[];
} }
@@ -29,11 +29,11 @@ export const BreadcrumbSidebar = ({ list }: Props) => {
{list.map((item, index) => ( {list.map((item, index) => (
<Fragment key={item.name}> <Fragment key={item.name}>
<BreadcrumbItem className="block"> <BreadcrumbItem className="block">
<BreadcrumbLink href={item.href} asChild={!!item.href}> <BreadcrumbLink href={item?.href} asChild={!!item?.href}>
{item.href ? ( {item.href ? (
<Link href={item.href}>{item.name}</Link> <Link href={item?.href}>{item?.name}</Link>
) : ( ) : (
item.name item?.name
)} )}
</BreadcrumbLink> </BreadcrumbLink>
</BreadcrumbItem> </BreadcrumbItem>
+147
View File
@@ -0,0 +1,147 @@
CREATE TABLE "environment" (
"environmentId" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"description" text,
"createdAt" text NOT NULL,
"projectId" text NOT NULL
);
ALTER TABLE "environment" ADD CONSTRAINT "environment_projectId_project_projectId_fk" FOREIGN KEY ("projectId") REFERENCES "public"."project"("projectId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
-- Insertar un ambiente "production" para cada proyecto existente
INSERT INTO "environment" ("environmentId", "name", "description", "createdAt", "projectId")
SELECT
-- Generar un ID único para cada ambiente usando el projectId como base
'env_prod_' || "projectId" || '_' || EXTRACT(EPOCH FROM NOW())::text,
'production',
'Production environment',
NOW()::text,
"projectId"
FROM "project"
WHERE "projectId" NOT IN (
SELECT DISTINCT "projectId"
FROM "environment"
WHERE "name" = 'production'
);
ALTER TABLE "application" ADD COLUMN "environmentId" text;--> statement-breakpoint
ALTER TABLE "postgres" ADD COLUMN "environmentId" text;--> statement-breakpoint
ALTER TABLE "mariadb" ADD COLUMN "environmentId" text;--> statement-breakpoint
ALTER TABLE "mongo" ADD COLUMN "environmentId" text;--> statement-breakpoint
ALTER TABLE "mysql" ADD COLUMN "environmentId" text;--> statement-breakpoint
ALTER TABLE "redis" ADD COLUMN "environmentId" text;--> statement-breakpoint
ALTER TABLE "compose" ADD COLUMN "environmentId" text;--> statement-breakpoint
-- Step 3: Update all services to point to their project's production environment
-- Update applications
UPDATE "application"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "application"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
-- Update compose
UPDATE "compose"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "compose"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
-- Update mariadb
UPDATE "mariadb"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "mariadb"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
-- Update mongo
UPDATE "mongo"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "mongo"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
-- Update mysql
UPDATE "mysql"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "mysql"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
-- Update postgres
UPDATE "postgres"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "postgres"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
-- Update redis
UPDATE "redis"
SET "environmentId" = (
SELECT e."environmentId"
FROM "environment" e
WHERE e."projectId" = "redis"."projectId"
AND e."name" = 'production'
LIMIT 1
);--> statement-breakpoint
--> statement-breakpoint
ALTER TABLE "application" DROP CONSTRAINT "application_projectId_project_projectId_fk";
--> statement-breakpoint
ALTER TABLE "postgres" DROP CONSTRAINT "postgres_projectId_project_projectId_fk";
--> statement-breakpoint
ALTER TABLE "mariadb" DROP CONSTRAINT "mariadb_projectId_project_projectId_fk";
--> statement-breakpoint
ALTER TABLE "mongo" DROP CONSTRAINT "mongo_projectId_project_projectId_fk";
--> statement-breakpoint
ALTER TABLE "mysql" DROP CONSTRAINT "mysql_projectId_project_projectId_fk";
--> statement-breakpoint
ALTER TABLE "redis" DROP CONSTRAINT "redis_projectId_project_projectId_fk";
--> statement-breakpoint
ALTER TABLE "compose" DROP CONSTRAINT "compose_projectId_project_projectId_fk";
--> statement-breakpoint
-- Step 4: Make environmentId columns NOT NULL
ALTER TABLE "application" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "compose" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "mariadb" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "mongo" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "mysql" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "postgres" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "redis" ALTER COLUMN "environmentId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "application" ADD CONSTRAINT "application_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "postgres" ADD CONSTRAINT "postgres_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "mariadb" ADD CONSTRAINT "mariadb_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "mongo" ADD CONSTRAINT "mongo_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "mysql" ADD CONSTRAINT "mysql_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "redis" ADD CONSTRAINT "redis_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "compose" ADD CONSTRAINT "compose_environmentId_environment_environmentId_fk" FOREIGN KEY ("environmentId") REFERENCES "public"."environment"("environmentId") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "application" DROP COLUMN "projectId";--> statement-breakpoint
ALTER TABLE "postgres" DROP COLUMN "projectId";--> statement-breakpoint
ALTER TABLE "mariadb" DROP COLUMN "projectId";--> statement-breakpoint
ALTER TABLE "mongo" DROP COLUMN "projectId";--> statement-breakpoint
ALTER TABLE "mysql" DROP COLUMN "projectId";--> statement-breakpoint
ALTER TABLE "redis" DROP COLUMN "projectId";--> statement-breakpoint
ALTER TABLE "compose" DROP COLUMN "projectId";
@@ -0,0 +1 @@
ALTER TABLE "environment" ADD COLUMN "env" text DEFAULT '' NOT NULL;
@@ -0,0 +1 @@
ALTER TABLE "member" ADD COLUMN "accessedEnvironments" text[] DEFAULT ARRAY[]::text[] NOT NULL;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+21
View File
@@ -750,6 +750,27 @@
"when": 1754912062243, "when": 1754912062243,
"tag": "0106_purple_maggott", "tag": "0106_purple_maggott",
"breakpoints": true "breakpoints": true
},
{
"idx": 107,
"version": "7",
"when": 1756793713380,
"tag": "0107_loud_kang",
"breakpoints": true
},
{
"idx": 108,
"version": "7",
"when": 1756955718127,
"tag": "0108_lazy_next_avengers",
"breakpoints": true
},
{
"idx": 109,
"version": "7",
"when": 1757052053574,
"tag": "0109_remarkable_sauron",
"breakpoints": true
} }
] ]
} }
@@ -20,7 +20,11 @@ export default async function handler(
const application = await db.query.applications.findFirst({ const application = await db.query.applications.findFirst({
where: eq(applications.refreshToken, refreshToken as string), where: eq(applications.refreshToken, refreshToken as string),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
bitbucket: true, bitbucket: true,
}, },
}); });
@@ -27,7 +27,11 @@ export default async function handler(
const composeResult = await db.query.compose.findFirst({ const composeResult = await db.query.compose.findFirst({
where: eq(compose.refreshToken, refreshToken as string), where: eq(compose.refreshToken, refreshToken as string),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
bitbucket: true, bitbucket: true,
}, },
}); });
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -71,7 +71,7 @@ const Service = (
const [_toggleMonitoring, _setToggleMonitoring] = useState(false); const [_toggleMonitoring, _setToggleMonitoring] = useState(false);
const { applicationId, activeTab } = props; const { applicationId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setTab] = useState<TabState>(activeTab); const [tab, setTab] = useState<TabState>(activeTab);
useEffect(() => { useEffect(() => {
@@ -97,18 +97,20 @@ const Service = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment.project.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/application/${applicationId}`,
}, },
]} ]}
/> />
<Head> <Head>
<title> <title>
Application: {data?.name} - {data?.project.name} | Dokploy Application: {data?.name} - {data?.environment.project.name} | Dokploy
</title> </title>
</Head> </Head>
<div className="w-full"> <div className="w-full">
@@ -215,7 +217,7 @@ const Service = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setTab(e as TabState); setTab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/application/${applicationId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/application/${applicationId}?tab=${e}`;
router.push(newPath); router.push(newPath);
}} }}
> >
@@ -379,6 +381,7 @@ export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ ctx: GetServerSidePropsContext<{
applicationId: string; applicationId: string;
activeTab: TabState; activeTab: TabState;
environmentId: string;
}>, }>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
@@ -420,6 +423,7 @@ export async function getServerSideProps(
trpcState: helpers.dehydrate(), trpcState: helpers.dehydrate(),
applicationId: params?.applicationId, applicationId: params?.applicationId,
activeTab: (activeTab || "general") as TabState, activeTab: (activeTab || "general") as TabState,
environmentId: params?.environmentId,
}, },
}; };
} catch { } catch {
@@ -67,7 +67,7 @@ const Service = (
const [_toggleMonitoring, _setToggleMonitoring] = useState(false); const [_toggleMonitoring, _setToggleMonitoring] = useState(false);
const { composeId, activeTab } = props; const { composeId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setTab] = useState<TabState>(activeTab); const [tab, setTab] = useState<TabState>(activeTab);
useEffect(() => { useEffect(() => {
@@ -88,18 +88,20 @@ const Service = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment?.project?.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/compose/${composeId}`,
}, },
]} ]}
/> />
<Head> <Head>
<title> <title>
Compose: {data?.name} - {data?.project.name} | Dokploy Compose: {data?.name} - {data?.environment?.project?.name} | Dokploy
</title> </title>
</Head> </Head>
<div className="w-full"> <div className="w-full">
@@ -208,7 +210,7 @@ const Service = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setTab(e as TabState); setTab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/compose/${composeId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/compose/${composeId}?tab=${e}`;
router.push(newPath); router.push(newPath);
}} }}
> >
@@ -375,6 +377,7 @@ export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ ctx: GetServerSidePropsContext<{
composeId: string; composeId: string;
activeTab: TabState; activeTab: TabState;
environmentId: string;
}>, }>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
@@ -414,6 +417,7 @@ export async function getServerSideProps(
trpcState: helpers.dehydrate(), trpcState: helpers.dehydrate(),
composeId: params?.composeId, composeId: params?.composeId,
activeTab: (activeTab || "general") as TabState, activeTab: (activeTab || "general") as TabState,
environmentId: params?.environmentId,
}, },
}; };
} catch { } catch {
@@ -55,7 +55,7 @@ const Mariadb = (
const { mariadbId, activeTab } = props; const { mariadbId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setSab] = useState<TabState>(activeTab); const [tab, setSab] = useState<TabState>(activeTab);
const { data } = api.mariadb.one.useQuery({ mariadbId }); const { data } = api.mariadb.one.useQuery({ mariadbId });
const { data: auth } = api.user.get.useQuery(); const { data: auth } = api.user.get.useQuery();
@@ -69,19 +69,22 @@ const Mariadb = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment?.project?.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/mariadb/${mariadbId}`,
}, },
]} ]}
/> />
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
<Head> <Head>
<title> <title>
Database: {data?.name} - {data?.project.name} | Dokploy Database: {data?.name} - {data?.environment?.project?.name} |
Dokploy
</title> </title>
</Head> </Head>
<Card className="h-full bg-sidebar p-2.5 rounded-xl w-full"> <Card className="h-full bg-sidebar p-2.5 rounded-xl w-full">
@@ -179,7 +182,7 @@ const Mariadb = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setSab(e as TabState); setSab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/mariadb/${mariadbId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/mariadb/${mariadbId}?tab=${e}`;
router.push(newPath, undefined, { shallow: true }); router.push(newPath, undefined, { shallow: true });
}} }}
@@ -300,7 +303,11 @@ Mariadb.getLayout = (page: ReactElement) => {
}; };
export async function getServerSideProps( export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ mariadbId: string; activeTab: TabState }>, ctx: GetServerSidePropsContext<{
mariadbId: string;
activeTab: TabState;
environmentId: string;
}>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
const activeTab = query.tab; const activeTab = query.tab;
@@ -338,6 +345,7 @@ export async function getServerSideProps(
trpcState: helpers.dehydrate(), trpcState: helpers.dehydrate(),
mariadbId: params?.mariadbId, mariadbId: params?.mariadbId,
activeTab: (activeTab || "general") as TabState, activeTab: (activeTab || "general") as TabState,
environmentId: params?.environmentId,
}, },
}; };
} catch { } catch {
@@ -54,7 +54,7 @@ const Mongo = (
const [_toggleMonitoring, _setToggleMonitoring] = useState(false); const [_toggleMonitoring, _setToggleMonitoring] = useState(false);
const { mongoId, activeTab } = props; const { mongoId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setSab] = useState<TabState>(activeTab); const [tab, setSab] = useState<TabState>(activeTab);
const { data } = api.mongo.one.useQuery({ mongoId }); const { data } = api.mongo.one.useQuery({ mongoId });
@@ -69,18 +69,20 @@ const Mongo = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment?.project?.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/mongo/${mongoId}`,
}, },
]} ]}
/> />
<Head> <Head>
<title> <title>
Database: {data?.name} - {data?.project.name} | Dokploy Database: {data?.name} - {data?.environment?.project?.name} | Dokploy
</title> </title>
</Head> </Head>
<div className="w-full"> <div className="w-full">
@@ -180,7 +182,7 @@ const Mongo = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setSab(e as TabState); setSab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/mongo/${mongoId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/mongo/${mongoId}?tab=${e}`;
router.push(newPath, undefined, { shallow: true }); router.push(newPath, undefined, { shallow: true });
}} }}
@@ -302,7 +304,11 @@ Mongo.getLayout = (page: ReactElement) => {
}; };
export async function getServerSideProps( export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ mongoId: string; activeTab: TabState }>, ctx: GetServerSidePropsContext<{
mongoId: string;
activeTab: TabState;
environmentId: string;
}>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
const activeTab = query.tab; const activeTab = query.tab;
@@ -340,6 +346,7 @@ export async function getServerSideProps(
trpcState: helpers.dehydrate(), trpcState: helpers.dehydrate(),
mongoId: params?.mongoId, mongoId: params?.mongoId,
activeTab: (activeTab || "general") as TabState, activeTab: (activeTab || "general") as TabState,
environmentId: params?.environmentId,
}, },
}; };
} catch { } catch {
@@ -54,7 +54,7 @@ const MySql = (
const [_toggleMonitoring, _setToggleMonitoring] = useState(false); const [_toggleMonitoring, _setToggleMonitoring] = useState(false);
const { mysqlId, activeTab } = props; const { mysqlId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setSab] = useState<TabState>(activeTab); const [tab, setSab] = useState<TabState>(activeTab);
const { data } = api.mysql.one.useQuery({ mysqlId }); const { data } = api.mysql.one.useQuery({ mysqlId });
const { data: auth } = api.user.get.useQuery(); const { data: auth } = api.user.get.useQuery();
@@ -68,19 +68,22 @@ const MySql = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment?.project?.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/mysql/${mysqlId}`,
}, },
]} ]}
/> />
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
<Head> <Head>
<title> <title>
Database: {data?.name} - {data?.project.name} | Dokploy Database: {data?.name} - {data?.environment?.project?.name} |
Dokploy
</title> </title>
</Head> </Head>
<div className="w-full"> <div className="w-full">
@@ -180,7 +183,7 @@ const MySql = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setSab(e as TabState); setSab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/mysql/${mysqlId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/mysql/${mysqlId}?tab=${e}`;
router.push(newPath, undefined, { shallow: true }); router.push(newPath, undefined, { shallow: true });
}} }}
@@ -286,7 +289,11 @@ MySql.getLayout = (page: ReactElement) => {
}; };
export async function getServerSideProps( export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ mysqlId: string; activeTab: TabState }>, ctx: GetServerSidePropsContext<{
mysqlId: string;
activeTab: TabState;
environmentId: string;
}>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
const activeTab = query.tab; const activeTab = query.tab;
@@ -54,7 +54,7 @@ const Postgresql = (
const [_toggleMonitoring, _setToggleMonitoring] = useState(false); const [_toggleMonitoring, _setToggleMonitoring] = useState(false);
const { postgresId, activeTab } = props; const { postgresId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setSab] = useState<TabState>(activeTab); const [tab, setSab] = useState<TabState>(activeTab);
const { data } = api.postgres.one.useQuery({ postgresId }); const { data } = api.postgres.one.useQuery({ postgresId });
const { data: auth } = api.user.get.useQuery(); const { data: auth } = api.user.get.useQuery();
@@ -68,18 +68,20 @@ const Postgresql = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment?.project?.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/postgres/${postgresId}`,
}, },
]} ]}
/> />
<Head> <Head>
<title> <title>
Database: {data?.name} - {data?.project.name} | Dokploy Database: {data?.name} - {data?.environment?.project?.name} | Dokploy
</title> </title>
</Head> </Head>
<div className="w-full"> <div className="w-full">
@@ -179,9 +181,11 @@ const Postgresql = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setSab(e as TabState); setSab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/postgres/${postgresId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/postgres/${postgresId}?tab=${e}`;
router.push(newPath, undefined, { shallow: true }); router.push(newPath, undefined, {
shallow: true,
});
}} }}
> >
<div className="flex flex-row items-center justify-between w-full gap-4 overflow-x-scroll"> <div className="flex flex-row items-center justify-between w-full gap-4 overflow-x-scroll">
@@ -228,7 +232,11 @@ const Postgresql = (
{data?.serverId && isCloud ? ( {data?.serverId && isCloud ? (
<ContainerPaidMonitoring <ContainerPaidMonitoring
appName={data?.appName || ""} appName={data?.appName || ""}
baseUrl={`${data?.serverId ? `http://${data?.server?.ipAddress}:${data?.server?.metricsConfig?.server?.port}` : "http://localhost:4500"}`} baseUrl={`${
data?.serverId
? `http://${data?.server?.ipAddress}:${data?.server?.metricsConfig?.server?.port}`
: "http://localhost:4500"
}`}
token={ token={
data?.server?.metricsConfig?.server?.token || "" data?.server?.metricsConfig?.server?.token || ""
} }
@@ -284,7 +292,11 @@ Postgresql.getLayout = (page: ReactElement) => {
}; };
export async function getServerSideProps( export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ postgresId: string; activeTab: TabState }>, ctx: GetServerSidePropsContext<{
postgresId: string;
activeTab: TabState;
environmentId: string;
}>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
const activeTab = query.tab; const activeTab = query.tab;
@@ -53,7 +53,7 @@ const Redis = (
const [_toggleMonitoring, _setToggleMonitoring] = useState(false); const [_toggleMonitoring, _setToggleMonitoring] = useState(false);
const { redisId, activeTab } = props; const { redisId, activeTab } = props;
const router = useRouter(); const router = useRouter();
const { projectId } = router.query; const { projectId, environmentId } = router.query;
const [tab, setSab] = useState<TabState>(activeTab); const [tab, setSab] = useState<TabState>(activeTab);
const { data } = api.redis.one.useQuery({ redisId }); const { data } = api.redis.one.useQuery({ redisId });
@@ -68,18 +68,20 @@ const Redis = (
list={[ list={[
{ name: "Projects", href: "/dashboard/projects" }, { name: "Projects", href: "/dashboard/projects" },
{ {
name: data?.project?.name || "", name: data?.environment?.project?.name || "",
href: `/dashboard/project/${projectId}`, },
{
name: data?.environment?.name || "",
href: `/dashboard/project/${projectId}/environment/${environmentId}`,
}, },
{ {
name: data?.name || "", name: data?.name || "",
href: `/dashboard/project/${projectId}/services/redis/${redisId}`,
}, },
]} ]}
/> />
<Head> <Head>
<title> <title>
Database: {data?.name} - {data?.project.name} | Dokploy Database: {data?.name} - {data?.environment?.project?.name} | Dokploy
</title> </title>
</Head> </Head>
<div className="w-full"> <div className="w-full">
@@ -179,7 +181,7 @@ const Redis = (
className="w-full" className="w-full"
onValueChange={(e) => { onValueChange={(e) => {
setSab(e as TabState); setSab(e as TabState);
const newPath = `/dashboard/project/${projectId}/services/redis/${redisId}?tab=${e}`; const newPath = `/dashboard/project/${projectId}/environment/${environmentId}/services/redis/${redisId}?tab=${e}`;
router.push(newPath, undefined, { shallow: true }); router.push(newPath, undefined, { shallow: true });
}} }}
@@ -291,7 +293,11 @@ Redis.getLayout = (page: ReactElement) => {
}; };
export async function getServerSideProps( export async function getServerSideProps(
ctx: GetServerSidePropsContext<{ redisId: string; activeTab: TabState }>, ctx: GetServerSidePropsContext<{
redisId: string;
activeTab: TabState;
environmentId: string;
}>,
) { ) {
const { query, params, req, res } = ctx; const { query, params, req, res } = ctx;
const activeTab = query.tab; const activeTab = query.tab;
+2
View File
@@ -11,6 +11,7 @@ import { deploymentRouter } from "./routers/deployment";
import { destinationRouter } from "./routers/destination"; import { destinationRouter } from "./routers/destination";
import { dockerRouter } from "./routers/docker"; import { dockerRouter } from "./routers/docker";
import { domainRouter } from "./routers/domain"; import { domainRouter } from "./routers/domain";
import { environmentRouter } from "./routers/environment";
import { gitProviderRouter } from "./routers/git-provider"; import { gitProviderRouter } from "./routers/git-provider";
import { giteaRouter } from "./routers/gitea"; import { giteaRouter } from "./routers/gitea";
import { githubRouter } from "./routers/github"; import { githubRouter } from "./routers/github";
@@ -84,6 +85,7 @@ export const appRouter = createTRPCRouter({
schedule: scheduleRouter, schedule: scheduleRouter,
rollback: rollbackRouter, rollback: rollbackRouter,
volumeBackups: volumeBackupsRouter, volumeBackups: volumeBackupsRouter,
environment: environmentRouter,
}); });
// export type definition of API // export type definition of API
+9 -4
View File
@@ -4,7 +4,11 @@ import {
apiUpdateAi, apiUpdateAi,
deploySuggestionSchema, deploySuggestionSchema,
} from "@dokploy/server/db/schema/ai"; } from "@dokploy/server/db/schema/ai";
import { createDomain, createMount } from "@dokploy/server/index"; import {
createDomain,
createMount,
findEnvironmentById,
} from "@dokploy/server/index";
import { import {
deleteAiSettings, deleteAiSettings,
getAiSettingById, getAiSettingById,
@@ -177,10 +181,12 @@ export const aiRouter = createTRPCRouter({
deploy: protectedProcedure deploy: protectedProcedure
.input(deploySuggestionSchema) .input(deploySuggestionSchema)
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
input.projectId, environment.projectId,
"create", "create",
); );
} }
@@ -192,8 +198,6 @@ export const aiRouter = createTRPCRouter({
}); });
} }
const project = await findProjectById(input.projectId);
const projectName = slugify(`${project.name} ${input.id}`); const projectName = slugify(`${project.name} ${input.id}`);
const compose = await createComposeByTemplate({ const compose = await createComposeByTemplate({
@@ -205,6 +209,7 @@ export const aiRouter = createTRPCRouter({
sourceType: "raw", sourceType: "raw",
appName: `${projectName}-${generatePassword(6)}`, appName: `${projectName}-${generatePassword(6)}`,
isolatedDeployment: true, isolatedDeployment: true,
environmentId: input.environmentId,
}); });
if (input.domains && input.domains?.length > 0) { if (input.domains && input.domains?.length > 0) {
+72 -32
View File
@@ -4,6 +4,7 @@ import {
createApplication, createApplication,
deleteAllMiddlewares, deleteAllMiddlewares,
findApplicationById, findApplicationById,
findEnvironmentById,
findGitProviderById, findGitProviderById,
findProjectById, findProjectById,
getApplicationStats, getApplicationStats,
@@ -63,10 +64,14 @@ export const applicationRouter = createTRPCRouter({
.input(apiCreateApplication) .input(apiCreateApplication)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -79,13 +84,13 @@ export const applicationRouter = createTRPCRouter({
}); });
} }
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
}); });
} }
const newApplication = await createApplication(input); const newApplication = await createApplication(input);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
@@ -97,6 +102,7 @@ export const applicationRouter = createTRPCRouter({
} }
return newApplication; return newApplication;
} catch (error: unknown) { } catch (error: unknown) {
console.log("error", error);
if (error instanceof TRPCError) { if (error instanceof TRPCError) {
throw error; throw error;
} }
@@ -120,7 +126,8 @@ export const applicationRouter = createTRPCRouter({
} }
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -175,7 +182,7 @@ export const applicationRouter = createTRPCRouter({
try { try {
if ( if (
application.project.organizationId !== application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -212,7 +219,8 @@ export const applicationRouter = createTRPCRouter({
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -247,14 +255,17 @@ export const applicationRouter = createTRPCRouter({
} catch (_) {} } catch (_) {}
} }
return result[0]; return application;
}), }),
stop: protectedProcedure stop: protectedProcedure
.input(apiFindOneApplication) .input(apiFindOneApplication)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const service = await findApplicationById(input.applicationId); const service = await findApplicationById(input.applicationId);
if (service.project.organizationId !== ctx.session.activeOrganizationId) { if (
service.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to stop this application", message: "You are not authorized to stop this application",
@@ -274,7 +285,10 @@ export const applicationRouter = createTRPCRouter({
.input(apiFindOneApplication) .input(apiFindOneApplication)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const service = await findApplicationById(input.applicationId); const service = await findApplicationById(input.applicationId);
if (service.project.organizationId !== ctx.session.activeOrganizationId) { if (
service.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to start this application", message: "You are not authorized to start this application",
@@ -296,7 +310,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -331,7 +346,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -349,7 +365,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -374,7 +391,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -401,7 +419,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -429,7 +448,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -455,7 +475,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -481,7 +502,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -504,7 +526,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -529,7 +552,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -590,7 +614,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -604,7 +629,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -630,7 +656,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -647,7 +674,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -683,7 +711,8 @@ export const applicationRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -698,7 +727,8 @@ export const applicationRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -734,7 +764,10 @@ export const applicationRouter = createTRPCRouter({
const app = await findApplicationById(input.applicationId as string); const app = await findApplicationById(input.applicationId as string);
if (app.project.organizationId !== ctx.session.activeOrganizationId) { if (
app.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this application", message: "You are not authorized to deploy this application",
@@ -777,7 +810,8 @@ export const applicationRouter = createTRPCRouter({
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -813,13 +847,14 @@ export const applicationRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
applicationId: z.string(), applicationId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -827,11 +862,16 @@ export const applicationRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -839,7 +879,7 @@ export const applicationRouter = createTRPCRouter({
const updatedApplication = await db const updatedApplication = await db
.update(applications) .update(applications)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(applications.applicationId, input.applicationId)) .where(eq(applications.applicationId, input.applicationId))
.returning() .returning()
+96 -33
View File
@@ -12,6 +12,7 @@ import {
deleteMount, deleteMount,
findComposeById, findComposeById,
findDomainsByComposeId, findDomainsByComposeId,
findEnvironmentById,
findGitProviderById, findGitProviderById,
findProjectById, findProjectById,
findServerById, findServerById,
@@ -64,10 +65,14 @@ export const composeRouter = createTRPCRouter({
.input(apiCreateCompose) .input(apiCreateCompose)
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -79,14 +84,15 @@ export const composeRouter = createTRPCRouter({
message: "You need to use a server to create a compose", message: "You need to use a server to create a compose",
}); });
} }
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
}); });
} }
const newService = await createCompose(input); const newService = await createCompose({
...input,
});
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewService( await addNewService(
@@ -115,7 +121,10 @@ export const composeRouter = createTRPCRouter({
} }
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this compose", message: "You are not authorized to access this compose",
@@ -166,7 +175,10 @@ export const composeRouter = createTRPCRouter({
.input(apiUpdateCompose) .input(apiUpdateCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to update this compose", message: "You are not authorized to update this compose",
@@ -188,7 +200,7 @@ export const composeRouter = createTRPCRouter({
const composeResult = await findComposeById(input.composeId); const composeResult = await findComposeById(input.composeId);
if ( if (
composeResult.project.organizationId !== composeResult.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -196,7 +208,6 @@ export const composeRouter = createTRPCRouter({
message: "You are not authorized to delete this compose", message: "You are not authorized to delete this compose",
}); });
} }
4;
const result = await db const result = await db
.delete(composeTable) .delete(composeTable)
@@ -215,13 +226,16 @@ export const composeRouter = createTRPCRouter({
} catch (_) {} } catch (_) {}
} }
return result[0]; return composeResult;
}), }),
cleanQueues: protectedProcedure cleanQueues: protectedProcedure
.input(apiFindCompose) .input(apiFindCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to clean this compose", message: "You are not authorized to clean this compose",
@@ -234,7 +248,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFetchServices) .input(apiFetchServices)
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to load this compose", message: "You are not authorized to load this compose",
@@ -251,7 +268,10 @@ export const composeRouter = createTRPCRouter({
) )
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to load this compose", message: "You are not authorized to load this compose",
@@ -270,7 +290,8 @@ export const composeRouter = createTRPCRouter({
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -296,7 +317,10 @@ export const composeRouter = createTRPCRouter({
.input(apiRandomizeCompose) .input(apiRandomizeCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to randomize this compose", message: "You are not authorized to randomize this compose",
@@ -308,7 +332,10 @@ export const composeRouter = createTRPCRouter({
.input(apiRandomizeCompose) .input(apiRandomizeCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to randomize this compose", message: "You are not authorized to randomize this compose",
@@ -323,7 +350,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to get this compose", message: "You are not authorized to get this compose",
@@ -341,7 +371,10 @@ export const composeRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this compose", message: "You are not authorized to deploy this compose",
@@ -374,7 +407,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to redeploy this compose", message: "You are not authorized to redeploy this compose",
@@ -406,7 +442,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to stop this compose", message: "You are not authorized to stop this compose",
@@ -420,7 +459,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to stop this compose", message: "You are not authorized to stop this compose",
@@ -435,7 +477,10 @@ export const composeRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to get this compose", message: "You are not authorized to get this compose",
@@ -448,7 +493,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to refresh this compose", message: "You are not authorized to refresh this compose",
@@ -462,17 +510,19 @@ export const composeRouter = createTRPCRouter({
deployTemplate: protectedProcedure deployTemplate: protectedProcedure
.input( .input(
z.object({ z.object({
projectId: z.string(), environmentId: z.string(),
serverId: z.string().optional(), serverId: z.string().optional(),
id: z.string(), id: z.string(),
baseUrl: z.string().optional(), baseUrl: z.string().optional(),
}), }),
) )
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
const environment = await findEnvironmentById(input.environmentId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, environment.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -490,7 +540,7 @@ export const composeRouter = createTRPCRouter({
const admin = await findUserById(ctx.user.ownerId); const admin = await findUserById(ctx.user.ownerId);
let serverIp = admin.serverIp || "127.0.0.1"; let serverIp = admin.serverIp || "127.0.0.1";
const project = await findProjectById(input.projectId); const project = await findProjectById(environment.projectId);
if (input.serverId) { if (input.serverId) {
const server = await findServerById(input.serverId); const server = await findServerById(input.serverId);
@@ -591,7 +641,10 @@ export const composeRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to disconnect this git provider", message: "You are not authorized to disconnect this git provider",
@@ -647,30 +700,38 @@ export const composeRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
composeId: z.string(), composeId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move this compose", message: "You are not authorized to move this compose",
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
const updatedCompose = await db const updatedCompose = await db
.update(composeTable) .update(composeTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(composeTable.composeId, input.composeId)) .where(eq(composeTable.composeId, input.composeId))
.returning() .returning()
@@ -698,7 +759,8 @@ export const composeRouter = createTRPCRouter({
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -769,7 +831,8 @@ export const composeRouter = createTRPCRouter({
); );
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -29,7 +29,8 @@ export const deploymentRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -43,7 +44,10 @@ export const deploymentRouter = createTRPCRouter({
.input(apiFindAllByCompose) .input(apiFindAllByCompose)
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this compose", message: "You are not authorized to access this compose",
+24 -12
View File
@@ -34,7 +34,8 @@ export const domainRouter = createTRPCRouter({
if (input.domainType === "compose" && input.composeId) { if (input.domainType === "compose" && input.composeId) {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -44,7 +45,7 @@ export const domainRouter = createTRPCRouter({
} else if (input.domainType === "application" && input.applicationId) { } else if (input.domainType === "application" && input.applicationId) {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -70,7 +71,8 @@ export const domainRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -83,7 +85,10 @@ export const domainRouter = createTRPCRouter({
.input(apiFindCompose) .input(apiFindCompose)
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId); const compose = await findComposeById(input.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this compose", message: "You are not authorized to access this compose",
@@ -122,7 +127,8 @@ export const domainRouter = createTRPCRouter({
if (currentDomain.applicationId) { if (currentDomain.applicationId) {
const newApp = await findApplicationById(currentDomain.applicationId); const newApp = await findApplicationById(currentDomain.applicationId);
if ( if (
newApp.project.organizationId !== ctx.session.activeOrganizationId newApp.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -132,7 +138,8 @@ export const domainRouter = createTRPCRouter({
} else if (currentDomain.composeId) { } else if (currentDomain.composeId) {
const newCompose = await findComposeById(currentDomain.composeId); const newCompose = await findComposeById(currentDomain.composeId);
if ( if (
newCompose.project.organizationId !== ctx.session.activeOrganizationId newCompose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -144,8 +151,8 @@ export const domainRouter = createTRPCRouter({
currentDomain.previewDeploymentId, currentDomain.previewDeploymentId,
); );
if ( if (
newPreviewDeployment.application.project.organizationId !== newPreviewDeployment.application.environment.project
ctx.session.activeOrganizationId .organizationId !== ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -175,7 +182,8 @@ export const domainRouter = createTRPCRouter({
if (domain.applicationId) { if (domain.applicationId) {
const application = await findApplicationById(domain.applicationId); const application = await findApplicationById(domain.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -184,7 +192,10 @@ export const domainRouter = createTRPCRouter({
} }
} else if (domain.composeId) { } else if (domain.composeId) {
const compose = await findComposeById(domain.composeId); const compose = await findComposeById(domain.composeId);
if (compose.project.organizationId !== ctx.session.activeOrganizationId) { if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this compose", message: "You are not authorized to access this compose",
@@ -200,7 +211,7 @@ export const domainRouter = createTRPCRouter({
if (domain.applicationId) { if (domain.applicationId) {
const application = await findApplicationById(domain.applicationId); const application = await findApplicationById(domain.applicationId);
if ( if (
application.project.organizationId !== application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -211,7 +222,8 @@ export const domainRouter = createTRPCRouter({
} else if (domain.composeId) { } else if (domain.composeId) {
const compose = await findComposeById(domain.composeId); const compose = await findComposeById(domain.composeId);
if ( if (
compose.project.organizationId !== ctx.session.activeOrganizationId compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -0,0 +1,328 @@
import {
addNewEnvironment,
checkEnvironmentAccess,
createEnvironment,
deleteEnvironment,
duplicateEnvironment,
findEnvironmentById,
findEnvironmentsByProjectId,
findMemberById,
updateEnvironmentById,
} from "@dokploy/server";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import {
apiCreateEnvironment,
apiDuplicateEnvironment,
apiFindOneEnvironment,
apiRemoveEnvironment,
apiUpdateEnvironment,
} from "@/server/db/schema";
// Helper function to filter services within an environment based on user permissions
const filterEnvironmentServices = (
environment: any,
accessedServices: string[],
) => ({
...environment,
applications: environment.applications.filter((app: any) =>
accessedServices.includes(app.applicationId),
),
mariadb: environment.mariadb.filter((db: any) =>
accessedServices.includes(db.mariadbId),
),
mongo: environment.mongo.filter((db: any) =>
accessedServices.includes(db.mongoId),
),
mysql: environment.mysql.filter((db: any) =>
accessedServices.includes(db.mysqlId),
),
postgres: environment.postgres.filter((db: any) =>
accessedServices.includes(db.postgresId),
),
redis: environment.redis.filter((db: any) =>
accessedServices.includes(db.redisId),
),
compose: environment.compose.filter((comp: any) =>
accessedServices.includes(comp.composeId),
),
});
export const environmentRouter = createTRPCRouter({
create: protectedProcedure
.input(apiCreateEnvironment)
.mutation(async ({ input, ctx }) => {
try {
// Check if user has access to the project
// This would typically involve checking project ownership/membership
// For now, we'll use a basic organization check
const environment = await createEnvironment(input);
if (ctx.user.role === "member") {
await addNewEnvironment(
ctx.user.id,
environment.environmentId,
ctx.session.activeOrganizationId,
);
}
return environment;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error creating the environment: ${error instanceof Error ? error.message : error}`,
cause: error,
});
}
}),
one: protectedProcedure
.input(apiFindOneEnvironment)
.query(async ({ input, ctx }) => {
try {
if (ctx.user.role === "member") {
await checkEnvironmentAccess(
ctx.user.id,
input.environmentId,
ctx.session.activeOrganizationId,
"access",
);
}
const environment = await findEnvironmentById(input.environmentId);
if (
environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to access this environment",
});
}
// Check environment access and filter services for members
if (ctx.user.role === "member") {
const { accessedEnvironments, accessedServices } =
await findMemberById(ctx.user.id, ctx.session.activeOrganizationId);
if (!accessedEnvironments.includes(environment.environmentId)) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to access this environment",
});
}
// Filter services based on member permissions
const filteredEnvironment = filterEnvironmentServices(
environment,
accessedServices,
);
return filteredEnvironment;
}
return environment;
} catch (error) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Environment not found",
});
}
}),
byProjectId: protectedProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input, ctx }) => {
try {
const environments = await findEnvironmentsByProjectId(input.projectId);
// Check organization access
if (
environments.some(
(environment) =>
environment.project.organizationId !==
ctx.session.activeOrganizationId,
)
) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to access this environment",
});
}
// Filter environments for members based on their permissions
if (ctx.user.role === "member") {
const { accessedEnvironments, accessedServices } =
await findMemberById(ctx.user.id, ctx.session.activeOrganizationId);
// Filter environments to only show those the member has access to
const filteredEnvironments = environments
.filter((environment) =>
accessedEnvironments.includes(environment.environmentId),
)
.map((environment) =>
filterEnvironmentServices(environment, accessedServices),
);
return filteredEnvironments;
}
return environments;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error fetching environments: ${error instanceof Error ? error.message : error}`,
});
}
}),
remove: protectedProcedure
.input(apiRemoveEnvironment)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.role === "member") {
await checkEnvironmentAccess(
ctx.user.id,
input.environmentId,
ctx.session.activeOrganizationId,
"access",
);
}
const environment = await findEnvironmentById(input.environmentId);
if (
environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to access this environment",
});
}
// Check environment access for members
if (ctx.user.role === "member") {
const { accessedEnvironments } = await findMemberById(
ctx.user.id,
ctx.session.activeOrganizationId,
);
if (!accessedEnvironments.includes(environment.environmentId)) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to delete this environment",
});
}
}
const deletedEnvironment = await deleteEnvironment(input.environmentId);
return deletedEnvironment;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error deleting the environment: ${error instanceof Error ? error.message : error}`,
});
}
}),
update: protectedProcedure
.input(apiUpdateEnvironment)
.mutation(async ({ input, ctx }) => {
try {
const { environmentId, ...updateData } = input;
if (ctx.user.role === "member") {
await checkEnvironmentAccess(
ctx.user.id,
environmentId,
ctx.session.activeOrganizationId,
"access",
);
}
const currentEnvironment = await findEnvironmentById(environmentId);
if (
currentEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to access this environment",
});
}
// Check environment access for members
if (ctx.user.role === "member") {
const { accessedEnvironments } = await findMemberById(
ctx.user.id,
ctx.session.activeOrganizationId,
);
if (
!accessedEnvironments.includes(currentEnvironment.environmentId)
) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to update this environment",
});
}
}
const environment = await updateEnvironmentById(
environmentId,
updateData,
);
return environment;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error updating the environment: ${error instanceof Error ? error.message : error}`,
});
}
}),
duplicate: protectedProcedure
.input(apiDuplicateEnvironment)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.role === "member") {
await checkEnvironmentAccess(
ctx.user.id,
input.environmentId,
ctx.session.activeOrganizationId,
"access",
);
}
const environment = await findEnvironmentById(input.environmentId);
if (
environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to access this environment",
});
}
// Check environment access for members
if (ctx.user.role === "member") {
const { accessedEnvironments } = await findMemberById(
ctx.user.id,
ctx.session.activeOrganizationId,
);
if (!accessedEnvironments.includes(environment.environmentId)) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You are not allowed to duplicate this environment",
});
}
}
const duplicatedEnvironment = await duplicateEnvironment(input);
return duplicatedEnvironment;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error duplicating the environment: ${error instanceof Error ? error.message : error}`,
});
}
}),
});
+67 -20
View File
@@ -6,6 +6,7 @@ import {
deployMariadb, deployMariadb,
findBackupsByDbId, findBackupsByDbId,
findMariadbById, findMariadbById,
findEnvironmentById,
findProjectById, findProjectById,
IS_CLOUD, IS_CLOUD,
rebuildDatabase, rebuildDatabase,
@@ -41,10 +42,14 @@ export const mariadbRouter = createTRPCRouter({
.input(apiCreateMariaDB) .input(apiCreateMariaDB)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -57,14 +62,15 @@ export const mariadbRouter = createTRPCRouter({
}); });
} }
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
}); });
} }
const newMariadb = await createMariadb(input); const newMariadb = await createMariadb({
...input,
});
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewService( await addNewService(
ctx.user.id, ctx.user.id,
@@ -101,7 +107,10 @@ export const mariadbRouter = createTRPCRouter({
); );
} }
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this Mariadb", message: "You are not authorized to access this Mariadb",
@@ -114,7 +123,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiFindOneMariaDB) .input(apiFindOneMariaDB)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const service = await findMariadbById(input.mariadbId); const service = await findMariadbById(input.mariadbId);
if (service.project.organizationId !== ctx.session.activeOrganizationId) { if (
service.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to start this Mariadb", message: "You are not authorized to start this Mariadb",
@@ -151,7 +163,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiSaveExternalPortMariaDB) .input(apiSaveExternalPortMariaDB)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMariadbById(input.mariadbId); const mongo = await findMariadbById(input.mariadbId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this external port", message: "You are not authorized to save this external port",
@@ -167,7 +182,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiDeployMariaDB) .input(apiDeployMariaDB)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this Mariadb", message: "You are not authorized to deploy this Mariadb",
@@ -188,7 +206,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiDeployMariaDB) .input(apiDeployMariaDB)
.subscription(async ({ input, ctx }) => { .subscription(async ({ input, ctx }) => {
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this Mariadb", message: "You are not authorized to deploy this Mariadb",
@@ -205,7 +226,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiChangeMariaDBStatus) .input(apiChangeMariaDBStatus)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMariadbById(input.mariadbId); const mongo = await findMariadbById(input.mariadbId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to change this Mariadb status", message: "You are not authorized to change this Mariadb status",
@@ -229,7 +253,10 @@ export const mariadbRouter = createTRPCRouter({
} }
const mongo = await findMariadbById(input.mariadbId); const mongo = await findMariadbById(input.mariadbId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to delete this Mariadb", message: "You are not authorized to delete this Mariadb",
@@ -255,7 +282,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiSaveEnvironmentVariablesMariaDB) .input(apiSaveEnvironmentVariablesMariaDB)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this environment", message: "You are not authorized to save this environment",
@@ -278,7 +308,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiResetMariadb) .input(apiResetMariadb)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to reload this Mariadb", message: "You are not authorized to reload this Mariadb",
@@ -308,7 +341,10 @@ export const mariadbRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const { mariadbId, ...rest } = input; const { mariadbId, ...rest } = input;
const mariadb = await findMariadbById(mariadbId); const mariadb = await findMariadbById(mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to update this Mariadb", message: "You are not authorized to update this Mariadb",
@@ -331,23 +367,31 @@ export const mariadbRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
mariadbId: z.string(), mariadbId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move this mariadb", message: "You are not authorized to move this mariadb",
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -355,7 +399,7 @@ export const mariadbRouter = createTRPCRouter({
const updatedMariadb = await db const updatedMariadb = await db
.update(mariadbTable) .update(mariadbTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(mariadbTable.mariadbId, input.mariadbId)) .where(eq(mariadbTable.mariadbId, input.mariadbId))
.returning() .returning()
@@ -374,7 +418,10 @@ export const mariadbRouter = createTRPCRouter({
.input(apiRebuildMariadb) .input(apiRebuildMariadb)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mariadb = await findMariadbById(input.mariadbId); const mariadb = await findMariadbById(input.mariadbId);
if (mariadb.project.organizationId !== ctx.session.activeOrganizationId) { if (
mariadb.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to rebuild this MariaDB database", message: "You are not authorized to rebuild this MariaDB database",
+71 -21
View File
@@ -6,6 +6,7 @@ import {
deployMongo, deployMongo,
findBackupsByDbId, findBackupsByDbId,
findMongoById, findMongoById,
findEnvironmentById,
findProjectById, findProjectById,
IS_CLOUD, IS_CLOUD,
rebuildDatabase, rebuildDatabase,
@@ -41,10 +42,14 @@ export const mongoRouter = createTRPCRouter({
.input(apiCreateMongo) .input(apiCreateMongo)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -57,14 +62,15 @@ export const mongoRouter = createTRPCRouter({
}); });
} }
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
}); });
} }
const newMongo = await createMongo(input); const newMongo = await createMongo({
...input,
});
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewService( await addNewService(
ctx.user.id, ctx.user.id,
@@ -106,7 +112,10 @@ export const mongoRouter = createTRPCRouter({
} }
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this mongo", message: "You are not authorized to access this mongo",
@@ -120,7 +129,10 @@ export const mongoRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const service = await findMongoById(input.mongoId); const service = await findMongoById(input.mongoId);
if (service.project.organizationId !== ctx.session.activeOrganizationId) { if (
service.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to start this mongo", message: "You are not authorized to start this mongo",
@@ -143,7 +155,10 @@ export const mongoRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to stop this mongo", message: "You are not authorized to stop this mongo",
@@ -165,7 +180,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiSaveExternalPortMongo) .input(apiSaveExternalPortMongo)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this external port", message: "You are not authorized to save this external port",
@@ -181,7 +199,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiDeployMongo) .input(apiDeployMongo)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this mongo", message: "You are not authorized to deploy this mongo",
@@ -201,7 +222,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiDeployMongo) .input(apiDeployMongo)
.subscription(async ({ input, ctx }) => { .subscription(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this mongo", message: "You are not authorized to deploy this mongo",
@@ -218,7 +242,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiChangeMongoStatus) .input(apiChangeMongoStatus)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to change this mongo status", message: "You are not authorized to change this mongo status",
@@ -233,7 +260,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiResetMongo) .input(apiResetMongo)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to reload this mongo", message: "You are not authorized to reload this mongo",
@@ -272,7 +302,10 @@ export const mongoRouter = createTRPCRouter({
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to delete this mongo", message: "You are not authorized to delete this mongo",
@@ -298,7 +331,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiSaveEnvironmentVariablesMongo) .input(apiSaveEnvironmentVariablesMongo)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this environment", message: "You are not authorized to save this environment",
@@ -322,7 +358,10 @@ export const mongoRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const { mongoId, ...rest } = input; const { mongoId, ...rest } = input;
const mongo = await findMongoById(mongoId); const mongo = await findMongoById(mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to update this mongo", message: "You are not authorized to update this mongo",
@@ -345,23 +384,31 @@ export const mongoRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
mongoId: z.string(), mongoId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move this mongo", message: "You are not authorized to move this mongo",
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -369,7 +416,7 @@ export const mongoRouter = createTRPCRouter({
const updatedMongo = await db const updatedMongo = await db
.update(mongoTable) .update(mongoTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(mongoTable.mongoId, input.mongoId)) .where(eq(mongoTable.mongoId, input.mongoId))
.returning() .returning()
@@ -388,7 +435,10 @@ export const mongoRouter = createTRPCRouter({
.input(apiRebuildMongo) .input(apiRebuildMongo)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMongoById(input.mongoId); const mongo = await findMongoById(input.mongoId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to rebuild this MongoDB database", message: "You are not authorized to rebuild this MongoDB database",
+30 -5
View File
@@ -3,9 +3,11 @@ import {
deleteMount, deleteMount,
findApplicationById, findApplicationById,
findMountById, findMountById,
findMountOrganizationId,
getServiceContainer, getServiceContainer,
updateMount, updateMount,
} from "@dokploy/server"; } from "@dokploy/server";
import { TRPCError } from "@trpc/server";
import { z } from "zod"; import { z } from "zod";
import { import {
apiCreateMount, apiCreateMount,
@@ -24,16 +26,39 @@ export const mountRouter = createTRPCRouter({
}), }),
remove: protectedProcedure remove: protectedProcedure
.input(apiRemoveMount) .input(apiRemoveMount)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
const organizationId = await findMountOrganizationId(input.mountId);
if (organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to delete this mount",
});
}
return await deleteMount(input.mountId); return await deleteMount(input.mountId);
}), }),
one: protectedProcedure.input(apiFindOneMount).query(async ({ input }) => { one: protectedProcedure
return await findMountById(input.mountId); .input(apiFindOneMount)
}), .query(async ({ input, ctx }) => {
const organizationId = await findMountOrganizationId(input.mountId);
if (organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this mount",
});
}
return await findMountById(input.mountId);
}),
update: protectedProcedure update: protectedProcedure
.input(apiUpdateMount) .input(apiUpdateMount)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
const organizationId = await findMountOrganizationId(input.mountId);
if (organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to update this mount",
});
}
return await updateMount(input.mountId, input); return await updateMount(input.mountId, input);
}), }),
allNamedByApplicationId: protectedProcedure allNamedByApplicationId: protectedProcedure
+72 -22
View File
@@ -5,6 +5,7 @@ import {
createMysql, createMysql,
deployMySql, deployMySql,
findBackupsByDbId, findBackupsByDbId,
findEnvironmentById,
findMySqlById, findMySqlById,
findProjectById, findProjectById,
IS_CLOUD, IS_CLOUD,
@@ -42,10 +43,14 @@ export const mysqlRouter = createTRPCRouter({
.input(apiCreateMySql) .input(apiCreateMySql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -57,8 +62,7 @@ export const mysqlRouter = createTRPCRouter({
message: "You need to use a server to create a MySQL", message: "You need to use a server to create a MySQL",
}); });
} }
1;
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -66,7 +70,9 @@ export const mysqlRouter = createTRPCRouter({
}); });
} }
const newMysql = await createMysql(input); const newMysql = await createMysql({
...input,
});
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewService( await addNewService(
ctx.user.id, ctx.user.id,
@@ -107,7 +113,10 @@ export const mysqlRouter = createTRPCRouter({
); );
} }
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this MySQL", message: "You are not authorized to access this MySQL",
@@ -120,7 +129,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiFindOneMySql) .input(apiFindOneMySql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const service = await findMySqlById(input.mysqlId); const service = await findMySqlById(input.mysqlId);
if (service.project.organizationId !== ctx.session.activeOrganizationId) { if (
service.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to start this MySQL", message: "You are not authorized to start this MySQL",
@@ -142,7 +154,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiFindOneMySql) .input(apiFindOneMySql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMySqlById(input.mysqlId); const mongo = await findMySqlById(input.mysqlId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to stop this MySQL", message: "You are not authorized to stop this MySQL",
@@ -163,7 +178,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiSaveExternalPortMySql) .input(apiSaveExternalPortMySql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMySqlById(input.mysqlId); const mongo = await findMySqlById(input.mysqlId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this external port", message: "You are not authorized to save this external port",
@@ -179,7 +197,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiDeployMySql) .input(apiDeployMySql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this MySQL", message: "You are not authorized to deploy this MySQL",
@@ -199,7 +220,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiDeployMySql) .input(apiDeployMySql)
.subscription(async ({ input, ctx }) => { .subscription(async ({ input, ctx }) => {
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this MySQL", message: "You are not authorized to deploy this MySQL",
@@ -216,7 +240,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiChangeMySqlStatus) .input(apiChangeMySqlStatus)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findMySqlById(input.mysqlId); const mongo = await findMySqlById(input.mysqlId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to change this MySQL status", message: "You are not authorized to change this MySQL status",
@@ -231,7 +258,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiResetMysql) .input(apiResetMysql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to reload this MySQL", message: "You are not authorized to reload this MySQL",
@@ -267,7 +297,10 @@ export const mysqlRouter = createTRPCRouter({
); );
} }
const mongo = await findMySqlById(input.mysqlId); const mongo = await findMySqlById(input.mysqlId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to delete this MySQL", message: "You are not authorized to delete this MySQL",
@@ -293,7 +326,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiSaveEnvironmentVariablesMySql) .input(apiSaveEnvironmentVariablesMySql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this environment", message: "You are not authorized to save this environment",
@@ -317,7 +353,10 @@ export const mysqlRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const { mysqlId, ...rest } = input; const { mysqlId, ...rest } = input;
const mysql = await findMySqlById(mysqlId); const mysql = await findMySqlById(mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to update this MySQL", message: "You are not authorized to update this MySQL",
@@ -340,23 +379,31 @@ export const mysqlRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
mysqlId: z.string(), mysqlId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move this mysql", message: "You are not authorized to move this mysql",
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -364,7 +411,7 @@ export const mysqlRouter = createTRPCRouter({
const updatedMysql = await db const updatedMysql = await db
.update(mysqlTable) .update(mysqlTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(mysqlTable.mysqlId, input.mysqlId)) .where(eq(mysqlTable.mysqlId, input.mysqlId))
.returning() .returning()
@@ -383,7 +430,10 @@ export const mysqlRouter = createTRPCRouter({
.input(apiRebuildMysql) .input(apiRebuildMysql)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mysql = await findMySqlById(input.mysqlId); const mysql = await findMySqlById(input.mysqlId);
if (mysql.project.organizationId !== ctx.session.activeOrganizationId) { if (
mysql.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to rebuild this MySQL database", message: "You are not authorized to rebuild this MySQL database",
+47 -15
View File
@@ -27,22 +27,44 @@ export const portRouter = createTRPCRouter({
}); });
} }
}), }),
one: protectedProcedure.input(apiFindOnePort).query(async ({ input }) => { one: protectedProcedure
try { .input(apiFindOnePort)
return await finPortById(input.portId); .query(async ({ input, ctx }) => {
} catch (error) { try {
throw new TRPCError({ const port = await finPortById(input.portId);
code: "BAD_REQUEST", if (
message: "Port not found", port.application.environment.project.organizationId !==
cause: error, ctx.session.activeOrganizationId
}); ) {
} throw new TRPCError({
}), code: "UNAUTHORIZED",
message: "You are not authorized to access this port",
});
}
return port;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Port not found",
cause: error,
});
}
}),
delete: protectedProcedure delete: protectedProcedure
.input(apiFindOnePort) .input(apiFindOnePort)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
const port = await finPortById(input.portId);
if (
port.application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to delete this port",
});
}
try { try {
return removePortById(input.portId); return await removePortById(input.portId);
} catch (error) { } catch (error) {
const message = const message =
error instanceof Error ? error.message : "Error input: Deleting port"; error instanceof Error ? error.message : "Error input: Deleting port";
@@ -54,9 +76,19 @@ export const portRouter = createTRPCRouter({
}), }),
update: protectedProcedure update: protectedProcedure
.input(apiUpdatePort) .input(apiUpdatePort)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
const port = await finPortById(input.portId);
if (
port.application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to update this port",
});
}
try { try {
return updatePortById(input.portId, input); return await updatePortById(input.portId, input);
} catch (error) { } catch (error) {
const message = const message =
error instanceof Error ? error.message : "Error updating the port"; error instanceof Error ? error.message : "Error updating the port";
+47 -21
View File
@@ -5,6 +5,7 @@ import {
createPostgres, createPostgres,
deployPostgres, deployPostgres,
findBackupsByDbId, findBackupsByDbId,
findEnvironmentById,
findPostgresById, findPostgresById,
findProjectById, findProjectById,
IS_CLOUD, IS_CLOUD,
@@ -41,10 +42,14 @@ export const postgresRouter = createTRPCRouter({
.input(apiCreatePostgres) .input(apiCreatePostgres)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -57,14 +62,15 @@ export const postgresRouter = createTRPCRouter({
}); });
} }
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
}); });
} }
const newPostgres = await createPostgres(input); const newPostgres = await createPostgres({
...input,
});
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewService( await addNewService(
ctx.user.id, ctx.user.id,
@@ -107,7 +113,8 @@ export const postgresRouter = createTRPCRouter({
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -122,7 +129,10 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const service = await findPostgresById(input.postgresId); const service = await findPostgresById(input.postgresId);
if (service.project.organizationId !== ctx.session.activeOrganizationId) { if (
service.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to start this Postgres", message: "You are not authorized to start this Postgres",
@@ -145,7 +155,8 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -169,7 +180,8 @@ export const postgresRouter = createTRPCRouter({
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -187,7 +199,8 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -210,7 +223,8 @@ export const postgresRouter = createTRPCRouter({
.subscription(async ({ input, ctx }) => { .subscription(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -229,7 +243,8 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -255,7 +270,8 @@ export const postgresRouter = createTRPCRouter({
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -280,7 +296,8 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -305,7 +322,8 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -337,7 +355,8 @@ export const postgresRouter = createTRPCRouter({
const { postgresId, ...rest } = input; const { postgresId, ...rest } = input;
const postgres = await findPostgresById(postgresId); const postgres = await findPostgresById(postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -361,13 +380,14 @@ export const postgresRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
postgresId: z.string(), postgresId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -375,11 +395,16 @@ export const postgresRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -387,7 +412,7 @@ export const postgresRouter = createTRPCRouter({
const updatedPostgres = await db const updatedPostgres = await db
.update(postgresTable) .update(postgresTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(postgresTable.postgresId, input.postgresId)) .where(eq(postgresTable.postgresId, input.postgresId))
.returning() .returning()
@@ -407,7 +432,8 @@ export const postgresRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const postgres = await findPostgresById(input.postgresId); const postgres = await findPostgresById(input.postgresId);
if ( if (
postgres.project.organizationId !== ctx.session.activeOrganizationId postgres.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -15,7 +15,8 @@ export const previewDeploymentRouter = createTRPCRouter({
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -31,7 +32,7 @@ export const previewDeploymentRouter = createTRPCRouter({
input.previewDeploymentId, input.previewDeploymentId,
); );
if ( if (
previewDeployment.application.project.organizationId !== previewDeployment.application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
@@ -49,7 +50,7 @@ export const previewDeploymentRouter = createTRPCRouter({
input.previewDeploymentId, input.previewDeploymentId,
); );
if ( if (
previewDeployment.application.project.organizationId !== previewDeployment.application.environment.project.organizationId !==
ctx.session.activeOrganizationId ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
+121 -79
View File
@@ -19,6 +19,7 @@ import {
deleteProject, deleteProject,
findApplicationById, findApplicationById,
findComposeById, findComposeById,
findEnvironmentById,
findMariadbById, findMariadbById,
findMemberById, findMemberById,
findMongoById, findMongoById,
@@ -43,6 +44,7 @@ import {
apiUpdateProject, apiUpdateProject,
applications, applications,
compose, compose,
environments,
mariadb, mariadb,
mongo, mongo,
mysql, mysql,
@@ -80,7 +82,7 @@ export const projectRouter = createTRPCRouter({
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewProject( await addNewProject(
ctx.user.id, ctx.user.id,
project.projectId, project.project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
); );
} }
@@ -117,29 +119,42 @@ export const projectRouter = createTRPCRouter({
eq(projects.organizationId, ctx.session.activeOrganizationId), eq(projects.organizationId, ctx.session.activeOrganizationId),
), ),
with: { with: {
applications: { environments: {
where: buildServiceFilter( with: {
applications.applicationId, applications: {
accessedServices, where: buildServiceFilter(
), applications.applicationId,
}, accessedServices,
compose: { ),
where: buildServiceFilter(compose.composeId, accessedServices), },
}, compose: {
mariadb: { where: buildServiceFilter(
where: buildServiceFilter(mariadb.mariadbId, accessedServices), compose.composeId,
}, accessedServices,
mongo: { ),
where: buildServiceFilter(mongo.mongoId, accessedServices), },
}, mariadb: {
mysql: { where: buildServiceFilter(
where: buildServiceFilter(mysql.mysqlId, accessedServices), mariadb.mariadbId,
}, accessedServices,
postgres: { ),
where: buildServiceFilter(postgres.postgresId, accessedServices), },
}, mongo: {
redis: { where: buildServiceFilter(mongo.mongoId, accessedServices),
where: buildServiceFilter(redis.redisId, accessedServices), },
mysql: {
where: buildServiceFilter(mysql.mysqlId, accessedServices),
},
postgres: {
where: buildServiceFilter(
postgres.postgresId,
accessedServices,
),
},
redis: {
where: buildServiceFilter(redis.redisId, accessedServices),
},
},
}, },
}, },
}); });
@@ -164,15 +179,22 @@ export const projectRouter = createTRPCRouter({
}), }),
all: protectedProcedure.query(async ({ ctx }) => { all: protectedProcedure.query(async ({ ctx }) => {
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
const { accessedProjects, accessedServices } = await findMemberById( const { accessedProjects, accessedEnvironments, accessedServices } =
ctx.user.id, await findMemberById(ctx.user.id, ctx.session.activeOrganizationId);
ctx.session.activeOrganizationId,
);
if (accessedProjects.length === 0) { if (accessedProjects.length === 0) {
return []; return [];
} }
// Build environment filter
const environmentFilter =
accessedEnvironments.length === 0
? sql`false`
: sql`${environments.environmentId} IN (${sql.join(
accessedEnvironments.map((envId) => sql`${envId}`),
sql`, `,
)})`;
return await db.query.projects.findMany({ return await db.query.projects.findMany({
where: and( where: and(
sql`${projects.projectId} IN (${sql.join( sql`${projects.projectId} IN (${sql.join(
@@ -182,31 +204,39 @@ export const projectRouter = createTRPCRouter({
eq(projects.organizationId, ctx.session.activeOrganizationId), eq(projects.organizationId, ctx.session.activeOrganizationId),
), ),
with: { with: {
applications: { environments: {
where: buildServiceFilter( where: environmentFilter,
applications.applicationId, with: {
accessedServices, applications: {
), where: buildServiceFilter(
with: { domains: true }, applications.applicationId,
}, accessedServices,
mariadb: { ),
where: buildServiceFilter(mariadb.mariadbId, accessedServices), with: { domains: true },
}, },
mongo: { mariadb: {
where: buildServiceFilter(mongo.mongoId, accessedServices), where: buildServiceFilter(mariadb.mariadbId, accessedServices),
}, },
mysql: { mongo: {
where: buildServiceFilter(mysql.mysqlId, accessedServices), where: buildServiceFilter(mongo.mongoId, accessedServices),
}, },
postgres: { mysql: {
where: buildServiceFilter(postgres.postgresId, accessedServices), where: buildServiceFilter(mysql.mysqlId, accessedServices),
}, },
redis: { postgres: {
where: buildServiceFilter(redis.redisId, accessedServices), where: buildServiceFilter(
}, postgres.postgresId,
compose: { accessedServices,
where: buildServiceFilter(compose.composeId, accessedServices), ),
with: { domains: true }, },
redis: {
where: buildServiceFilter(redis.redisId, accessedServices),
},
compose: {
where: buildServiceFilter(compose.composeId, accessedServices),
with: { domains: true },
},
},
}, },
}, },
orderBy: desc(projects.createdAt), orderBy: desc(projects.createdAt),
@@ -215,19 +245,23 @@ export const projectRouter = createTRPCRouter({
return await db.query.projects.findMany({ return await db.query.projects.findMany({
with: { with: {
applications: { environments: {
with: { with: {
domains: true, applications: {
}, with: {
}, domains: true,
mariadb: true, },
mongo: true, },
mysql: true, mariadb: true,
postgres: true, mongo: true,
redis: true, mysql: true,
compose: { postgres: true,
with: { redis: true,
domains: true, compose: {
with: {
domains: true,
},
},
}, },
}, },
}, },
@@ -288,7 +322,7 @@ export const projectRouter = createTRPCRouter({
duplicate: protectedProcedure duplicate: protectedProcedure
.input( .input(
z.object({ z.object({
sourceProjectId: z.string(), sourceEnvironmentId: z.string(),
name: z.string(), name: z.string(),
description: z.string().optional(), description: z.string().optional(),
includeServices: z.boolean().default(true), includeServices: z.boolean().default(true),
@@ -322,9 +356,15 @@ export const projectRouter = createTRPCRouter({
} }
// Get source project // Get source project
const sourceProject = await findProjectById(input.sourceProjectId); const sourceEnvironment = input.duplicateInSameProject
? await findEnvironmentById(input.sourceEnvironmentId)
: null;
if (sourceProject.organizationId !== ctx.session.activeOrganizationId) { if (
input.duplicateInSameProject &&
sourceEnvironment?.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
@@ -333,15 +373,17 @@ export const projectRouter = createTRPCRouter({
// Create new project or use existing one // Create new project or use existing one
const targetProject = input.duplicateInSameProject const targetProject = input.duplicateInSameProject
? sourceProject ? sourceEnvironment
: await createProject( : await createProject(
{ {
name: input.name, name: input.name,
description: input.description, description: input.description,
env: sourceProject.env, env: sourceEnvironment?.project.env,
}, },
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
); ).then((value) => value.environment);
console.log("targetProject", targetProject);
if (input.includeServices) { if (input.includeServices) {
const servicesToDuplicate = input.selectedServices || []; const servicesToDuplicate = input.selectedServices || [];
@@ -374,7 +416,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${application.name} (copy)` ? `${application.name} (copy)`
: application.name, : application.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const domain of domains) { for (const domain of domains) {
@@ -444,7 +486,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${postgres.name} (copy)` ? `${postgres.name} (copy)`
: postgres.name, : postgres.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const mount of mounts) { for (const mount of mounts) {
@@ -480,7 +522,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${mariadb.name} (copy)` ? `${mariadb.name} (copy)`
: mariadb.name, : mariadb.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const mount of mounts) { for (const mount of mounts) {
@@ -516,7 +558,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${mongo.name} (copy)` ? `${mongo.name} (copy)`
: mongo.name, : mongo.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const mount of mounts) { for (const mount of mounts) {
@@ -552,7 +594,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${mysql.name} (copy)` ? `${mysql.name} (copy)`
: mysql.name, : mysql.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const mount of mounts) { for (const mount of mounts) {
@@ -588,7 +630,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${redis.name} (copy)` ? `${redis.name} (copy)`
: redis.name, : redis.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const mount of mounts) { for (const mount of mounts) {
@@ -623,7 +665,7 @@ export const projectRouter = createTRPCRouter({
name: input.duplicateInSameProject name: input.duplicateInSameProject
? `${compose.name} (copy)` ? `${compose.name} (copy)`
: compose.name, : compose.name,
projectId: targetProject.projectId, environmentId: targetProject?.environmentId || "",
}); });
for (const mount of mounts) { for (const mount of mounts) {
@@ -658,7 +700,7 @@ export const projectRouter = createTRPCRouter({
if (!input.duplicateInSameProject && ctx.user.role === "member") { if (!input.duplicateInSameProject && ctx.user.role === "member") {
await addNewProject( await addNewProject(
ctx.user.id, ctx.user.id,
targetProject.projectId, targetProject?.projectId || "",
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
); );
} }
+8 -4
View File
@@ -19,7 +19,8 @@ export const redirectsRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -34,7 +35,8 @@ export const redirectsRouter = createTRPCRouter({
const redirect = await findRedirectById(input.redirectId); const redirect = await findRedirectById(input.redirectId);
const application = await findApplicationById(redirect.applicationId); const application = await findApplicationById(redirect.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -49,7 +51,8 @@ export const redirectsRouter = createTRPCRouter({
const redirect = await findRedirectById(input.redirectId); const redirect = await findRedirectById(input.redirectId);
const application = await findApplicationById(redirect.applicationId); const application = await findApplicationById(redirect.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -64,7 +67,8 @@ export const redirectsRouter = createTRPCRouter({
const redirect = await findRedirectById(input.redirectId); const redirect = await findRedirectById(input.redirectId);
const application = await findApplicationById(redirect.applicationId); const application = await findApplicationById(redirect.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
+67 -20
View File
@@ -4,6 +4,7 @@ import {
createMount, createMount,
createRedis, createRedis,
deployRedis, deployRedis,
findEnvironmentById,
findProjectById, findProjectById,
findRedisById, findRedisById,
IS_CLOUD, IS_CLOUD,
@@ -40,10 +41,14 @@ export const redisRouter = createTRPCRouter({
.input(apiCreateRedis) .input(apiCreateRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
try { try {
// Get project from environment
const environment = await findEnvironmentById(input.environmentId);
const project = await findProjectById(environment.projectId);
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await checkServiceAccess( await checkServiceAccess(
ctx.user.id, ctx.user.id,
input.projectId, project.projectId,
ctx.session.activeOrganizationId, ctx.session.activeOrganizationId,
"create", "create",
); );
@@ -56,14 +61,15 @@ export const redisRouter = createTRPCRouter({
}); });
} }
const project = await findProjectById(input.projectId);
if (project.organizationId !== ctx.session.activeOrganizationId) { if (project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this project", message: "You are not authorized to access this project",
}); });
} }
const newRedis = await createRedis(input); const newRedis = await createRedis({
...input,
});
if (ctx.user.role === "member") { if (ctx.user.role === "member") {
await addNewService( await addNewService(
ctx.user.id, ctx.user.id,
@@ -98,7 +104,10 @@ export const redisRouter = createTRPCRouter({
} }
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to access this Redis", message: "You are not authorized to access this Redis",
@@ -111,7 +120,10 @@ export const redisRouter = createTRPCRouter({
.input(apiFindOneRedis) .input(apiFindOneRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to start this Redis", message: "You are not authorized to start this Redis",
@@ -133,7 +145,10 @@ export const redisRouter = createTRPCRouter({
.input(apiResetRedis) .input(apiResetRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to reload this Redis", message: "You are not authorized to reload this Redis",
@@ -163,7 +178,10 @@ export const redisRouter = createTRPCRouter({
.input(apiFindOneRedis) .input(apiFindOneRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to stop this Redis", message: "You are not authorized to stop this Redis",
@@ -184,7 +202,10 @@ export const redisRouter = createTRPCRouter({
.input(apiSaveExternalPortRedis) .input(apiSaveExternalPortRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findRedisById(input.redisId); const mongo = await findRedisById(input.redisId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this external port", message: "You are not authorized to save this external port",
@@ -200,7 +221,10 @@ export const redisRouter = createTRPCRouter({
.input(apiDeployRedis) .input(apiDeployRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this Redis", message: "You are not authorized to deploy this Redis",
@@ -220,7 +244,10 @@ export const redisRouter = createTRPCRouter({
.input(apiDeployRedis) .input(apiDeployRedis)
.subscription(async ({ input, ctx }) => { .subscription(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to deploy this Redis", message: "You are not authorized to deploy this Redis",
@@ -236,7 +263,10 @@ export const redisRouter = createTRPCRouter({
.input(apiChangeRedisStatus) .input(apiChangeRedisStatus)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const mongo = await findRedisById(input.redisId); const mongo = await findRedisById(input.redisId);
if (mongo.project.organizationId !== ctx.session.activeOrganizationId) { if (
mongo.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to change this Redis status", message: "You are not authorized to change this Redis status",
@@ -261,7 +291,10 @@ export const redisRouter = createTRPCRouter({
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to delete this Redis", message: "You are not authorized to delete this Redis",
@@ -284,7 +317,10 @@ export const redisRouter = createTRPCRouter({
.input(apiSaveEnvironmentVariablesRedis) .input(apiSaveEnvironmentVariablesRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to save this environment", message: "You are not authorized to save this environment",
@@ -324,23 +360,31 @@ export const redisRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
redisId: z.string(), redisId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move this redis", message: "You are not authorized to move this redis",
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { input.targetEnvironmentId,
);
if (
targetEnvironment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -348,7 +392,7 @@ export const redisRouter = createTRPCRouter({
const updatedRedis = await db const updatedRedis = await db
.update(redisTable) .update(redisTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(redisTable.redisId, input.redisId)) .where(eq(redisTable.redisId, input.redisId))
.returning() .returning()
@@ -367,7 +411,10 @@ export const redisRouter = createTRPCRouter({
.input(apiRebuildRedis) .input(apiRebuildRedis)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const redis = await findRedisById(input.redisId); const redis = await findRedisById(input.redisId);
if (redis.project.organizationId !== ctx.session.activeOrganizationId) { if (
redis.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to rebuild this Redis database", message: "You are not authorized to rebuild this Redis database",
+16 -2
View File
@@ -1,4 +1,8 @@
import { removeRollbackById, rollback } from "@dokploy/server"; import {
findRollbackById,
removeRollbackById,
rollback,
} from "@dokploy/server";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc"; import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import { apiFindOneRollback } from "@/server/db/schema"; import { apiFindOneRollback } from "@/server/db/schema";
@@ -22,8 +26,18 @@ export const rollbackRouter = createTRPCRouter({
}), }),
rollback: protectedProcedure rollback: protectedProcedure
.input(apiFindOneRollback) .input(apiFindOneRollback)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
try { try {
const currentRollback = await findRollbackById(input.rollbackId);
if (
currentRollback?.deployment?.application?.environment?.project
.organizationId !== ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to rollback this deployment",
});
}
return await rollback(input.rollbackId); return await rollback(input.rollbackId);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
+8 -4
View File
@@ -19,7 +19,8 @@ export const securityRouter = createTRPCRouter({
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
const application = await findApplicationById(input.applicationId); const application = await findApplicationById(input.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -34,7 +35,8 @@ export const securityRouter = createTRPCRouter({
const security = await findSecurityById(input.securityId); const security = await findSecurityById(input.securityId);
const application = await findApplicationById(security.applicationId); const application = await findApplicationById(security.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -49,7 +51,8 @@ export const securityRouter = createTRPCRouter({
const security = await findSecurityById(input.securityId); const security = await findSecurityById(input.securityId);
const application = await findApplicationById(security.applicationId); const application = await findApplicationById(security.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
@@ -64,7 +67,8 @@ export const securityRouter = createTRPCRouter({
const security = await findSecurityById(input.securityId); const security = await findSecurityById(input.securityId);
const application = await findApplicationById(security.applicationId); const application = await findApplicationById(security.applicationId);
if ( if (
application.project.organizationId !== ctx.session.activeOrganizationId application.environment.project.organizationId !==
ctx.session.activeOrganizationId
) { ) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
+4
View File
@@ -112,6 +112,10 @@ export const member = pgTable("member", {
.array() .array()
.notNull() .notNull()
.default(sql`ARRAY[]::text[]`), .default(sql`ARRAY[]::text[]`),
accessedEnvironments: text("accessedEnvironments")
.array()
.notNull()
.default(sql`ARRAY[]::text[]`),
accessedServices: text("accesedServices") accessedServices: text("accesedServices")
.array() .array()
.notNull() .notNull()
+1 -1
View File
@@ -55,7 +55,7 @@ export const apiUpdateAi = createSchema
.omit({ organizationId: true }); .omit({ organizationId: true });
export const deploySuggestionSchema = z.object({ export const deploySuggestionSchema = z.object({
projectId: z.string().min(1), environmentId: z.string().min(1),
id: z.string().min(1), id: z.string().min(1),
dockerCompose: z.string().min(1), dockerCompose: z.string().min(1),
envVariables: z.string(), envVariables: z.string(),
+8 -7
View File
@@ -13,6 +13,7 @@ import { z } from "zod";
import { bitbucket } from "./bitbucket"; import { bitbucket } from "./bitbucket";
import { deployments } from "./deployment"; import { deployments } from "./deployment";
import { domains } from "./domain"; import { domains } from "./domain";
import { environments } from "./environment";
import { gitea } from "./gitea"; import { gitea } from "./gitea";
import { github } from "./github"; import { github } from "./github";
import { gitlab } from "./gitlab"; import { gitlab } from "./gitlab";
@@ -179,9 +180,9 @@ export const applications = pgTable("application", {
registryId: text("registryId").references(() => registry.registryId, { registryId: text("registryId").references(() => registry.registryId, {
onDelete: "set null", onDelete: "set null",
}), }),
projectId: text("projectId") environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
githubId: text("githubId").references(() => github.githubId, { githubId: text("githubId").references(() => github.githubId, {
onDelete: "set null", onDelete: "set null",
}), }),
@@ -202,9 +203,9 @@ export const applications = pgTable("application", {
export const applicationsRelations = relations( export const applicationsRelations = relations(
applications, applications,
({ one, many }) => ({ ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [applications.projectId], fields: [applications.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
deployments: many(deployments), deployments: many(deployments),
customGitSSHKey: one(sshKeys, { customGitSSHKey: one(sshKeys, {
@@ -273,7 +274,7 @@ const createSchema = createInsertSchema(applications, {
customGitBuildPath: z.string().optional(), customGitBuildPath: z.string().optional(),
customGitUrl: z.string().optional(), customGitUrl: z.string().optional(),
buildPath: z.string().optional(), buildPath: z.string().optional(),
projectId: z.string(), environmentId: z.string(),
sourceType: z sourceType: z
.enum(["github", "docker", "git", "gitlab", "bitbucket", "gitea", "drop"]) .enum(["github", "docker", "git", "gitlab", "bitbucket", "gitea", "drop"])
.optional(), .optional(),
@@ -317,7 +318,7 @@ export const apiCreateApplication = createSchema.pick({
name: true, name: true,
appName: true, appName: true,
description: true, description: true,
projectId: true, environmentId: true,
serverId: true, serverId: true,
}); });
+9 -8
View File
@@ -7,6 +7,7 @@ import { backups } from "./backups";
import { bitbucket } from "./bitbucket"; import { bitbucket } from "./bitbucket";
import { deployments } from "./deployment"; import { deployments } from "./deployment";
import { domains } from "./domain"; import { domains } from "./domain";
import { environments } from "./environment";
import { gitea } from "./gitea"; import { gitea } from "./gitea";
import { github } from "./github"; import { github } from "./github";
import { gitlab } from "./gitlab"; import { gitlab } from "./gitlab";
@@ -84,9 +85,9 @@ export const compose = pgTable("compose", {
.default(false), .default(false),
triggerType: triggerType("triggerType").default("push"), triggerType: triggerType("triggerType").default("push"),
composeStatus: applicationStatus("composeStatus").notNull().default("idle"), composeStatus: applicationStatus("composeStatus").notNull().default("idle"),
projectId: text("projectId") environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
createdAt: text("createdAt") createdAt: text("createdAt")
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
@@ -109,9 +110,9 @@ export const compose = pgTable("compose", {
}); });
export const composeRelations = relations(compose, ({ one, many }) => ({ export const composeRelations = relations(compose, ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [compose.projectId], fields: [compose.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
deployments: many(deployments), deployments: many(deployments),
mounts: many(mounts), mounts: many(mounts),
@@ -149,7 +150,7 @@ const createSchema = createInsertSchema(compose, {
description: z.string(), description: z.string(),
env: z.string().optional(), env: z.string().optional(),
composeFile: z.string().optional(), composeFile: z.string().optional(),
projectId: z.string(), environmentId: z.string(),
customGitSSHKeyId: z.string().optional(), customGitSSHKeyId: z.string().optional(),
command: z.string().optional(), command: z.string().optional(),
composePath: z.string().min(1), composePath: z.string().min(1),
@@ -160,7 +161,7 @@ const createSchema = createInsertSchema(compose, {
export const apiCreateCompose = createSchema.pick({ export const apiCreateCompose = createSchema.pick({
name: true, name: true,
description: true, description: true,
projectId: true, environmentId: true,
composeType: true, composeType: true,
appName: true, appName: true,
serverId: true, serverId: true,
@@ -169,7 +170,7 @@ export const apiCreateCompose = createSchema.pick({
export const apiCreateComposeByTemplate = createSchema export const apiCreateComposeByTemplate = createSchema
.pick({ .pick({
projectId: true, environmentId: true,
}) })
.extend({ .extend({
id: z.string().min(1), id: z.string().min(1),
@@ -0,0 +1,85 @@
import { relations } from "drizzle-orm";
import { pgTable, text } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { applications } from "./application";
import { compose } from "./compose";
import { mariadb } from "./mariadb";
import { mongo } from "./mongo";
import { mysql } from "./mysql";
import { postgres } from "./postgres";
import { projects } from "./project";
import { redis } from "./redis";
export const environments = pgTable("environment", {
environmentId: text("environmentId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
name: text("name").notNull(),
description: text("description"),
createdAt: text("createdAt")
.notNull()
.$defaultFn(() => new Date().toISOString()),
env: text("env").notNull().default(""),
projectId: text("projectId")
.notNull()
.references(() => projects.projectId, { onDelete: "cascade" }),
});
export const environmentRelations = relations(
environments,
({ one, many }) => ({
project: one(projects, {
fields: [environments.projectId],
references: [projects.projectId],
}),
applications: many(applications),
mariadb: many(mariadb),
postgres: many(postgres),
mysql: many(mysql),
redis: many(redis),
mongo: many(mongo),
compose: many(compose),
}),
);
const createSchema = createInsertSchema(environments, {
environmentId: z.string().min(1),
name: z.string().min(1),
description: z.string().optional(),
});
export const apiCreateEnvironment = createSchema.pick({
name: true,
description: true,
projectId: true,
});
export const apiFindOneEnvironment = createSchema
.pick({
environmentId: true,
})
.required();
export const apiRemoveEnvironment = createSchema
.pick({
environmentId: true,
})
.required();
export const apiUpdateEnvironment = createSchema.partial().extend({
environmentId: z.string().min(1),
});
export const apiDuplicateEnvironment = createSchema
.pick({
environmentId: true,
name: true,
description: true,
})
.required({
environmentId: true,
name: true,
});
+1
View File
@@ -8,6 +8,7 @@ export * from "./compose";
export * from "./deployment"; export * from "./deployment";
export * from "./destination"; export * from "./destination";
export * from "./domain"; export * from "./domain";
export * from "./environment";
export * from "./git-provider"; export * from "./git-provider";
export * from "./gitea"; export * from "./gitea";
export * from "./github"; export * from "./github";
+9 -7
View File
@@ -4,6 +4,7 @@ import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { z } from "zod"; import { z } from "zod";
import { backups } from "./backups"; import { backups } from "./backups";
import { environments } from "./environment";
import { mounts } from "./mount"; import { mounts } from "./mount";
import { projects } from "./project"; import { projects } from "./project";
import { server } from "./server"; import { server } from "./server";
@@ -66,18 +67,19 @@ export const mariadb = pgTable("mariadb", {
createdAt: text("createdAt") createdAt: text("createdAt")
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
projectId: text("projectId")
environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
serverId: text("serverId").references(() => server.serverId, { serverId: text("serverId").references(() => server.serverId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
}); });
export const mariadbRelations = relations(mariadb, ({ one, many }) => ({ export const mariadbRelations = relations(mariadb, ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [mariadb.projectId], fields: [mariadb.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
backups: many(backups), backups: many(backups),
mounts: many(mounts), mounts: many(mounts),
@@ -115,7 +117,7 @@ const createSchema = createInsertSchema(mariadb, {
memoryLimit: z.string().optional(), memoryLimit: z.string().optional(),
cpuReservation: z.string().optional(), cpuReservation: z.string().optional(),
cpuLimit: z.string().optional(), cpuLimit: z.string().optional(),
projectId: z.string(), environmentId: z.string(),
applicationStatus: z.enum(["idle", "running", "done", "error"]), applicationStatus: z.enum(["idle", "running", "done", "error"]),
externalPort: z.number(), externalPort: z.number(),
description: z.string().optional(), description: z.string().optional(),
@@ -136,7 +138,7 @@ export const apiCreateMariaDB = createSchema
appName: true, appName: true,
dockerImage: true, dockerImage: true,
databaseRootPassword: true, databaseRootPassword: true,
projectId: true, environmentId: true,
description: true, description: true,
databaseName: true, databaseName: true,
databaseUser: true, databaseUser: true,
+9 -7
View File
@@ -4,6 +4,7 @@ import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { z } from "zod"; import { z } from "zod";
import { backups } from "./backups"; import { backups } from "./backups";
import { environments } from "./environment";
import { mounts } from "./mount"; import { mounts } from "./mount";
import { projects } from "./project"; import { projects } from "./project";
import { server } from "./server"; import { server } from "./server";
@@ -62,9 +63,10 @@ export const mongo = pgTable("mongo", {
createdAt: text("createdAt") createdAt: text("createdAt")
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
projectId: text("projectId")
environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
serverId: text("serverId").references(() => server.serverId, { serverId: text("serverId").references(() => server.serverId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
@@ -72,9 +74,9 @@ export const mongo = pgTable("mongo", {
}); });
export const mongoRelations = relations(mongo, ({ one, many }) => ({ export const mongoRelations = relations(mongo, ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [mongo.projectId], fields: [mongo.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
backups: many(backups), backups: many(backups),
mounts: many(mounts), mounts: many(mounts),
@@ -104,7 +106,7 @@ const createSchema = createInsertSchema(mongo, {
memoryLimit: z.string().optional(), memoryLimit: z.string().optional(),
cpuReservation: z.string().optional(), cpuReservation: z.string().optional(),
cpuLimit: z.string().optional(), cpuLimit: z.string().optional(),
projectId: z.string(), environmentId: z.string(),
applicationStatus: z.enum(["idle", "running", "done", "error"]), applicationStatus: z.enum(["idle", "running", "done", "error"]),
externalPort: z.number(), externalPort: z.number(),
description: z.string().optional(), description: z.string().optional(),
@@ -125,7 +127,7 @@ export const apiCreateMongo = createSchema
name: true, name: true,
appName: true, appName: true,
dockerImage: true, dockerImage: true,
projectId: true, environmentId: true,
description: true, description: true,
databaseUser: true, databaseUser: true,
databasePassword: true, databasePassword: true,
+8 -7
View File
@@ -4,6 +4,7 @@ import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { z } from "zod"; import { z } from "zod";
import { backups } from "./backups"; import { backups } from "./backups";
import { environments } from "./environment";
import { mounts } from "./mount"; import { mounts } from "./mount";
import { projects } from "./project"; import { projects } from "./project";
import { server } from "./server"; import { server } from "./server";
@@ -64,18 +65,19 @@ export const mysql = pgTable("mysql", {
createdAt: text("createdAt") createdAt: text("createdAt")
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
projectId: text("projectId")
environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
serverId: text("serverId").references(() => server.serverId, { serverId: text("serverId").references(() => server.serverId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
}); });
export const mysqlRelations = relations(mysql, ({ one, many }) => ({ export const mysqlRelations = relations(mysql, ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [mysql.projectId], fields: [mysql.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
backups: many(backups), backups: many(backups),
mounts: many(mounts), mounts: many(mounts),
@@ -113,7 +115,6 @@ const createSchema = createInsertSchema(mysql, {
memoryLimit: z.string().optional(), memoryLimit: z.string().optional(),
cpuReservation: z.string().optional(), cpuReservation: z.string().optional(),
cpuLimit: z.string().optional(), cpuLimit: z.string().optional(),
projectId: z.string(),
applicationStatus: z.enum(["idle", "running", "done", "error"]), applicationStatus: z.enum(["idle", "running", "done", "error"]),
externalPort: z.number(), externalPort: z.number(),
description: z.string().optional(), description: z.string().optional(),
@@ -133,7 +134,7 @@ export const apiCreateMySql = createSchema
name: true, name: true,
appName: true, appName: true,
dockerImage: true, dockerImage: true,
projectId: true, environmentId: true,
description: true, description: true,
databaseName: true, databaseName: true,
databaseUser: true, databaseUser: true,
+9 -7
View File
@@ -4,6 +4,7 @@ import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { z } from "zod"; import { z } from "zod";
import { backups } from "./backups"; import { backups } from "./backups";
import { environments } from "./environment";
import { mounts } from "./mount"; import { mounts } from "./mount";
import { projects } from "./project"; import { projects } from "./project";
import { server } from "./server"; import { server } from "./server";
@@ -64,18 +65,19 @@ export const postgres = pgTable("postgres", {
createdAt: text("createdAt") createdAt: text("createdAt")
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
projectId: text("projectId")
environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
serverId: text("serverId").references(() => server.serverId, { serverId: text("serverId").references(() => server.serverId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
}); });
export const postgresRelations = relations(postgres, ({ one, many }) => ({ export const postgresRelations = relations(postgres, ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [postgres.projectId], fields: [postgres.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
backups: many(backups), backups: many(backups),
mounts: many(mounts), mounts: many(mounts),
@@ -104,7 +106,7 @@ const createSchema = createInsertSchema(postgres, {
memoryLimit: z.string().optional(), memoryLimit: z.string().optional(),
cpuReservation: z.string().optional(), cpuReservation: z.string().optional(),
cpuLimit: z.string().optional(), cpuLimit: z.string().optional(),
projectId: z.string(), environmentId: z.string(),
applicationStatus: z.enum(["idle", "running", "done", "error"]), applicationStatus: z.enum(["idle", "running", "done", "error"]),
externalPort: z.number(), externalPort: z.number(),
createdAt: z.string(), createdAt: z.string(),
@@ -128,7 +130,7 @@ export const apiCreatePostgres = createSchema
databaseUser: true, databaseUser: true,
databasePassword: true, databasePassword: true,
dockerImage: true, dockerImage: true,
projectId: true, environmentId: true,
description: true, description: true,
serverId: true, serverId: true,
}) })
+2 -14
View File
@@ -4,13 +4,7 @@ import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { z } from "zod"; import { z } from "zod";
import { organization } from "./account"; import { organization } from "./account";
import { applications } from "./application"; import { environments } from "./environment";
import { compose } from "./compose";
import { mariadb } from "./mariadb";
import { mongo } from "./mongo";
import { mysql } from "./mysql";
import { postgres } from "./postgres";
import { redis } from "./redis";
export const projects = pgTable("project", { export const projects = pgTable("project", {
projectId: text("projectId") projectId: text("projectId")
@@ -30,13 +24,7 @@ export const projects = pgTable("project", {
}); });
export const projectRelations = relations(projects, ({ many, one }) => ({ export const projectRelations = relations(projects, ({ many, one }) => ({
mysql: many(mysql), environments: many(environments),
postgres: many(postgres),
mariadb: many(mariadb),
applications: many(applications),
mongo: many(mongo),
redis: many(redis),
compose: many(compose),
organization: one(organization, { organization: one(organization, {
fields: [projects.organizationId], fields: [projects.organizationId],
references: [organization.id], references: [organization.id],
+9 -7
View File
@@ -3,6 +3,7 @@ import { integer, json, pgTable, text } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod"; import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { z } from "zod"; import { z } from "zod";
import { environments } from "./environment";
import { mounts } from "./mount"; import { mounts } from "./mount";
import { projects } from "./project"; import { projects } from "./project";
import { server } from "./server"; import { server } from "./server";
@@ -60,18 +61,19 @@ export const redis = pgTable("redis", {
labelsSwarm: json("labelsSwarm").$type<LabelsSwarm>(), labelsSwarm: json("labelsSwarm").$type<LabelsSwarm>(),
networkSwarm: json("networkSwarm").$type<NetworkSwarm[]>(), networkSwarm: json("networkSwarm").$type<NetworkSwarm[]>(),
replicas: integer("replicas").default(1).notNull(), replicas: integer("replicas").default(1).notNull(),
projectId: text("projectId")
environmentId: text("environmentId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => environments.environmentId, { onDelete: "cascade" }),
serverId: text("serverId").references(() => server.serverId, { serverId: text("serverId").references(() => server.serverId, {
onDelete: "cascade", onDelete: "cascade",
}), }),
}); });
export const redisRelations = relations(redis, ({ one, many }) => ({ export const redisRelations = relations(redis, ({ one, many }) => ({
project: one(projects, { environment: one(environments, {
fields: [redis.projectId], fields: [redis.environmentId],
references: [projects.projectId], references: [environments.environmentId],
}), }),
mounts: many(mounts), mounts: many(mounts),
server: one(server, { server: one(server, {
@@ -93,7 +95,7 @@ const createSchema = createInsertSchema(redis, {
memoryLimit: z.string().optional(), memoryLimit: z.string().optional(),
cpuReservation: z.string().optional(), cpuReservation: z.string().optional(),
cpuLimit: z.string().optional(), cpuLimit: z.string().optional(),
projectId: z.string(), environmentId: z.string(),
applicationStatus: z.enum(["idle", "running", "done", "error"]), applicationStatus: z.enum(["idle", "running", "done", "error"]),
externalPort: z.number(), externalPort: z.number(),
description: z.string().optional(), description: z.string().optional(),
@@ -114,7 +116,7 @@ export const apiCreateRedis = createSchema
appName: true, appName: true,
databasePassword: true, databasePassword: true,
dockerImage: true, dockerImage: true,
projectId: true, environmentId: true,
description: true, description: true,
serverId: true, serverId: true,
}) })
+3 -1
View File
@@ -27,7 +27,9 @@ export const rollbacks = pgTable("rollback", {
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
fullContext: jsonb("fullContext").$type< fullContext: jsonb("fullContext").$type<
Application & { Application & {
project: Project; environment: {
project: Project;
};
mounts: Mount[]; mounts: Mount[];
ports: Port[]; ports: Port[];
registry?: Registry | null; registry?: Registry | null;
+1
View File
@@ -175,6 +175,7 @@ export const apiAssignPermissions = createSchema
}) })
.extend({ .extend({
accessedProjects: z.array(z.string()).optional(), accessedProjects: z.array(z.string()).optional(),
accessedEnvironments: z.array(z.string()).optional(),
accessedServices: z.array(z.string()).optional(), accessedServices: z.array(z.string()).optional(),
canCreateProjects: z.boolean().optional(), canCreateProjects: z.boolean().optional(),
canCreateServices: z.boolean().optional(), canCreateServices: z.boolean().optional(),
+1
View File
@@ -16,6 +16,7 @@ export * from "./services/deployment";
export * from "./services/destination"; export * from "./services/destination";
export * from "./services/docker"; export * from "./services/docker";
export * from "./services/domain"; export * from "./services/domain";
export * from "./services/environment";
export * from "./services/git-provider"; export * from "./services/git-provider";
export * from "./services/gitea"; export * from "./services/gitea";
export * from "./services/github"; export * from "./services/github";
+15 -11
View File
@@ -105,7 +105,11 @@ export const findApplicationById = async (applicationId: string) => {
const application = await db.query.applications.findFirst({ const application = await db.query.applications.findFirst({
where: eq(applications.applicationId, applicationId), where: eq(applications.applicationId, applicationId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
domains: true, domains: true,
deployments: true, deployments: true,
mounts: true, mounts: true,
@@ -180,7 +184,7 @@ export const deployApplication = async ({
}) => { }) => {
const application = await findApplicationById(applicationId); const application = await findApplicationById(applicationId);
const buildLink = `${await getDokployUrl()}/dashboard/project/${application.projectId}/services/application/${application.applicationId}?tab=deployments`; const buildLink = `${await getDokployUrl()}/dashboard/project/${application.environment.projectId}/environment/${application.environmentId}/services/application/${application.applicationId}?tab=deployments`;
const deployment = await createDeployment({ const deployment = await createDeployment({
applicationId: applicationId, applicationId: applicationId,
title: titleLog, title: titleLog,
@@ -227,11 +231,11 @@ export const deployApplication = async ({
} }
await sendBuildSuccessNotifications({ await sendBuildSuccessNotifications({
projectName: application.project.name, projectName: application.environment.project.name,
applicationName: application.name, applicationName: application.name,
applicationType: "application", applicationType: "application",
buildLink, buildLink,
organizationId: application.project.organizationId, organizationId: application.environment.project.organizationId,
domains: application.domains, domains: application.domains,
}); });
} catch (error) { } catch (error) {
@@ -239,13 +243,13 @@ export const deployApplication = async ({
await updateApplicationStatus(applicationId, "error"); await updateApplicationStatus(applicationId, "error");
await sendBuildErrorNotifications({ await sendBuildErrorNotifications({
projectName: application.project.name, projectName: application.environment.project.name,
applicationName: application.name, applicationName: application.name,
applicationType: "application", applicationType: "application",
// @ts-ignore // @ts-ignore
errorMessage: error?.message || "Error building", errorMessage: error?.message || "Error building",
buildLink, buildLink,
organizationId: application.project.organizationId, organizationId: application.environment.project.organizationId,
}); });
throw error; throw error;
@@ -307,7 +311,7 @@ export const deployRemoteApplication = async ({
}) => { }) => {
const application = await findApplicationById(applicationId); const application = await findApplicationById(applicationId);
const buildLink = `${await getDokployUrl()}/dashboard/project/${application.projectId}/services/application/${application.applicationId}?tab=deployments`; const buildLink = `${await getDokployUrl()}/dashboard/project/${application.environment.projectId}/environment/${application.environmentId}/services/application/${application.applicationId}?tab=deployments`;
const deployment = await createDeployment({ const deployment = await createDeployment({
applicationId: applicationId, applicationId: applicationId,
title: titleLog, title: titleLog,
@@ -363,11 +367,11 @@ export const deployRemoteApplication = async ({
} }
await sendBuildSuccessNotifications({ await sendBuildSuccessNotifications({
projectName: application.project.name, projectName: application.environment.project.name,
applicationName: application.name, applicationName: application.name,
applicationType: "application", applicationType: "application",
buildLink, buildLink,
organizationId: application.project.organizationId, organizationId: application.environment.project.organizationId,
domains: application.domains, domains: application.domains,
}); });
} catch (error) { } catch (error) {
@@ -387,12 +391,12 @@ export const deployRemoteApplication = async ({
await updateApplicationStatus(applicationId, "error"); await updateApplicationStatus(applicationId, "error");
await sendBuildErrorNotifications({ await sendBuildErrorNotifications({
projectName: application.project.name, projectName: application.environment.project.name,
applicationName: application.name, applicationName: application.name,
applicationType: "application", applicationType: "application",
errorMessage: `Please check the logs for details: ${errorMessage}`, errorMessage: `Please check the logs for details: ${errorMessage}`,
buildLink, buildLink,
organizationId: application.project.organizationId, organizationId: application.environment.project.organizationId,
}); });
throw error; throw error;
+15 -11
View File
@@ -126,7 +126,11 @@ export const findComposeById = async (composeId: string) => {
const result = await db.query.compose.findFirst({ const result = await db.query.compose.findFirst({
where: eq(compose.composeId, composeId), where: eq(compose.composeId, composeId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
deployments: true, deployments: true,
mounts: true, mounts: true,
domains: true, domains: true,
@@ -222,7 +226,7 @@ export const deployCompose = async ({
const compose = await findComposeById(composeId); const compose = await findComposeById(composeId);
const buildLink = `${await getDokployUrl()}/dashboard/project/${ const buildLink = `${await getDokployUrl()}/dashboard/project/${
compose.projectId compose.environment.projectId
}/services/compose/${compose.composeId}?tab=deployments`; }/services/compose/${compose.composeId}?tab=deployments`;
const deployment = await createDeploymentCompose({ const deployment = await createDeploymentCompose({
composeId: composeId, composeId: composeId,
@@ -255,11 +259,11 @@ export const deployCompose = async ({
}); });
await sendBuildSuccessNotifications({ await sendBuildSuccessNotifications({
projectName: compose.project.name, projectName: compose.environment.project.name,
applicationName: compose.name, applicationName: compose.name,
applicationType: "compose", applicationType: "compose",
buildLink, buildLink,
organizationId: compose.project.organizationId, organizationId: compose.environment.project.organizationId,
domains: compose.domains, domains: compose.domains,
}); });
} catch (error) { } catch (error) {
@@ -268,13 +272,13 @@ export const deployCompose = async ({
composeStatus: "error", composeStatus: "error",
}); });
await sendBuildErrorNotifications({ await sendBuildErrorNotifications({
projectName: compose.project.name, projectName: compose.environment.project.name,
applicationName: compose.name, applicationName: compose.name,
applicationType: "compose", applicationType: "compose",
// @ts-ignore // @ts-ignore
errorMessage: error?.message || "Error building", errorMessage: error?.message || "Error building",
buildLink, buildLink,
organizationId: compose.project.organizationId, organizationId: compose.environment.project.organizationId,
}); });
throw error; throw error;
} }
@@ -330,7 +334,7 @@ export const deployRemoteCompose = async ({
const compose = await findComposeById(composeId); const compose = await findComposeById(composeId);
const buildLink = `${await getDokployUrl()}/dashboard/project/${ const buildLink = `${await getDokployUrl()}/dashboard/project/${
compose.projectId compose.environment.projectId
}/services/compose/${compose.composeId}?tab=deployments`; }/services/compose/${compose.composeId}?tab=deployments`;
const deployment = await createDeploymentCompose({ const deployment = await createDeploymentCompose({
composeId: composeId, composeId: composeId,
@@ -387,11 +391,11 @@ export const deployRemoteCompose = async ({
}); });
await sendBuildSuccessNotifications({ await sendBuildSuccessNotifications({
projectName: compose.project.name, projectName: compose.environment.project.name,
applicationName: compose.name, applicationName: compose.name,
applicationType: "compose", applicationType: "compose",
buildLink, buildLink,
organizationId: compose.project.organizationId, organizationId: compose.environment.project.organizationId,
domains: compose.domains, domains: compose.domains,
}); });
} catch (error) { } catch (error) {
@@ -410,13 +414,13 @@ export const deployRemoteCompose = async ({
composeStatus: "error", composeStatus: "error",
}); });
await sendBuildErrorNotifications({ await sendBuildErrorNotifications({
projectName: compose.project.name, projectName: compose.environment.project.name,
applicationName: compose.name, applicationName: compose.name,
applicationType: "compose", applicationType: "compose",
// @ts-ignore // @ts-ignore
errorMessage: error?.message || "Error building", errorMessage: error?.message || "Error building",
buildLink, buildLink,
organizationId: compose.project.organizationId, organizationId: compose.environment.project.organizationId,
}); });
throw error; throw error;
} }
+140
View File
@@ -0,0 +1,140 @@
import { db } from "@dokploy/server/db";
import {
type apiCreateEnvironment,
type apiDuplicateEnvironment,
environments,
} from "@dokploy/server/db/schema";
import { TRPCError } from "@trpc/server";
import { asc, eq } from "drizzle-orm";
export type Environment = typeof environments.$inferSelect;
export const createEnvironment = async (
input: typeof apiCreateEnvironment._type,
) => {
const newEnvironment = await db
.insert(environments)
.values({
...input,
})
.returning()
.then((value) => value[0]);
if (!newEnvironment) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error creating the environment",
});
}
return newEnvironment;
};
export const findEnvironmentById = async (environmentId: string) => {
const environment = await db.query.environments.findFirst({
where: eq(environments.environmentId, environmentId),
with: {
applications: true,
mariadb: true,
mongo: true,
mysql: true,
postgres: true,
redis: true,
compose: true,
project: true,
},
});
if (!environment) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Environment not found",
});
}
return environment;
};
export const findEnvironmentsByProjectId = async (projectId: string) => {
const projectEnvironments = await db.query.environments.findMany({
where: eq(environments.projectId, projectId),
orderBy: asc(environments.createdAt),
with: {
applications: true,
mariadb: true,
mongo: true,
mysql: true,
postgres: true,
redis: true,
compose: true,
project: true,
},
});
return projectEnvironments;
};
export const deleteEnvironment = async (environmentId: string) => {
const currentEnvironment = await findEnvironmentById(environmentId);
if (currentEnvironment.name === "production") {
throw new TRPCError({
code: "BAD_REQUEST",
message: "You cannot delete the production environment",
});
}
const deletedEnvironment = await db
.delete(environments)
.where(eq(environments.environmentId, environmentId))
.returning()
.then((value) => value[0]);
return deletedEnvironment;
};
export const updateEnvironmentById = async (
environmentId: string,
environmentData: Partial<Environment>,
) => {
const result = await db
.update(environments)
.set({
...environmentData,
})
.where(eq(environments.environmentId, environmentId))
.returning()
.then((res) => res[0]);
return result;
};
export const duplicateEnvironment = async (
input: typeof apiDuplicateEnvironment._type,
) => {
// Find the original environment
const originalEnvironment = await findEnvironmentById(input.environmentId);
// Create a new environment with the provided name and description
const newEnvironment = await db
.insert(environments)
.values({
name: input.name,
description: input.description || originalEnvironment.description,
projectId: originalEnvironment.projectId,
})
.returning()
.then((value) => value[0]);
if (!newEnvironment) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error duplicating the environment",
});
}
return newEnvironment;
};
export const createProductionEnvironment = async (projectId: string) => {
return createEnvironment({
name: "production",
description: "Production environment",
projectId,
});
};
+5 -1
View File
@@ -56,7 +56,11 @@ export const findMariadbById = async (mariadbId: string) => {
const result = await db.query.mariadb.findFirst({ const result = await db.query.mariadb.findFirst({
where: eq(mariadb.mariadbId, mariadbId), where: eq(mariadb.mariadbId, mariadbId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
mounts: true, mounts: true,
server: true, server: true,
backups: { backups: {
+5 -1
View File
@@ -53,7 +53,11 @@ export const findMongoById = async (mongoId: string) => {
const result = await db.query.mongo.findFirst({ const result = await db.query.mongo.findFirst({
where: eq(mongo.mongoId, mongoId), where: eq(mongo.mongoId, mongoId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
mounts: true, mounts: true,
server: true, server: true,
backups: { backups: {
+91 -7
View File
@@ -105,13 +105,69 @@ export const findMountById = async (mountId: string) => {
const mount = await db.query.mounts.findFirst({ const mount = await db.query.mounts.findFirst({
where: eq(mounts.mountId, mountId), where: eq(mounts.mountId, mountId),
with: { with: {
application: true, application: {
postgres: true, with: {
mariadb: true, environment: {
mongo: true, with: {
mysql: true, project: true,
redis: true, },
compose: true, },
},
},
postgres: {
with: {
environment: {
with: {
project: true,
},
},
},
},
mariadb: {
with: {
environment: {
with: {
project: true,
},
},
},
},
mongo: {
with: {
environment: {
with: {
project: true,
},
},
},
},
mysql: {
with: {
environment: {
with: {
project: true,
},
},
},
},
redis: {
with: {
environment: {
with: {
project: true,
},
},
},
},
compose: {
with: {
environment: {
with: {
project: true,
},
},
},
},
}, },
}); });
if (!mount) { if (!mount) {
@@ -123,6 +179,34 @@ export const findMountById = async (mountId: string) => {
return mount; return mount;
}; };
export const findMountOrganizationId = async (mountId: string) => {
const mount = await findMountById(mountId);
if (mount.application) {
return mount.application.environment.project.organizationId;
}
if (mount.postgres) {
return mount.postgres.environment.project.organizationId;
}
if (mount.mariadb) {
return mount.mariadb.environment.project.organizationId;
}
if (mount.mongo) {
return mount.mongo.environment.project.organizationId;
}
if (mount.mysql) {
return mount.mysql.environment.project.organizationId;
}
if (mount.redis) {
return mount.redis.environment.project.organizationId;
}
if (mount.compose) {
return mount.compose.environment.project.organizationId;
}
return null;
};
export const updateMount = async ( export const updateMount = async (
mountId: string, mountId: string,
mountData: Partial<Mount>, mountData: Partial<Mount>,
+5 -1
View File
@@ -56,7 +56,11 @@ export const findMySqlById = async (mysqlId: string) => {
const result = await db.query.mysql.findFirst({ const result = await db.query.mysql.findFirst({
where: eq(mysql.mysqlId, mysqlId), where: eq(mysql.mysqlId, mysqlId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
mounts: true, mounts: true,
server: true, server: true,
backups: { backups: {
+11
View File
@@ -27,6 +27,17 @@ export const createPort = async (input: typeof apiCreatePort._type) => {
export const finPortById = async (portId: string) => { export const finPortById = async (portId: string) => {
const result = await db.query.ports.findFirst({ const result = await db.query.ports.findFirst({
where: eq(ports.portId, portId), where: eq(ports.portId, portId),
with: {
application: {
with: {
environment: {
with: {
project: true,
},
},
},
},
},
}); });
if (!result) { if (!result) {
throw new TRPCError({ throw new TRPCError({
+5 -1
View File
@@ -51,7 +51,11 @@ export const findPostgresById = async (postgresId: string) => {
const result = await db.query.postgres.findFirst({ const result = await db.query.postgres.findFirst({
where: eq(postgres.postgresId, postgresId), where: eq(postgres.postgresId, postgresId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
mounts: true, mounts: true,
server: true, server: true,
backups: { backups: {
@@ -31,7 +31,11 @@ export const findPreviewDeploymentById = async (
application: { application: {
with: { with: {
server: true, server: true,
project: true, environment: {
with: {
project: true,
},
},
}, },
}, },
}, },
@@ -45,37 +49,6 @@ export const findPreviewDeploymentById = async (
return application; return application;
}; };
export const findApplicationByPreview = async (applicationId: string) => {
const application = await db.query.applications.findFirst({
with: {
previewDeployments: {
where: eq(previewDeployments.applicationId, applicationId),
},
project: true,
domains: true,
deployments: true,
mounts: true,
redirects: true,
security: true,
ports: true,
registry: true,
gitlab: true,
github: true,
bitbucket: true,
gitea: true,
server: true,
},
});
if (!application) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Application not found",
});
}
return application;
};
export const removePreviewDeployment = async (previewDeploymentId: string) => { export const removePreviewDeployment = async (previewDeploymentId: string) => {
try { try {
const previewDeployment = const previewDeployment =
@@ -163,7 +136,7 @@ export const createPreviewDeployment = async (
const appName = `preview-${application.appName}-${generatePassword(6)}`; const appName = `preview-${application.appName}-${generatePassword(6)}`;
const org = await db.query.organization.findFirst({ const org = await db.query.organization.findFirst({
where: eq(organization.id, application.project.organizationId), where: eq(organization.id, application.environment.project.organizationId),
}); });
const generateDomain = await generateWildcardDomain( const generateDomain = await generateWildcardDomain(
application.previewWildcard || "*.traefik.me", application.previewWildcard || "*.traefik.me",
+21 -9
View File
@@ -11,6 +11,7 @@ import {
} from "@dokploy/server/db/schema"; } from "@dokploy/server/db/schema";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { createProductionEnvironment } from "./environment";
export type Project = typeof projects.$inferSelect; export type Project = typeof projects.$inferSelect;
@@ -34,20 +35,31 @@ export const createProject = async (
}); });
} }
return newProject; // Automatically create a production environment
const newEnvironment = await createProductionEnvironment(
newProject.projectId,
);
return {
project: newProject,
environment: newEnvironment,
};
}; };
export const findProjectById = async (projectId: string) => { export const findProjectById = async (projectId: string) => {
const project = await db.query.projects.findFirst({ const project = await db.query.projects.findFirst({
where: eq(projects.projectId, projectId), where: eq(projects.projectId, projectId),
with: { with: {
applications: true, environments: {
mariadb: true, with: {
mongo: true, applications: true,
mysql: true, mariadb: true,
postgres: true, mongo: true,
redis: true, mysql: true,
compose: true, postgres: true,
redis: true,
compose: true,
},
},
}, },
}); });
if (!project) { if (!project) {
@@ -86,7 +98,7 @@ export const updateProjectById = async (
}; };
export const validUniqueServerAppName = async (appName: string) => { export const validUniqueServerAppName = async (appName: string) => {
const query = await db.query.projects.findMany({ const query = await db.query.environments.findMany({
with: { with: {
applications: { applications: {
where: eq(applications.appName, appName), where: eq(applications.appName, appName),
+5 -1
View File
@@ -52,7 +52,11 @@ export const findRedisById = async (redisId: string) => {
const result = await db.query.redis.findFirst({ const result = await db.query.redis.findFirst({
where: eq(redis.redisId, redisId), where: eq(redis.redisId, redisId),
with: { with: {
project: true, environment: {
with: {
project: true,
},
},
mounts: true, mounts: true,
server: true, server: true,
}, },
+20 -3
View File
@@ -76,9 +76,24 @@ export const createRollback = async (
}); });
}; };
const findRollbackById = async (rollbackId: string) => { export const findRollbackById = async (rollbackId: string) => {
const result = await db.query.rollbacks.findFirst({ const result = await db.query.rollbacks.findFirst({
where: eq(rollbacks.rollbackId, rollbackId), where: eq(rollbacks.rollbackId, rollbackId),
with: {
deployment: {
with: {
application: {
with: {
environment: {
with: {
project: true,
},
},
},
},
},
},
},
}); });
if (!result) { if (!result) {
@@ -179,7 +194,9 @@ const rollbackApplication = async (
image: string, image: string,
serverId?: string | null, serverId?: string | null,
fullContext?: Application & { fullContext?: Application & {
project: Project; environment: {
project: Project;
};
mounts: Mount[]; mounts: Mount[];
ports: Port[]; ports: Port[];
}, },
@@ -225,7 +242,7 @@ const rollbackApplication = async (
const bindsMount = generateBindMounts(mounts); const bindsMount = generateBindMounts(mounts);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
fullContext.project.env, fullContext.environment.project.env,
); );
// For rollback, we use the provided image instead of calculating it // For rollback, we use the provided image instead of calculating it
+38 -3
View File
@@ -35,9 +35,29 @@ export const findScheduleById = async (scheduleId: string) => {
const schedule = await db.query.schedules.findFirst({ const schedule = await db.query.schedules.findFirst({
where: eq(schedules.scheduleId, scheduleId), where: eq(schedules.scheduleId, scheduleId),
with: { with: {
application: true, application: {
compose: true, with: {
server: true, environment: {
with: {
project: true,
},
},
},
},
compose: {
with: {
environment: {
with: {
project: true,
},
},
},
},
server: {
with: {
organization: true,
},
},
}, },
}); });
@@ -50,6 +70,21 @@ export const findScheduleById = async (scheduleId: string) => {
return schedule; return schedule;
}; };
export const findScheduleOrganizationId = async (scheduleId: string) => {
const schedule = await findScheduleById(scheduleId);
if (schedule?.application) {
return schedule?.application?.environment?.project?.organizationId;
}
if (schedule?.compose) {
return schedule?.compose?.environment?.project?.organizationId;
}
if (schedule?.server) {
return schedule?.server?.organization?.id;
}
return null;
};
export const deleteSchedule = async (scheduleId: string) => { export const deleteSchedule = async (scheduleId: string) => {
const schedule = await findScheduleById(scheduleId); const schedule = await findScheduleById(scheduleId);
const serverId = const serverId =
+58
View File
@@ -23,6 +23,23 @@ export const addNewProject = async (
); );
}; };
export const addNewEnvironment = async (
userId: string,
environmentId: string,
organizationId: string,
) => {
const userR = await findMemberById(userId, organizationId);
await db
.update(member)
.set({
accessedEnvironments: [...userR.accessedEnvironments, environmentId],
})
.where(
and(eq(member.id, userR.id), eq(member.organizationId, organizationId)),
);
};
export const addNewService = async ( export const addNewService = async (
userId: string, userId: string,
serviceId: string, serviceId: string,
@@ -131,6 +148,21 @@ export const canPerformAccessProject = async (
return false; return false;
}; };
export const canPerformAccessEnvironment = async (
userId: string,
environmentId: string,
organizationId: string,
) => {
const { accessedEnvironments } = await findMemberById(userId, organizationId);
const haveAccessToEnvironment = accessedEnvironments.includes(environmentId);
if (haveAccessToEnvironment) {
return true;
}
return false;
};
export const canAccessToTraefikFiles = async ( export const canAccessToTraefikFiles = async (
userId: string, userId: string,
organizationId: string, organizationId: string,
@@ -182,6 +214,32 @@ export const checkServiceAccess = async (
} }
}; };
export const checkEnvironmentAccess = async (
userId: string,
environmentId: string,
organizationId: string,
action = "access" as const,
) => {
let hasPermission = false;
switch (action) {
case "access":
hasPermission = await canPerformAccessEnvironment(
userId,
environmentId,
organizationId,
);
break;
default:
hasPermission = false;
}
if (!hasPermission) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Permission denied",
});
}
};
export const checkProjectAccess = async ( export const checkProjectAccess = async (
authId: string, authId: string,
action: "create" | "delete" | "access", action: "create" | "delete" | "access",
+4 -2
View File
@@ -4,6 +4,7 @@ import {
createDeploymentBackup, createDeploymentBackup,
updateDeploymentStatus, updateDeploymentStatus,
} from "@dokploy/server/services/deployment"; } from "@dokploy/server/services/deployment";
import { findEnvironmentById } from "@dokploy/server/services/environment";
import { findProjectById } from "@dokploy/server/services/project"; import { findProjectById } from "@dokploy/server/services/project";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup"; import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync, execAsyncRemote } from "../process/execAsync"; import { execAsync, execAsyncRemote } from "../process/execAsync";
@@ -13,8 +14,9 @@ export const runComposeBackup = async (
compose: Compose, compose: Compose,
backup: BackupSchedule, backup: BackupSchedule,
) => { ) => {
const { projectId, name } = compose; const { environmentId, name } = compose;
const project = await findProjectById(projectId); const environment = await findEnvironmentById(environmentId);
const project = await findProjectById(environment.projectId);
const { prefix, databaseType } = backup; const { prefix, databaseType } = backup;
const destination = backup.destination; const destination = backup.destination;
const backupFileName = `${new Date().toISOString()}.sql.gz`; const backupFileName = `${new Date().toISOString()}.sql.gz`;
+4 -2
View File
@@ -4,6 +4,7 @@ import {
updateDeploymentStatus, updateDeploymentStatus,
} from "@dokploy/server/services/deployment"; } from "@dokploy/server/services/deployment";
import type { Mariadb } from "@dokploy/server/services/mariadb"; import type { Mariadb } from "@dokploy/server/services/mariadb";
import { findEnvironmentById } from "@dokploy/server/services/environment";
import { findProjectById } from "@dokploy/server/services/project"; import { findProjectById } from "@dokploy/server/services/project";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup"; import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync, execAsyncRemote } from "../process/execAsync"; import { execAsync, execAsyncRemote } from "../process/execAsync";
@@ -13,8 +14,9 @@ export const runMariadbBackup = async (
mariadb: Mariadb, mariadb: Mariadb,
backup: BackupSchedule, backup: BackupSchedule,
) => { ) => {
const { projectId, name } = mariadb; const { environmentId, name } = mariadb;
const project = await findProjectById(projectId); const environment = await findEnvironmentById(environmentId);
const project = await findProjectById(environment.projectId);
const { prefix } = backup; const { prefix } = backup;
const destination = backup.destination; const destination = backup.destination;
const backupFileName = `${new Date().toISOString()}.sql.gz`; const backupFileName = `${new Date().toISOString()}.sql.gz`;
+4 -2
View File
@@ -4,14 +4,16 @@ import {
updateDeploymentStatus, updateDeploymentStatus,
} from "@dokploy/server/services/deployment"; } from "@dokploy/server/services/deployment";
import type { Mongo } from "@dokploy/server/services/mongo"; import type { Mongo } from "@dokploy/server/services/mongo";
import { findEnvironmentById } from "@dokploy/server/services/environment";
import { findProjectById } from "@dokploy/server/services/project"; import { findProjectById } from "@dokploy/server/services/project";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup"; import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync, execAsyncRemote } from "../process/execAsync"; import { execAsync, execAsyncRemote } from "../process/execAsync";
import { getBackupCommand, getS3Credentials, normalizeS3Path } from "./utils"; import { getBackupCommand, getS3Credentials, normalizeS3Path } from "./utils";
export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => { export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {
const { projectId, name } = mongo; const { environmentId, name } = mongo;
const project = await findProjectById(projectId); const environment = await findEnvironmentById(environmentId);
const project = await findProjectById(environment.projectId);
const { prefix } = backup; const { prefix } = backup;
const destination = backup.destination; const destination = backup.destination;
const backupFileName = `${new Date().toISOString()}.sql.gz`; const backupFileName = `${new Date().toISOString()}.sql.gz`;
+4 -2
View File
@@ -4,14 +4,16 @@ import {
updateDeploymentStatus, updateDeploymentStatus,
} from "@dokploy/server/services/deployment"; } from "@dokploy/server/services/deployment";
import type { MySql } from "@dokploy/server/services/mysql"; import type { MySql } from "@dokploy/server/services/mysql";
import { findEnvironmentById } from "@dokploy/server/services/environment";
import { findProjectById } from "@dokploy/server/services/project"; import { findProjectById } from "@dokploy/server/services/project";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup"; import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync, execAsyncRemote } from "../process/execAsync"; import { execAsync, execAsyncRemote } from "../process/execAsync";
import { getBackupCommand, getS3Credentials, normalizeS3Path } from "./utils"; import { getBackupCommand, getS3Credentials, normalizeS3Path } from "./utils";
export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => { export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
const { projectId, name } = mysql; const { environmentId, name } = mysql;
const project = await findProjectById(projectId); const environment = await findEnvironmentById(environmentId);
const project = await findProjectById(environment.projectId);
const { prefix } = backup; const { prefix } = backup;
const destination = backup.destination; const destination = backup.destination;
const backupFileName = `${new Date().toISOString()}.sql.gz`; const backupFileName = `${new Date().toISOString()}.sql.gz`;
@@ -4,6 +4,7 @@ import {
updateDeploymentStatus, updateDeploymentStatus,
} from "@dokploy/server/services/deployment"; } from "@dokploy/server/services/deployment";
import type { Postgres } from "@dokploy/server/services/postgres"; import type { Postgres } from "@dokploy/server/services/postgres";
import { findEnvironmentById } from "@dokploy/server/services/environment";
import { findProjectById } from "@dokploy/server/services/project"; import { findProjectById } from "@dokploy/server/services/project";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup"; import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync, execAsyncRemote } from "../process/execAsync"; import { execAsync, execAsyncRemote } from "../process/execAsync";
@@ -13,8 +14,9 @@ export const runPostgresBackup = async (
postgres: Postgres, postgres: Postgres,
backup: BackupSchedule, backup: BackupSchedule,
) => { ) => {
const { name, projectId } = postgres; const { name, environmentId } = postgres;
const project = await findProjectById(projectId); const environment = await findEnvironmentById(environmentId);
const project = await findProjectById(environment.projectId);
const deployment = await createDeploymentBackup({ const deployment = await createDeploymentBackup({
backupId: backup.backupId, backupId: backup.backupId,
+10 -5
View File
@@ -22,7 +22,7 @@ import { spawnAsync } from "../process/spawnAsync";
export type ComposeNested = InferResultType< export type ComposeNested = InferResultType<
"compose", "compose",
{ project: true; mounts: true; domains: true } { environment: { with: { project: true } }; mounts: true; domains: true }
>; >;
export const buildCompose = async (compose: ComposeNested, logPath: string) => { export const buildCompose = async (compose: ComposeNested, logPath: string) => {
const writeStream = createWriteStream(logPath, { flags: "a" }); const writeStream = createWriteStream(logPath, { flags: "a" });
@@ -72,7 +72,10 @@ export const buildCompose = async (compose: ComposeNested, logPath: string) => {
NODE_ENV: process.env.NODE_ENV, NODE_ENV: process.env.NODE_ENV,
PATH: process.env.PATH, PATH: process.env.PATH,
...(composeType === "stack" && { ...(composeType === "stack" && {
...getEnviromentVariablesObject(compose.env, compose.project.env), ...getEnviromentVariablesObject(
compose.env,
compose.environment.project.env,
),
}), }),
}, },
}, },
@@ -202,7 +205,8 @@ const createEnvFile = (compose: ComposeNested) => {
const envFileContent = prepareEnvironmentVariables( const envFileContent = prepareEnvironmentVariables(
envContent, envContent,
compose.project.env, compose.environment.project.env,
compose.environment.env,
).join("\n"); ).join("\n");
if (!existsSync(dirname(envFilePath))) { if (!existsSync(dirname(envFilePath))) {
@@ -232,7 +236,8 @@ export const getCreateEnvFileCommand = (compose: ComposeNested) => {
const envFileContent = prepareEnvironmentVariables( const envFileContent = prepareEnvironmentVariables(
envContent, envContent,
compose.project.env, compose.environment.project.env,
compose.environment.env,
).join("\n"); ).join("\n");
const encodedContent = encodeBase64(envFileContent); const encodedContent = encodeBase64(envFileContent);
@@ -247,7 +252,7 @@ const getExportEnvCommand = (compose: ComposeNested) => {
const envVars = getEnviromentVariablesObject( const envVars = getEnviromentVariablesObject(
compose.env, compose.env,
compose.project.env, compose.environment.project.env,
); );
const exports = Object.entries(envVars) const exports = Object.entries(envVars)
.map(([key, value]) => `export ${key}=${JSON.stringify(value)}`) .map(([key, value]) => `export ${key}=${JSON.stringify(value)}`)
@@ -28,7 +28,8 @@ export const buildCustomDocker = async (
dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || "."; dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || ".";
const args = prepareEnvironmentVariables( const args = prepareEnvironmentVariables(
buildArgs, buildArgs,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const dockerContextPath = getDockerContextPath(application); const dockerContextPath = getDockerContextPath(application);
@@ -51,7 +52,12 @@ export const buildCustomDocker = async (
as it could be publicly exposed. as it could be publicly exposed.
*/ */
if (!publishDirectory) { if (!publishDirectory) {
createEnvFile(dockerFilePath, env, application.project.env); createEnvFile(
dockerFilePath,
env,
application.environment.project.env,
application.environment.env,
);
} }
await spawnAsync( await spawnAsync(
@@ -92,7 +98,8 @@ export const getDockerCommand = (
dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || "."; dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || ".";
const args = prepareEnvironmentVariables( const args = prepareEnvironmentVariables(
buildArgs, buildArgs,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const dockerContextPath = const dockerContextPath =
@@ -121,7 +128,8 @@ export const getDockerCommand = (
command += createEnvFileCommand( command += createEnvFileCommand(
dockerFilePath, dockerFilePath,
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
} }
+4 -2
View File
@@ -13,7 +13,8 @@ export const buildHeroku = async (
const buildAppDirectory = getBuildAppDirectory(application); const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
try { try {
const args = [ const args = [
@@ -53,7 +54,8 @@ export const getHerokuCommand = (
const buildAppDirectory = getBuildAppDirectory(application); const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const args = [ const args = [
+3 -2
View File
@@ -30,7 +30,7 @@ export type ApplicationNested = InferResultType<
redirects: true; redirects: true;
ports: true; ports: true;
registry: true; registry: true;
project: true; environment: { with: { project: true } };
} }
>; >;
@@ -148,7 +148,8 @@ export const mechanizeDockerContainer = async (
const filesMount = generateFileMounts(appName, application); const filesMount = generateFileMounts(appName, application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const image = getImageName(application); const image = getImageName(application);
@@ -20,7 +20,8 @@ export const buildNixpacks = async (
const buildContainerId = `${appName}-${nanoid(10)}`; const buildContainerId = `${appName}-${nanoid(10)}`;
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const writeToStream = (data: string) => { const writeToStream = (data: string) => {
@@ -101,7 +102,8 @@ export const getNixpacksCommand = (
const buildContainerId = `${appName}-${nanoid(10)}`; const buildContainerId = `${appName}-${nanoid(10)}`;
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const args = ["build", buildAppDirectory, "--name", appName]; const args = ["build", buildAppDirectory, "--name", appName];
+4 -2
View File
@@ -12,7 +12,8 @@ export const buildPaketo = async (
const buildAppDirectory = getBuildAppDirectory(application); const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
try { try {
const args = [ const args = [
@@ -52,7 +53,8 @@ export const getPaketoCommand = (
const buildAppDirectory = getBuildAppDirectory(application); const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
const args = [ const args = [
@@ -26,7 +26,8 @@ export const buildRailpack = async (
const buildAppDirectory = getBuildAppDirectory(application); const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
try { try {
@@ -123,7 +124,8 @@ export const getRailpackCommand = (
const buildAppDirectory = getBuildAppDirectory(application); const buildAppDirectory = getBuildAppDirectory(application);
const envVariables = prepareEnvironmentVariables( const envVariables = prepareEnvironmentVariables(
env, env,
application.project.env, application.environment.project.env,
application.environment.env,
); );
// Prepare command // Prepare command

Some files were not shown because too many files have changed in this diff Show More