mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-19 10:25:20 +00:00
feat: implement environment access control and service filtering based on user permissions, enhancing security and usability in environment management
This commit is contained in:
@@ -208,66 +208,81 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
<DropdownMenuLabel>Environments</DropdownMenuLabel>
|
<DropdownMenuLabel>Environments</DropdownMenuLabel>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
|
|
||||||
{environments?.map((environment) => (
|
{environments?.map((environment) => {
|
||||||
<div key={environment.environmentId} className="flex items-center">
|
const servicesCount =
|
||||||
<DropdownMenuItem
|
environment.mariadb.length +
|
||||||
className="flex-1 cursor-pointer"
|
environment.mongo.length +
|
||||||
onClick={() => {
|
environment.mysql.length +
|
||||||
router.push(
|
environment.postgres.length +
|
||||||
`/dashboard/project/${projectId}/environment/${environment.environmentId}`,
|
environment.redis.length +
|
||||||
);
|
environment.applications.length +
|
||||||
}}
|
environment.compose.length;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={environment.environmentId}
|
||||||
|
className="flex items-center"
|
||||||
>
|
>
|
||||||
<div className="flex items-center justify-between w-full">
|
<DropdownMenuItem
|
||||||
<span>{environment.name}</span>
|
className="flex-1 cursor-pointer"
|
||||||
{environment.environmentId === currentEnvironmentId && (
|
onClick={() => {
|
||||||
<div className="w-2 h-2 bg-blue-500 rounded-full" />
|
router.push(
|
||||||
)}
|
`/dashboard/project/${projectId}/environment/${environment.environmentId}`,
|
||||||
</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" />
|
<div className="flex items-center justify-between w-full">
|
||||||
</Button>
|
<span>
|
||||||
</EnvironmentVariables>
|
{environment.name} ({servicesCount})
|
||||||
{environment.name !== "production" && (
|
</span>
|
||||||
<div className="flex items-center gap-1 px-2">
|
{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
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
className="h-6 w-6 p-0"
|
className="h-6 w-6 p-0"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
openEditDialog(environment);
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<PencilIcon className="h-3 w-3" />
|
<Terminal className="h-3 w-3" />
|
||||||
</Button>
|
</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
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
|
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
openDeleteDialog(environment);
|
openDeleteDialog(environment);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TrashIcon className="h-3 w-3" />
|
<TrashIcon className="h-3 w-3" />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
);
|
||||||
|
})}
|
||||||
|
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
|
addNewEnvironment,
|
||||||
|
checkEnvironmentAccess,
|
||||||
createEnvironment,
|
createEnvironment,
|
||||||
deleteEnvironment,
|
deleteEnvironment,
|
||||||
duplicateEnvironment,
|
duplicateEnvironment,
|
||||||
@@ -18,16 +20,53 @@ import {
|
|||||||
apiUpdateEnvironment,
|
apiUpdateEnvironment,
|
||||||
} from "@/server/db/schema";
|
} 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({
|
export const environmentRouter = createTRPCRouter({
|
||||||
create: protectedProcedure
|
create: protectedProcedure
|
||||||
.input(apiCreateEnvironment)
|
.input(apiCreateEnvironment)
|
||||||
.mutation(async ({ input }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
// Check if user has access to the project
|
// Check if user has access to the project
|
||||||
// This would typically involve checking project ownership/membership
|
// This would typically involve checking project ownership/membership
|
||||||
// For now, we'll use a basic organization check
|
// For now, we'll use a basic organization check
|
||||||
|
|
||||||
const environment = await createEnvironment(input);
|
const environment = await createEnvironment(input);
|
||||||
|
|
||||||
|
if (ctx.user.role === "member") {
|
||||||
|
await addNewEnvironment(
|
||||||
|
ctx.user.id,
|
||||||
|
environment.environmentId,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
);
|
||||||
|
}
|
||||||
return environment;
|
return environment;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
@@ -42,6 +81,14 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
.input(apiFindOneEnvironment)
|
.input(apiFindOneEnvironment)
|
||||||
.query(async ({ input, ctx }) => {
|
.query(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
|
if (ctx.user.role === "member") {
|
||||||
|
await checkEnvironmentAccess(
|
||||||
|
ctx.user.id,
|
||||||
|
input.environmentId,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
"access",
|
||||||
|
);
|
||||||
|
}
|
||||||
const environment = await findEnvironmentById(input.environmentId);
|
const environment = await findEnvironmentById(input.environmentId);
|
||||||
if (
|
if (
|
||||||
environment.project.organizationId !==
|
environment.project.organizationId !==
|
||||||
@@ -66,30 +113,10 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Filter services based on member permissions
|
// Filter services based on member permissions
|
||||||
const filteredEnvironment = {
|
const filteredEnvironment = filterEnvironmentServices(
|
||||||
...environment,
|
environment,
|
||||||
applications: environment.applications.filter((app) =>
|
accessedServices,
|
||||||
accessedServices.includes(app.applicationId),
|
);
|
||||||
),
|
|
||||||
mariadb: environment.mariadb.filter((db) =>
|
|
||||||
accessedServices.includes(db.mariadbId),
|
|
||||||
),
|
|
||||||
mongo: environment.mongo.filter((db) =>
|
|
||||||
accessedServices.includes(db.mongoId),
|
|
||||||
),
|
|
||||||
mysql: environment.mysql.filter((db) =>
|
|
||||||
accessedServices.includes(db.mysqlId),
|
|
||||||
),
|
|
||||||
postgres: environment.postgres.filter((db) =>
|
|
||||||
accessedServices.includes(db.postgresId),
|
|
||||||
),
|
|
||||||
redis: environment.redis.filter((db) =>
|
|
||||||
accessedServices.includes(db.redisId),
|
|
||||||
),
|
|
||||||
compose: environment.compose.filter((comp) =>
|
|
||||||
accessedServices.includes(comp.composeId),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
return filteredEnvironment;
|
return filteredEnvironment;
|
||||||
}
|
}
|
||||||
@@ -125,15 +152,17 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
|
|
||||||
// Filter environments for members based on their permissions
|
// Filter environments for members based on their permissions
|
||||||
if (ctx.user.role === "member") {
|
if (ctx.user.role === "member") {
|
||||||
const { accessedEnvironments } = await findMemberById(
|
const { accessedEnvironments, accessedServices } =
|
||||||
ctx.user.id,
|
await findMemberById(ctx.user.id, ctx.session.activeOrganizationId);
|
||||||
ctx.session.activeOrganizationId,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Filter environments to only show those the member has access to
|
// Filter environments to only show those the member has access to
|
||||||
const filteredEnvironments = environments.filter((environment) =>
|
const filteredEnvironments = environments
|
||||||
accessedEnvironments.includes(environment.environmentId),
|
.filter((environment) =>
|
||||||
);
|
accessedEnvironments.includes(environment.environmentId),
|
||||||
|
)
|
||||||
|
.map((environment) =>
|
||||||
|
filterEnvironmentServices(environment, accessedServices),
|
||||||
|
);
|
||||||
|
|
||||||
return filteredEnvironments;
|
return filteredEnvironments;
|
||||||
}
|
}
|
||||||
@@ -151,6 +180,14 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
.input(apiRemoveEnvironment)
|
.input(apiRemoveEnvironment)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
|
if (ctx.user.role === "member") {
|
||||||
|
await checkEnvironmentAccess(
|
||||||
|
ctx.user.id,
|
||||||
|
input.environmentId,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
"access",
|
||||||
|
);
|
||||||
|
}
|
||||||
const environment = await findEnvironmentById(input.environmentId);
|
const environment = await findEnvironmentById(input.environmentId);
|
||||||
if (
|
if (
|
||||||
environment.project.organizationId !==
|
environment.project.organizationId !==
|
||||||
@@ -192,6 +229,14 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
const { environmentId, ...updateData } = input;
|
const { environmentId, ...updateData } = input;
|
||||||
|
if (ctx.user.role === "member") {
|
||||||
|
await checkEnvironmentAccess(
|
||||||
|
ctx.user.id,
|
||||||
|
environmentId,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
"access",
|
||||||
|
);
|
||||||
|
}
|
||||||
const currentEnvironment = await findEnvironmentById(environmentId);
|
const currentEnvironment = await findEnvironmentById(environmentId);
|
||||||
if (
|
if (
|
||||||
currentEnvironment.project.organizationId !==
|
currentEnvironment.project.organizationId !==
|
||||||
@@ -237,6 +282,14 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
.input(apiDuplicateEnvironment)
|
.input(apiDuplicateEnvironment)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
|
if (ctx.user.role === "member") {
|
||||||
|
await checkEnvironmentAccess(
|
||||||
|
ctx.user.id,
|
||||||
|
input.environmentId,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
"access",
|
||||||
|
);
|
||||||
|
}
|
||||||
const environment = await findEnvironmentById(input.environmentId);
|
const environment = await findEnvironmentById(input.environmentId);
|
||||||
if (
|
if (
|
||||||
environment.project.organizationId !==
|
environment.project.organizationId !==
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user