Skip to content

Commit 2163ff0

Browse files
authoredMar 20, 2024
fix(comments): permission check (#6057)
1 parent f809fe3 commit 2163ff0

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed
 

‎packages/sanity/src/structure/comments/plugin/field/CommentsField.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function CommentFieldInner(
9292

9393
const {
9494
comments,
95+
hasPermission,
9596
isCommentsOpen,
9697
isCreatingDataset,
9798
mentionOptions,
@@ -316,6 +317,11 @@ function CommentFieldInner(
316317
],
317318
)
318319

320+
// Render the default field component if the user doesn't have permission
321+
if (!hasPermission) {
322+
return props.renderDefault(props)
323+
}
324+
319325
return (
320326
<FieldStack {...applyCommentsFieldAttr(PathUtils.toString(props.path))} ref={rootRef}>
321327
{props.renderDefault({

‎packages/sanity/src/structure/comments/plugin/input/components/CommentsPortableTextInput.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ export const CommentsPortableTextInputInner = React.memo(function CommentsPortab
6363
const currentUser = useCurrentUser()
6464
const portal = usePortal()
6565

66-
const {mentionOptions, comments, operation, onCommentsOpen, getComment, setStatus, status} =
67-
useComments()
66+
const {
67+
comments,
68+
getComment,
69+
hasPermission,
70+
mentionOptions,
71+
onCommentsOpen,
72+
operation,
73+
setStatus,
74+
status,
75+
} = useComments()
6876
const {setSelectedPath, selectedPath} = useCommentsSelectedPath()
6977
const {scrollToComment, scrollToGroup} = useCommentsScroll()
7078
const {handleOpenDialog} = useCommentsUpsell()
@@ -502,6 +510,11 @@ export const CommentsPortableTextInputInner = React.memo(function CommentsPortab
502510
)
503511
const showFloatingInput = Boolean(nextCommentSelection && popoverAuthoringReferenceElement)
504512

513+
// Render the default input if the user doesn't have permission
514+
if (!hasPermission) {
515+
return props.renderDefault(props)
516+
}
517+
505518
return (
506519
<>
507520
<BoundaryElementProvider element={boundaryElement}>

‎packages/sanity/src/structure/comments/src/context/comments/CommentsProvider.tsx

+25-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getPublishedId,
55
useAddonDataset,
66
useCurrentUser,
7+
useDocumentValuePermissions,
78
useEditState,
89
useSchema,
910
useUserListWithPermissions,
@@ -73,9 +74,23 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr
7374

7475
const {name: workspaceName, dataset, projectId} = useWorkspace()
7576

77+
const documentValue = useMemo(() => {
78+
return editState.draft || editState.published
79+
}, [editState.draft, editState.published])
80+
81+
const documentRevisionId = useMemo(() => documentValue?._rev, [documentValue])
82+
7683
// A map to keep track of the latest transaction ID for each comment document.
7784
const transactionsIdMap = useMemo(() => new Map<DocumentId, TransactionId>(), [])
7885

86+
// We only need to check for read permission on the document since users with
87+
// read permission on the document can both read and write comments.
88+
// This is how permission work for the comments add-on dataset.
89+
const [readPermission] = useDocumentValuePermissions({
90+
document: documentValue || {_type: documentType, _id: publishedId},
91+
permission: 'read',
92+
})
93+
7994
// When the latest transaction ID is received, we remove the transaction id from the map.
8095
const handleOnLatestTransactionIdReceived = useCallback(
8196
(commentDocumentId: string) => {
@@ -109,12 +124,6 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr
109124
[transactionsIdMap],
110125
)
111126

112-
const documentValue = useMemo(() => {
113-
return editState.draft || editState.published
114-
}, [editState.draft, editState.published])
115-
116-
const documentRevisionId = useMemo(() => documentValue?._rev, [documentValue])
117-
118127
const handleSetStatus = useCallback(
119128
(newStatus: CommentStatus) => {
120129
// Avoids going to "resolved" when using links to comments
@@ -267,6 +276,8 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr
267276
isCommentsOpen,
268277
onCommentsOpen,
269278

279+
hasPermission: Boolean(readPermission?.granted),
280+
270281
comments: {
271282
data: threadItemsByStatus,
272283
error,
@@ -282,20 +293,21 @@ export const CommentsProvider = memo(function CommentsProvider(props: CommentsPr
282293
mentionOptions,
283294
}),
284295
[
285-
error,
296+
isCreatingDataset,
297+
status,
298+
handleSetStatus,
286299
getComment,
287300
isCommentsOpen,
288-
isCreatingDataset,
289-
loading,
290-
mentionOptions,
291301
onCommentsOpen,
302+
readPermission?.granted,
303+
threadItemsByStatus,
304+
error,
305+
loading,
292306
operation.create,
293307
operation.react,
294308
operation.remove,
295309
operation.update,
296-
status,
297-
handleSetStatus,
298-
threadItemsByStatus,
310+
mentionOptions,
299311
],
300312
)
301313

‎packages/sanity/src/structure/comments/src/context/comments/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface CommentsContextValue {
1919
isCommentsOpen?: boolean
2020
onCommentsOpen?: () => void
2121

22+
hasPermission: boolean
23+
2224
comments: {
2325
data: {
2426
open: CommentThreadItem[]

0 commit comments

Comments
 (0)
Please sign in to comment.