mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-23 12:18:46 +00:00
feat: add environment deletion permission control - Add canDeleteEnvironments field to member table - Implement permission validation in environment deletion endpoint - Add UI toggle in user permissions modal - Hide delete buttons for users without permission - Maintain backward compatibility for owners/admins #2594
This commit is contained in:
@@ -59,6 +59,13 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const { data: currentUser } = api.user.get.useQuery();
|
||||||
|
|
||||||
|
// Check if user can delete environments
|
||||||
|
const canDeleteEnvironments = currentUser?.role === "owner" ||
|
||||||
|
currentUser?.role === "admin" ||
|
||||||
|
currentUser?.canDeleteEnvironments === true;
|
||||||
|
|
||||||
// Form states
|
// Form states
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [description, setDescription] = useState("");
|
const [description, setDescription] = useState("");
|
||||||
@@ -267,17 +274,19 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
<PencilIcon className="h-3 w-3" />
|
<PencilIcon className="h-3 w-3" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button
|
{canDeleteEnvironments && (
|
||||||
variant="ghost"
|
<Button
|
||||||
size="sm"
|
variant="ghost"
|
||||||
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
|
size="sm"
|
||||||
onClick={(e) => {
|
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
|
||||||
e.stopPropagation();
|
onClick={(e) => {
|
||||||
openDeleteDialog(environment);
|
e.stopPropagation();
|
||||||
}}
|
openDeleteDialog(environment);
|
||||||
>
|
}}
|
||||||
<TrashIcon className="h-3 w-3" />
|
>
|
||||||
</Button>
|
<TrashIcon className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -161,6 +161,7 @@ const addPermissions = z.object({
|
|||||||
canCreateServices: z.boolean().optional().default(false),
|
canCreateServices: z.boolean().optional().default(false),
|
||||||
canDeleteProjects: z.boolean().optional().default(false),
|
canDeleteProjects: z.boolean().optional().default(false),
|
||||||
canDeleteServices: z.boolean().optional().default(false),
|
canDeleteServices: z.boolean().optional().default(false),
|
||||||
|
canDeleteEnvironments: z.boolean().optional().default(false),
|
||||||
canAccessToTraefikFiles: z.boolean().optional().default(false),
|
canAccessToTraefikFiles: z.boolean().optional().default(false),
|
||||||
canAccessToDocker: z.boolean().optional().default(false),
|
canAccessToDocker: z.boolean().optional().default(false),
|
||||||
canAccessToAPI: z.boolean().optional().default(false),
|
canAccessToAPI: z.boolean().optional().default(false),
|
||||||
@@ -193,6 +194,7 @@ export const AddUserPermissions = ({ userId }: Props) => {
|
|||||||
defaultValues: {
|
defaultValues: {
|
||||||
accessedProjects: [],
|
accessedProjects: [],
|
||||||
accessedServices: [],
|
accessedServices: [],
|
||||||
|
canDeleteEnvironments: false,
|
||||||
},
|
},
|
||||||
resolver: zodResolver(addPermissions),
|
resolver: zodResolver(addPermissions),
|
||||||
});
|
});
|
||||||
@@ -207,6 +209,7 @@ export const AddUserPermissions = ({ userId }: Props) => {
|
|||||||
canCreateServices: data.canCreateServices,
|
canCreateServices: data.canCreateServices,
|
||||||
canDeleteProjects: data.canDeleteProjects,
|
canDeleteProjects: data.canDeleteProjects,
|
||||||
canDeleteServices: data.canDeleteServices,
|
canDeleteServices: data.canDeleteServices,
|
||||||
|
canDeleteEnvironments: data.canDeleteEnvironments || false,
|
||||||
canAccessToTraefikFiles: data.canAccessToTraefikFiles,
|
canAccessToTraefikFiles: data.canAccessToTraefikFiles,
|
||||||
canAccessToDocker: data.canAccessToDocker,
|
canAccessToDocker: data.canAccessToDocker,
|
||||||
canAccessToAPI: data.canAccessToAPI,
|
canAccessToAPI: data.canAccessToAPI,
|
||||||
@@ -223,6 +226,7 @@ export const AddUserPermissions = ({ userId }: Props) => {
|
|||||||
canCreateProjects: data.canCreateProjects,
|
canCreateProjects: data.canCreateProjects,
|
||||||
canDeleteServices: data.canDeleteServices,
|
canDeleteServices: data.canDeleteServices,
|
||||||
canDeleteProjects: data.canDeleteProjects,
|
canDeleteProjects: data.canDeleteProjects,
|
||||||
|
canDeleteEnvironments: data.canDeleteEnvironments,
|
||||||
canAccessToTraefikFiles: data.canAccessToTraefikFiles,
|
canAccessToTraefikFiles: data.canAccessToTraefikFiles,
|
||||||
accessedProjects: data.accessedProjects || [],
|
accessedProjects: data.accessedProjects || [],
|
||||||
accessedEnvironments: data.accessedEnvironments || [],
|
accessedEnvironments: data.accessedEnvironments || [],
|
||||||
@@ -343,6 +347,26 @@ export const AddUserPermissions = ({ userId }: Props) => {
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="canDeleteEnvironments"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex flex-row items-center justify-between rounded-lg border p-3 shadow-sm">
|
||||||
|
<div className="space-y-0.5">
|
||||||
|
<FormLabel>Delete Environments</FormLabel>
|
||||||
|
<FormDescription>
|
||||||
|
Allow the user to delete environments
|
||||||
|
</FormDescription>
|
||||||
|
</div>
|
||||||
|
<FormControl>
|
||||||
|
<Switch
|
||||||
|
checked={field.value}
|
||||||
|
onCheckedChange={field.onChange}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="canAccessToTraefikFiles"
|
name="canAccessToTraefikFiles"
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE "member" ADD COLUMN "canDeleteEnvironments" boolean DEFAULT false NOT NULL;
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -778,6 +778,13 @@
|
|||||||
"when": 1757189541734,
|
"when": 1757189541734,
|
||||||
"tag": "0110_red_psynapse",
|
"tag": "0110_red_psynapse",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 111,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1757681390650,
|
||||||
|
"tag": "0111_daily_jack_murdock",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
addNewEnvironment,
|
addNewEnvironment,
|
||||||
checkEnvironmentAccess,
|
checkEnvironmentAccess,
|
||||||
|
checkEnvironmentDeletionPermission,
|
||||||
createEnvironment,
|
createEnvironment,
|
||||||
deleteEnvironment,
|
deleteEnvironment,
|
||||||
duplicateEnvironment,
|
duplicateEnvironment,
|
||||||
@@ -187,14 +188,6 @@ 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 !==
|
||||||
@@ -206,27 +199,33 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check environment access for members
|
// Check environment deletion permission
|
||||||
if (ctx.user.role === "member") {
|
await checkEnvironmentDeletionPermission(
|
||||||
const { accessedEnvironments } = await findMemberById(
|
ctx.user.id,
|
||||||
ctx.user.id,
|
environment.projectId,
|
||||||
ctx.session.activeOrganizationId,
|
ctx.session.activeOrganizationId,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!accessedEnvironments.includes(environment.environmentId)) {
|
// Additional check for environment access for members
|
||||||
throw new TRPCError({
|
if (ctx.user.role === "member") {
|
||||||
code: "FORBIDDEN",
|
await checkEnvironmentAccess(
|
||||||
message: "You are not allowed to delete this environment",
|
ctx.user.id,
|
||||||
});
|
input.environmentId,
|
||||||
}
|
ctx.session.activeOrganizationId,
|
||||||
|
"access",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const deletedEnvironment = await deleteEnvironment(input.environmentId);
|
const deletedEnvironment = await deleteEnvironment(input.environmentId);
|
||||||
return deletedEnvironment;
|
return deletedEnvironment;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof TRPCError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "BAD_REQUEST",
|
code: "BAD_REQUEST",
|
||||||
message: `Error deleting the environment: ${error instanceof Error ? error.message : error}`,
|
message: `Error deleting the environment: ${error instanceof Error ? error.message : error}`,
|
||||||
|
cause: error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -108,6 +108,9 @@ export const member = pgTable("member", {
|
|||||||
canAccessToTraefikFiles: boolean("canAccessToTraefikFiles")
|
canAccessToTraefikFiles: boolean("canAccessToTraefikFiles")
|
||||||
.notNull()
|
.notNull()
|
||||||
.default(false),
|
.default(false),
|
||||||
|
canDeleteEnvironments: boolean("canDeleteEnvironments")
|
||||||
|
.notNull()
|
||||||
|
.default(false),
|
||||||
accessedProjects: text("accesedProjects")
|
accessedProjects: text("accesedProjects")
|
||||||
.array()
|
.array()
|
||||||
.notNull()
|
.notNull()
|
||||||
|
|||||||
@@ -186,6 +186,7 @@ export const apiAssignPermissions = createSchema
|
|||||||
canAccessToAPI: z.boolean().optional(),
|
canAccessToAPI: z.boolean().optional(),
|
||||||
canAccessToSSHKeys: z.boolean().optional(),
|
canAccessToSSHKeys: z.boolean().optional(),
|
||||||
canAccessToGitProviders: z.boolean().optional(),
|
canAccessToGitProviders: z.boolean().optional(),
|
||||||
|
canDeleteEnvironments: z.boolean().optional(),
|
||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,24 @@ export const canPerformAccessEnvironment = async (
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const canPerformDeleteEnvironment = async (
|
||||||
|
userId: string,
|
||||||
|
projectId: string,
|
||||||
|
organizationId: string,
|
||||||
|
) => {
|
||||||
|
const { accessedProjects, canDeleteEnvironments } = await findMemberById(
|
||||||
|
userId,
|
||||||
|
organizationId,
|
||||||
|
);
|
||||||
|
const haveAccessToProject = accessedProjects.includes(projectId);
|
||||||
|
|
||||||
|
if (canDeleteEnvironments && haveAccessToProject) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
export const canAccessToTraefikFiles = async (
|
export const canAccessToTraefikFiles = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
organizationId: string,
|
organizationId: string,
|
||||||
@@ -240,6 +258,42 @@ export const checkEnvironmentAccess = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const checkEnvironmentDeletionPermission = async (
|
||||||
|
userId: string,
|
||||||
|
projectId: string,
|
||||||
|
organizationId: string,
|
||||||
|
) => {
|
||||||
|
const member = await findMemberById(userId, organizationId);
|
||||||
|
|
||||||
|
if (!member) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "User not found in organization",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (member.role === "owner" || member.role === "admin") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!member.canDeleteEnvironments) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have permission to delete environments",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasProjectAccess = member.accessedProjects.includes(projectId);
|
||||||
|
if (!hasProjectAccess) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this project",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
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