Skip to content

Commit

Permalink
fix(core): update documentPairPermission to allow for versions
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Feb 11, 2025
1 parent 8e1ed89 commit 76b7b11
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface PairPermissionsOptions {
grantsStore: GrantsStore
permission: DocumentPermission
draft: SanityDocument | null
version: SanityDocument | null
published: SanityDocument | null
liveEdit: boolean
}
Expand All @@ -41,6 +42,7 @@ function getPairPermissions({
grantsStore,
permission,
draft,
version,
published,
liveEdit,
}: PairPermissionsOptions): Array<[string, Observable<PermissionCheckResult>]> {
Expand All @@ -51,8 +53,10 @@ function getPairPermissions({
//
// note: this should _not_ be used if the draft and published versions should
// be considered separately/explicitly in the permissions.
const effectiveVersion = draft || published
const effectiveVersionType = effectiveVersion === draft ? 'draft' : 'published'
const effectiveVersion = version || draft || published
const effectiveVersionType =
// eslint-disable-next-line no-nested-ternary
effectiveVersion === version ? version : effectiveVersion === draft ? 'draft' : 'published'

const {checkDocumentPermission} = grantsStore

Expand All @@ -76,6 +80,12 @@ function getPairPermissions({
return [['delete draft document', checkDocumentPermission('update', draft)]]
}

case 'discardVersion': {
if (liveEdit) return []

return [['delete version', checkDocumentPermission('update', version || null)]]
}

case 'publish': {
if (liveEdit) return []

Expand Down Expand Up @@ -157,6 +167,7 @@ function getPairPermissions({
export type DocumentPermission =
| 'delete'
| 'discardDraft'
| 'discardVersion'
| 'publish'
| 'unpublish'
| 'update'
Expand Down Expand Up @@ -190,7 +201,7 @@ export function getDocumentPairPermissions({
permission,
type,
serverActionsEnabled,
version,
version: v,
pairListenerOptions,
}: DocumentPairPermissionsOptions): Observable<PermissionCheckResult> {
// this case was added to fix a crash that would occur if the `schemaType` was
Expand All @@ -206,21 +217,24 @@ export function getDocumentPairPermissions({

return snapshotPair(
client,
getIdPair(id, {version}),
getIdPair(id, {version: v}),
type,
serverActionsEnabled,
pairListenerOptions,
).pipe(
switchMap((pair) =>
combineLatest([pair.draft.snapshots$, pair.published.snapshots$]).pipe(
map(([draft, published]) => ({draft, published})),
),
combineLatest([
pair.draft.snapshots$,
pair.published.snapshots$,
pair.version?.snapshots$ || of(null),
]).pipe(map(([draft, published, version]) => ({draft, published, version}))),
),
switchMap(({draft, published}) => {
switchMap(({draft, published, version}) => {
const pairPermissions = getPairPermissions({
grantsStore,
permission,
draft,
version,
published,
liveEdit,
}).map(([label, observable]) =>
Expand Down

0 comments on commit 76b7b11

Please sign in to comment.