Skip to content

Commit

Permalink
fix: error when opening admin panel without any permissions (#9392)
Browse files Browse the repository at this point in the history
We were getting the following error when opening the create first user
page, as sanitizedPermissions is `{}` if the user has no permissions at
all

![CleanShot 2024-11-20 at 18 48
46@2x](https://github.com/user-attachments/assets/90270a0b-10e5-4f22-a91e-174f82a559b2)
  • Loading branch information
AlessioGr authored Nov 21, 2024
1 parent 1f92b5b commit 9470f9b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 2 additions & 3 deletions packages/payload/src/collections/operations/docAccess.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CollectionPermission, SanitizedCollectionPermission } from '../../auth/index.js'
import type { SanitizedCollectionPermission } from '../../auth/index.js'
import type { AllOperations, PayloadRequest } from '../../types/index.js'
import type { Collection } from '../config/types.js'

Expand Down Expand Up @@ -45,13 +45,12 @@ export async function docAccessOperation(args: Arguments): Promise<SanitizedColl
})

const sanitizedPermissions = sanitizePermissions({
canAccessAdmin: true,
collections: {
[config.slug]: result,
},
})

return sanitizedPermissions.collections[config.slug]
return sanitizedPermissions?.collections?.[config.slug]
} catch (e: unknown) {
await killTransaction(req)
throw e
Expand Down
3 changes: 1 addition & 2 deletions packages/payload/src/globals/operations/docAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ export const docAccessOperation = async (args: Arguments): Promise<SanitizedGlob
await commitTransaction(req)
}
const sanitizedPermissions = sanitizePermissions({
canAccessAdmin: true,
globals: {
[globalConfig.slug]: result,
},
})

return sanitizedPermissions.globals[globalConfig.slug]
return sanitizedPermissions?.globals?.[globalConfig.slug]
} catch (e: unknown) {
await killTransaction(req)
throw e
Expand Down
6 changes: 5 additions & 1 deletion packages/payload/src/utilities/sanitizePermissions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { MarkOptional } from 'ts-essentials'

import type {
CollectionPermission,
FieldPermissions,
Expand Down Expand Up @@ -196,7 +198,9 @@ export function recursivelySanitizeGlobals(obj: Permissions['globals']): void {
/**
* Recursively remove empty objects and false values from an object.
*/
export function sanitizePermissions(data: Permissions): SanitizedPermissions {
export function sanitizePermissions(
data: MarkOptional<Permissions, 'canAccessAdmin'>,
): SanitizedPermissions {
if (data.canAccessAdmin === false) {
delete data.canAccessAdmin
}
Expand Down

0 comments on commit 9470f9b

Please sign in to comment.