mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-25 13:18:47 +00:00
feat(tags): enhance tag management with permission checks
- Integrated user permissions for tag creation, updating, and deletion in the TagManager component. - Updated API routes to enforce permission checks for tag operations. - Added new permissions for managing tags in the roles configuration. - Improved error handling for unauthorized access in tag-related operations.
This commit is contained in:
@@ -18,6 +18,7 @@ export const TagManager = () => {
|
|||||||
const { data: tags, isPending } = api.tag.all.useQuery();
|
const { data: tags, isPending } = api.tag.all.useQuery();
|
||||||
const { mutateAsync: deleteTag, isPending: isRemoving } =
|
const { mutateAsync: deleteTag, isPending: isRemoving } =
|
||||||
api.tag.remove.useMutation();
|
api.tag.remove.useMutation();
|
||||||
|
const { data: permissions } = api.user.getPermissions.useQuery();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
@@ -47,7 +48,7 @@ export const TagManager = () => {
|
|||||||
No tags yet. Create your first tag to start organizing
|
No tags yet. Create your first tag to start organizing
|
||||||
projects.
|
projects.
|
||||||
</span>
|
</span>
|
||||||
<HandleTag />
|
{permissions?.tag.create && <HandleTag />}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex flex-col gap-4 min-h-[25vh]">
|
<div className="flex flex-col gap-4 min-h-[25vh]">
|
||||||
@@ -70,8 +71,10 @@ export const TagManager = () => {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-row gap-1 items-center">
|
<div className="flex flex-row gap-1 items-center">
|
||||||
<HandleTag tagId={tag.tagId} />
|
{permissions?.tag.update && (
|
||||||
<DialogAction
|
<HandleTag tagId={tag.tagId} />
|
||||||
|
)}
|
||||||
|
{permissions?.tag.delete && (<DialogAction
|
||||||
title="Delete Tag"
|
title="Delete Tag"
|
||||||
description={`Are you sure you want to delete the tag "${tag.name}"? This will remove the tag from all projects. This action cannot be undone.`}
|
description={`Are you sure you want to delete the tag "${tag.name}"? This will remove the tag from all projects. This action cannot be undone.`}
|
||||||
type="destructive"
|
type="destructive"
|
||||||
@@ -101,15 +104,18 @@ export const TagManager = () => {
|
|||||||
<Trash2 className="size-4 text-primary group-hover:text-red-500" />
|
<Trash2 className="size-4 text-primary group-hover:text-red-500" />
|
||||||
</Button>
|
</Button>
|
||||||
</DialogAction>
|
</DialogAction>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-row gap-2 flex-wrap w-full justify-end mr-4">
|
{permissions?.tag.create && (
|
||||||
<HandleTag />
|
<div className="flex flex-row gap-2 flex-wrap w-full justify-end mr-4">
|
||||||
</div>
|
<HandleTag />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -143,6 +143,10 @@ const RESOURCE_META: Record<string, { label: string; description: string }> = {
|
|||||||
description:
|
description:
|
||||||
"Manage notification providers (Slack, Discord, Telegram, etc.)",
|
"Manage notification providers (Slack, Discord, Telegram, etc.)",
|
||||||
},
|
},
|
||||||
|
tag: {
|
||||||
|
label: "Tags",
|
||||||
|
description: "Manage tags to organize and categorize projects",
|
||||||
|
},
|
||||||
member: {
|
member: {
|
||||||
label: "Users",
|
label: "Users",
|
||||||
description: "Manage organization members, invitations, and roles",
|
description: "Manage organization members, invitations, and roles",
|
||||||
@@ -379,6 +383,12 @@ const ACTION_META: Record<
|
|||||||
},
|
},
|
||||||
delete: { label: "Delete", description: "Remove notification providers" },
|
delete: { label: "Delete", description: "Remove notification providers" },
|
||||||
},
|
},
|
||||||
|
tag: {
|
||||||
|
read: { label: "Read", description: "View tags" },
|
||||||
|
create: { label: "Create", description: "Create new tags" },
|
||||||
|
update: { label: "Update", description: "Edit existing tags" },
|
||||||
|
delete: { label: "Delete", description: "Delete tags" },
|
||||||
|
},
|
||||||
member: {
|
member: {
|
||||||
read: {
|
read: {
|
||||||
label: "Read",
|
label: "Read",
|
||||||
@@ -447,6 +457,7 @@ const ROLE_PRESETS: {
|
|||||||
domain: ["read"],
|
domain: ["read"],
|
||||||
destination: ["read"],
|
destination: ["read"],
|
||||||
notification: ["read"],
|
notification: ["read"],
|
||||||
|
tag: ["read"],
|
||||||
member: ["read"],
|
member: ["read"],
|
||||||
logs: ["read"],
|
logs: ["read"],
|
||||||
monitoring: ["read"],
|
monitoring: ["read"],
|
||||||
@@ -515,6 +526,7 @@ const ROLE_PRESETS: {
|
|||||||
domain: ["read", "create", "delete"],
|
domain: ["read", "create", "delete"],
|
||||||
destination: ["read", "create", "delete"],
|
destination: ["read", "create", "delete"],
|
||||||
notification: ["read", "create", "delete"],
|
notification: ["read", "create", "delete"],
|
||||||
|
tag: ["read", "create", "update", "delete"],
|
||||||
logs: ["read"],
|
logs: ["read"],
|
||||||
monitoring: ["read"],
|
monitoring: ["read"],
|
||||||
auditLog: ["read"],
|
auditLog: ["read"],
|
||||||
|
|||||||
@@ -656,6 +656,18 @@ export const settingsRouter = createTRPCRouter({
|
|||||||
"github",
|
"github",
|
||||||
"gitlab",
|
"gitlab",
|
||||||
"gitea",
|
"gitea",
|
||||||
|
"tag",
|
||||||
|
"patch",
|
||||||
|
"server",
|
||||||
|
"volumeBackups",
|
||||||
|
"environment",
|
||||||
|
"auditLog",
|
||||||
|
"customRole",
|
||||||
|
"whitelabeling",
|
||||||
|
"sso",
|
||||||
|
"licenseKey",
|
||||||
|
"organization",
|
||||||
|
"previewDeployment",
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { findMemberByUserId } from "@dokploy/server/services/permission";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { and, eq } from "drizzle-orm";
|
import { and, eq } from "drizzle-orm";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
@@ -11,10 +12,10 @@ import {
|
|||||||
projectTags,
|
projectTags,
|
||||||
tags,
|
tags,
|
||||||
} from "@/server/db/schema";
|
} from "@/server/db/schema";
|
||||||
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
import { createTRPCRouter, protectedProcedure, withPermission } from "../trpc";
|
||||||
|
|
||||||
export const tagRouter = createTRPCRouter({
|
export const tagRouter = createTRPCRouter({
|
||||||
create: protectedProcedure
|
create: withPermission("tag", "create")
|
||||||
.input(apiCreateTag)
|
.input(apiCreateTag)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
@@ -46,7 +47,7 @@ export const tagRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
all: protectedProcedure.query(async ({ ctx }) => {
|
all: withPermission("tag", "read").query(async ({ ctx }) => {
|
||||||
try {
|
try {
|
||||||
const organizationTags = await db.query.tags.findMany({
|
const organizationTags = await db.query.tags.findMany({
|
||||||
where: eq(tags.organizationId, ctx.session.activeOrganizationId),
|
where: eq(tags.organizationId, ctx.session.activeOrganizationId),
|
||||||
@@ -63,36 +64,38 @@ export const tagRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
one: protectedProcedure.input(apiFindOneTag).query(async ({ input, ctx }) => {
|
one: withPermission("tag", "read")
|
||||||
try {
|
.input(apiFindOneTag)
|
||||||
const tag = await db.query.tags.findFirst({
|
.query(async ({ input, ctx }) => {
|
||||||
where: and(
|
try {
|
||||||
eq(tags.tagId, input.tagId),
|
const tag = await db.query.tags.findFirst({
|
||||||
eq(tags.organizationId, ctx.session.activeOrganizationId),
|
where: and(
|
||||||
),
|
eq(tags.tagId, input.tagId),
|
||||||
});
|
eq(tags.organizationId, ctx.session.activeOrganizationId),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "NOT_FOUND",
|
||||||
|
message: "Tag not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag;
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof TRPCError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "NOT_FOUND",
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
message: "Tag not found",
|
message: `Error fetching tag: ${error instanceof Error ? error.message : error}`,
|
||||||
|
cause: error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
return tag;
|
update: withPermission("tag", "update")
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof TRPCError) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
|
||||||
message: `Error fetching tag: ${error instanceof Error ? error.message : error}`,
|
|
||||||
cause: error,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
update: protectedProcedure
|
|
||||||
.input(apiUpdateTag)
|
.input(apiUpdateTag)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
@@ -142,7 +145,7 @@ export const tagRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
remove: protectedProcedure
|
remove: withPermission("tag", "delete")
|
||||||
.input(apiRemoveTag)
|
.input(apiRemoveTag)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
@@ -186,6 +189,11 @@ export const tagRouter = createTRPCRouter({
|
|||||||
)
|
)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
|
const memberRecord = await findMemberByUserId(
|
||||||
|
ctx.user.id,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
);
|
||||||
|
|
||||||
// Verify the project belongs to the user's organization
|
// Verify the project belongs to the user's organization
|
||||||
const project = await db.query.projects.findFirst({
|
const project = await db.query.projects.findFirst({
|
||||||
where: and(
|
where: and(
|
||||||
@@ -202,6 +210,18 @@ export const tagRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify the member has access to the project
|
||||||
|
if (
|
||||||
|
memberRecord.role !== "owner" &&
|
||||||
|
memberRecord.role !== "admin" &&
|
||||||
|
!memberRecord.accessedProjects.includes(input.projectId)
|
||||||
|
) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this project",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Verify the tag belongs to the user's organization
|
// Verify the tag belongs to the user's organization
|
||||||
const tag = await db.query.tags.findFirst({
|
const tag = await db.query.tags.findFirst({
|
||||||
where: and(
|
where: and(
|
||||||
@@ -257,6 +277,11 @@ export const tagRouter = createTRPCRouter({
|
|||||||
)
|
)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
|
const memberRecord = await findMemberByUserId(
|
||||||
|
ctx.user.id,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
);
|
||||||
|
|
||||||
// Verify the project belongs to the user's organization
|
// Verify the project belongs to the user's organization
|
||||||
const project = await db.query.projects.findFirst({
|
const project = await db.query.projects.findFirst({
|
||||||
where: and(
|
where: and(
|
||||||
@@ -273,6 +298,18 @@ export const tagRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify the member has access to the project
|
||||||
|
if (
|
||||||
|
memberRecord.role !== "owner" &&
|
||||||
|
memberRecord.role !== "admin" &&
|
||||||
|
!memberRecord.accessedProjects.includes(input.projectId)
|
||||||
|
) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this project",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Verify the tag belongs to the user's organization
|
// Verify the tag belongs to the user's organization
|
||||||
const tag = await db.query.tags.findFirst({
|
const tag = await db.query.tags.findFirst({
|
||||||
where: and(
|
where: and(
|
||||||
@@ -320,6 +357,11 @@ export const tagRouter = createTRPCRouter({
|
|||||||
)
|
)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
try {
|
||||||
|
const memberRecord = await findMemberByUserId(
|
||||||
|
ctx.user.id,
|
||||||
|
ctx.session.activeOrganizationId,
|
||||||
|
);
|
||||||
|
|
||||||
// Verify the project belongs to the user's organization
|
// Verify the project belongs to the user's organization
|
||||||
const project = await db.query.projects.findFirst({
|
const project = await db.query.projects.findFirst({
|
||||||
where: and(
|
where: and(
|
||||||
@@ -336,6 +378,18 @@ export const tagRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify the member has access to the project
|
||||||
|
if (
|
||||||
|
memberRecord.role !== "owner" &&
|
||||||
|
memberRecord.role !== "admin" &&
|
||||||
|
!memberRecord.accessedProjects.includes(input.projectId)
|
||||||
|
) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You don't have access to this project",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Verify all tags belong to the user's organization
|
// Verify all tags belong to the user's organization
|
||||||
if (input.tagIds.length > 0) {
|
if (input.tagIds.length > 0) {
|
||||||
const tagCount = await db.query.tags.findMany({
|
const tagCount = await db.query.tags.findMany({
|
||||||
|
|||||||
@@ -84,11 +84,9 @@ export const apiCreateTag = createSchema.pick({
|
|||||||
color: true,
|
color: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const apiFindOneTag = createSchema
|
export const apiFindOneTag = z.object({
|
||||||
.pick({
|
tagId: z.string().min(1),
|
||||||
tagId: true,
|
});
|
||||||
})
|
|
||||||
.required();
|
|
||||||
|
|
||||||
export const apiRemoveTag = createSchema
|
export const apiRemoveTag = createSchema
|
||||||
.pick({
|
.pick({
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ export const statements = {
|
|||||||
domain: ["read", "create", "delete"],
|
domain: ["read", "create", "delete"],
|
||||||
destination: ["read", "create", "delete"],
|
destination: ["read", "create", "delete"],
|
||||||
notification: ["read", "create", "update", "delete"],
|
notification: ["read", "create", "update", "delete"],
|
||||||
|
tag: ["read", "create", "update", "delete"],
|
||||||
logs: ["read"],
|
logs: ["read"],
|
||||||
monitoring: ["read"],
|
monitoring: ["read"],
|
||||||
auditLog: ["read"],
|
auditLog: ["read"],
|
||||||
@@ -69,6 +70,7 @@ export const enterpriseOnlyResources = new Set<string>([
|
|||||||
"domain",
|
"domain",
|
||||||
"destination",
|
"destination",
|
||||||
"notification",
|
"notification",
|
||||||
|
"tag",
|
||||||
"logs",
|
"logs",
|
||||||
"monitoring",
|
"monitoring",
|
||||||
"auditLog",
|
"auditLog",
|
||||||
@@ -107,6 +109,7 @@ export const ownerRole = ac.newRole({
|
|||||||
domain: ["read", "create", "delete"],
|
domain: ["read", "create", "delete"],
|
||||||
destination: ["read", "create", "delete"],
|
destination: ["read", "create", "delete"],
|
||||||
notification: ["read", "create", "update", "delete"],
|
notification: ["read", "create", "update", "delete"],
|
||||||
|
tag: ["read", "create", "update", "delete"],
|
||||||
logs: ["read"],
|
logs: ["read"],
|
||||||
monitoring: ["read"],
|
monitoring: ["read"],
|
||||||
auditLog: ["read"],
|
auditLog: ["read"],
|
||||||
@@ -143,6 +146,7 @@ export const adminRole = ac.newRole({
|
|||||||
domain: ["read", "create", "delete"],
|
domain: ["read", "create", "delete"],
|
||||||
destination: ["read", "create", "delete"],
|
destination: ["read", "create", "delete"],
|
||||||
notification: ["read", "create", "update", "delete"],
|
notification: ["read", "create", "update", "delete"],
|
||||||
|
tag: ["read", "create", "update", "delete"],
|
||||||
logs: ["read"],
|
logs: ["read"],
|
||||||
monitoring: ["read"],
|
monitoring: ["read"],
|
||||||
auditLog: ["read"],
|
auditLog: ["read"],
|
||||||
@@ -186,5 +190,6 @@ export const memberRole = ac.newRole({
|
|||||||
certificate: [],
|
certificate: [],
|
||||||
destination: [],
|
destination: [],
|
||||||
notification: [],
|
notification: [],
|
||||||
|
tag: ["read"],
|
||||||
auditLog: [],
|
auditLog: [],
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user