From c58aafeb3231ff94ae0cd560e015cb58a152b042 Mon Sep 17 00:00:00 2001 From: Hemachandar <132386067+hmacr@users.noreply.github.com> Date: Sun, 29 Sep 2024 02:59:34 +0530 Subject: [PATCH] fix: check doc access before sending mention email (#7664) * fix: check doc access before sending mention email * refactor --------- Co-authored-by: Tom Moor --- .../tasks/CommentCreatedNotificationsTask.ts | 4 +++- .../tasks/CommentUpdatedNotificationsTask.ts | 5 ++++- .../DocumentPublishedNotificationsTask.ts | 4 +++- .../tasks/RevisionCreatedNotificationsTask.ts | 16 ++------------ server/utils/policies.ts | 21 +++++++++++++++++++ 5 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 server/utils/policies.ts diff --git a/server/queues/tasks/CommentCreatedNotificationsTask.ts b/server/queues/tasks/CommentCreatedNotificationsTask.ts index 50f92429b1..2bed7340e8 100644 --- a/server/queues/tasks/CommentCreatedNotificationsTask.ts +++ b/server/queues/tasks/CommentCreatedNotificationsTask.ts @@ -5,6 +5,7 @@ import NotificationHelper from "@server/models/helpers/NotificationHelper"; import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; import { sequelize } from "@server/storage/database"; import { CommentEvent } from "@server/types"; +import { canUserAccessDocument } from "@server/utils/policies"; import BaseTask, { TaskPriority } from "./BaseTask"; export default class CommentCreatedNotificationsTask extends BaseTask { @@ -52,7 +53,8 @@ export default class CommentCreatedNotificationsTask extends BaseTask { @@ -41,7 +42,8 @@ export default class CommentUpdatedNotificationsTask extends BaseTask { @@ -33,7 +34,8 @@ export default class DocumentPublishedNotificationsTask extends BaseTask { @@ -54,7 +54,7 @@ export default class RevisionCreatedNotificationsTask extends BaseTask { - try { - const document = await Document.findByPk(model.id, { - userId: user.id, - }); - authorize(user, "read", document); - return true; - } catch (err) { - return false; - } - }; - public get options() { return { priority: TaskPriority.Background, diff --git a/server/utils/policies.ts b/server/utils/policies.ts new file mode 100644 index 0000000000..3058b46934 --- /dev/null +++ b/server/utils/policies.ts @@ -0,0 +1,21 @@ +import { Document, User } from "@server/models"; +import { authorize } from "@server/policies"; + +/** + * Check if the given user can access a document + * + * @param user - The user to check + * @param documentId - The document to check + * @returns Boolean whether the user can access the document + */ +export const canUserAccessDocument = async (user: User, documentId: string) => { + try { + const document = await Document.findByPk(documentId, { + userId: user.id, + }); + authorize(user, "read", document); + return true; + } catch (err) { + return false; + } +};