From d7f22fdd5fb483a5736a9b3c3afec2b02ed22fb7 Mon Sep 17 00:00:00 2001 From: tobias Date: Mon, 20 May 2024 09:42:31 +0200 Subject: [PATCH] Add posts full CRUD for editor role --- payload/src/access/isEditor.ts | 12 ++++++++++++ payload/src/collections/Posts.ts | 12 ++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 payload/src/access/isEditor.ts diff --git a/payload/src/access/isEditor.ts b/payload/src/access/isEditor.ts new file mode 100644 index 0000000..ad163eb --- /dev/null +++ b/payload/src/access/isEditor.ts @@ -0,0 +1,12 @@ +import { Access, FieldAccess } from "payload/types"; +import { User } from "../payload-types"; + +export const isEditor: Access = ({ req: { user } }) => { + // Return true or false based on if the user has an editor role + return Boolean(user?.roles?.includes('editor', 'admin')); +} + +export const isEditorFieldLevel: FieldAccess<{ id: string }, unknown, User> = ({ req: { user } }) => { + // Return true or false based on if the user has an editor role + return Boolean(user?.roles?.includes('editor', 'admin')); +} \ No newline at end of file diff --git a/payload/src/collections/Posts.ts b/payload/src/collections/Posts.ts index fbca324..45caa48 100644 --- a/payload/src/collections/Posts.ts +++ b/payload/src/collections/Posts.ts @@ -1,4 +1,8 @@ import { CollectionConfig } from "payload/types"; +import { isAdmin } from "@/access/isAdmin"; +import { isEditor } from "@/access/isEditor"; +import { isSSG } from "@/access/isSSG"; + const Posts: CollectionConfig = { slug: "posts", versions: true, @@ -7,9 +11,11 @@ const Posts: CollectionConfig = { useAsTitle: "title", }, access: { + //TODO: Author can CRUD own post + create: isEditor, read: () => true, - create: () => true, - update: () => true, + update: isEditor, + delete: isEditor, }, hooks: { afterChange: [ @@ -84,6 +90,7 @@ const Posts: CollectionConfig = { }, { name: "author", + //TODO: Add active user as default type: 'relationship', relationTo: 'users', hasMany: false, @@ -95,6 +102,7 @@ const Posts: CollectionConfig = { { name: "status", type: "select", + required: true, options: [ { value: "draft",