Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/payload/src/fields/hooks/afterRead/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,19 @@ export const promise = async ({
}

// Set defaultValue on the field for globals being returned without being first created
// or collection documents created prior to having a default
// or collection documents created prior to having a default.

// Skip setting defaults when: global has no ID (never created or filtered by access)
// AND access control is active (not overriding). This prevents default values like
// `_status: 'draft'` from appearing when access control filters out the document.
const shouldSkipDefaults = global && !doc.id && !overrideAccess

if (
!removedFieldValue &&
allowDefaultValue &&
typeof siblingDoc[field.name!] === 'undefined' &&
typeof field.defaultValue !== 'undefined'
typeof field.defaultValue !== 'undefined' &&
!shouldSkipDefaults
) {
siblingDoc[field.name!] = await getDefaultValue({
defaultValue: field.defaultValue,
Expand Down
2 changes: 1 addition & 1 deletion test/versions/globals/DraftWithMax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { draftWithMaxGlobalSlug } from '../slugs.js'

const DraftWithMaxGlobal: GlobalConfig = {
slug: draftWithMaxGlobalSlug,
label: 'Draft Global',
label: 'Draft with Max Global',
admin: {
components: {
views: {
Expand Down
30 changes: 30 additions & 0 deletions test/versions/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2554,6 +2554,36 @@ describe('Versions', () => {
expect(retrieved.title).toStrictEqual('i will be a draft')
})

it('should not return _status field when access control denies read', async () => {
// Create a draft global
const draft = await payload.updateGlobal({
slug: draftGlobalSlug,
data: {
_status: 'draft',
title: 'draft only',
},
draft: true,
})

expect(draft._status).toStrictEqual('draft')

// Create a request without a user (simulating unauthenticated request)
// Access control on draftGlobalSlug requires published status when no user
const req = await createLocalReq({}, payload)
req.user = null

const result = await payload.findGlobal({
slug: draftGlobalSlug,
overrideAccess: false,
req,
})

// Should return empty object, not {_status: 'draft'}
// The _status field should not be populated with its default value
expect(Object.keys(result)).toHaveLength(0)
expect(result._status).toBeUndefined()
})

describe('server functions', () => {
let draftDoc
let event
Expand Down
6 changes: 3 additions & 3 deletions test/versions/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export interface AutosavePost {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down Expand Up @@ -514,7 +514,7 @@ export interface Diff {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand All @@ -529,7 +529,7 @@ export interface Diff {
root: {
type: string;
children: {
type: string;
type: any;
version: number;
[k: string]: unknown;
}[];
Expand Down
Loading