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
14 changes: 11 additions & 3 deletions packages/payload/src/globals/operations/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type GlobalFindOneArgs = {
*/
data?: Record<string, unknown>
depth?: number
disableErrors?: boolean
draft?: boolean
globalConfig: SanitizedGlobalConfig
includeLockStatus?: boolean
Expand All @@ -45,6 +46,7 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
const {
slug,
depth,
disableErrors,
draft: replaceWithVersion = false,
flattenLocales,
globalConfig,
Expand Down Expand Up @@ -86,11 +88,14 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
let accessResult!: AccessResult

if (!overrideAccess) {
accessResult = await executeAccess({ req }, globalConfig.access.read)
accessResult = await executeAccess({ disableErrors, req }, globalConfig.access.read)
}

if (accessResult === false) {
throw new NotFound(req.t)
if (!disableErrors) {
throw new NotFound(req.t)
}
return null!
}

const select = sanitizeSelect({
Expand Down Expand Up @@ -125,7 +130,10 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
const hasDoc = docFromDB && Object.keys(docFromDB).length > 0

if (!hasDoc && !args.data && !overrideAccess && accessResult !== true) {
return {} as any
if (!disableErrors) {
return {} as any
}
return null!
}

let doc = (args.data as any) ?? (hasDoc ? docFromDB : null) ?? {}
Expand Down
10 changes: 10 additions & 0 deletions packages/payload/src/globals/operations/local/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ type BaseFindOneOptions<TSlug extends GlobalSlug, TSelect extends SelectType> =
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
*/
depth?: number
/**
* When set to `true`, errors will not be thrown.
*/
disableErrors?: boolean
/**
* Whether the document should be queried from the versions table/collection or not. [More](https://payloadcms.com/docs/versions/drafts#draft-api)
*/
draft?: boolean
/**
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
*/
Expand Down Expand Up @@ -98,6 +106,7 @@ export async function findOneGlobalLocal<
slug: globalSlug,
data,
depth,
disableErrors,
draft = false,
flattenLocales,
includeLockStatus,
Expand All @@ -117,6 +126,7 @@ export async function findOneGlobalLocal<
slug: globalSlug as string,
data,
depth,
disableErrors,
draft,
flattenLocales,
globalConfig,
Expand Down
10 changes: 10 additions & 0 deletions test/globals/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ describe('globals', () => {
expect(es).toMatchObject(localized.es)
})

it('should return null when user is unauthorised and using findGlobal with disableErrors: true', async () => {
const doc = await payload.findGlobal({
disableErrors: true,
overrideAccess: false,
slug: accessControlSlug,
})

expect(doc).toBeNull()
})

it('should respect valid access query constraint', async () => {
const emptyGlobal = await payload.findGlobal({
overrideAccess: false,
Expand Down