From a27c335113c59e640bf5241357eb0392d076f16a Mon Sep 17 00:00:00 2001 From: tobias Date: Fri, 5 Jul 2024 22:42:32 +0200 Subject: [PATCH] Catch non-arrays --- src/app/(payload)/access/isAdmin.ts | 12 ++++++++++++ src/app/(payload)/access/isEditor.ts | 4 ++++ src/app/(payload)/access/isUser.ts | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/src/app/(payload)/access/isAdmin.ts b/src/app/(payload)/access/isAdmin.ts index 80a979d..d649515 100644 --- a/src/app/(payload)/access/isAdmin.ts +++ b/src/app/(payload)/access/isAdmin.ts @@ -2,6 +2,10 @@ import { Access } from 'payload/types' import type { User } from 'types/payload-types' export const isAdmin = ({ req: { user } }: any) => { + if (!user.roles) { + return false + } + if (user && user.roles?.includes('admin')) { return true } @@ -10,6 +14,10 @@ export const isAdmin = ({ req: { user } }: any) => { } export const isAdminOrCreatedBy = ({ req: { user } }: any) => { + if (user.role) { + return false + } + if (user && user.role === 'admin') { return true } @@ -27,6 +35,10 @@ export const isAdminOrCreatedBy = ({ req: { user } }: any) => { export const isAdminOrSelf = ({ req: { user } }: any) => { if (user) { + if (!user.roles) { + return false + } + if (user.roles?.includes('admin')) { return true } diff --git a/src/app/(payload)/access/isEditor.ts b/src/app/(payload)/access/isEditor.ts index 4a93211..e25dbe5 100644 --- a/src/app/(payload)/access/isEditor.ts +++ b/src/app/(payload)/access/isEditor.ts @@ -2,6 +2,10 @@ import { Access, FieldAccess } from 'payload/types' import type { User } from 'types/payload-types' export const isEditor = ({ req: { user } }: any) => { + if (!user.roles) { + return false + } + if (user && user?.roles?.some((role: string) => ['editor', 'admin'].includes(role))) { return true } diff --git a/src/app/(payload)/access/isUser.ts b/src/app/(payload)/access/isUser.ts index 18cede9..5b9fdde 100644 --- a/src/app/(payload)/access/isUser.ts +++ b/src/app/(payload)/access/isUser.ts @@ -2,6 +2,10 @@ import { Access, FieldAccess } from 'payload/types' import type { User } from 'types/payload-types' export const isUser = ({ req: { user } }: any) => { + if (!user.roles) { + return false + } + if (user && user?.roles?.some((role: string) => ['user', 'editor', 'admin'].includes(role))) { return true }