Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🔒️ Check user permission before deleting comment (#2021)

Luke Vella99ba6533bca9
diff --git a/apps/web/src/features/poll/query.ts b/apps/web/src/features/poll/query.ts
new file mode 100644
index 000000000..957590799
--- /dev/null
+++ b/apps/web/src/features/poll/query.ts
@@ -0,0 +1,15 @@
+import { prisma } from "@rallly/database";
+
+export const hasPollAdminAccess = async (pollId: string, userId: string) => {
+ const poll = await prisma.poll.findFirst({
+ where: {
+ id: pollId,
+ OR: [{ userId: userId }, { space: { members: { some: { userId } } } }],
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ return poll !== null;
+};
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index 5006a04b0..e861fadfb 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -11,6 +11,7 @@ import { getCurrentUserSpace } from "@/auth/data";
import { moderateContent } from "@/features/moderation";
import { getPolls } from "@/features/poll/data";
import { canUserManagePoll } from "@/features/poll/helpers";
+import { hasPollAdminAccess } from "@/features/poll/query";
import { formatEventDateTime } from "@/features/scheduled-event/utils";
import { getEmailClient } from "@/utils/emails";
import { createIcsEvent } from "@/utils/ics";
@@ -26,17 +27,6 @@ import {
import { comments } from "./polls/comments";
import { participants } from "./polls/participants";
-const hasPollAdminAccess = async (pollId: string, userId: string) => {
- const poll = await prisma.poll.findFirst({
- where: {
- id: pollId,
- OR: [{ userId: userId }, { space: { members: { some: { userId } } } }],
- },
- });
-
- return poll !== null;
-};
-
const getPollIdFromAdminUrlId = async (urlId: string) => {
const res = await prisma.poll.findUnique({
select: {
diff --git a/apps/web/src/trpc/routers/polls/comments.ts b/apps/web/src/trpc/routers/polls/comments.ts
index c47fea6d2..9708f2dbf 100644
--- a/apps/web/src/trpc/routers/polls/comments.ts
+++ b/apps/web/src/trpc/routers/polls/comments.ts
@@ -2,6 +2,7 @@ import { prisma } from "@rallly/database";
import { absoluteUrl } from "@rallly/utils/absolute-url";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
+import { hasPollAdminAccess } from "@/features/poll/query";
import { getEmailClient } from "@/utils/emails";
import { createToken } from "@/utils/session";
import {
@@ -193,7 +194,7 @@ export const comments = router({
.mutation(async ({ input: { commentId }, ctx }) => {
const comment = await prisma.comment.findUnique({
where: { id: commentId },
- select: { pollId: true },
+ select: { pollId: true, userId: true, guestId: true },
});
if (!comment) {
@@ -203,6 +204,19 @@ export const comments = router({
});
}
+ const isAuthor =
+ comment.userId === ctx.user.id || comment.guestId === ctx.user.id;
+
+ if (
+ !isAuthor &&
+ !(await hasPollAdminAccess(comment.pollId, ctx.user.id))
+ ) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "You are not allowed to delete this comment",
+ });
+ }
+
await prisma.comment.delete({
where: {
id: commentId,