diff --git a/payload/src/access/isUser.ts b/payload/src/access/isUser.ts new file mode 100644 index 0000000..548953c --- /dev/null +++ b/payload/src/access/isUser.ts @@ -0,0 +1,13 @@ +import { Access, FieldAccess } from "payload/types"; +import { User } from "../payload-types"; + +export const isUser: Access = ({ req: { user } }) => { + // Return true or false based on if the user has an ssg or admin role + return Boolean(user?.roles?.some(role => ['user', 'editor', 'admin'].includes(role))); + +} + +export const isUserFieldLevel: FieldAccess<{ id: string }, unknown, User> = ({ req: { user } }) => { + // Return true or false based on if the user has an ssg or admin role + return Boolean(user?.roles?.some(role => ['user', 'editor', 'admin'].includes(role))); +} \ No newline at end of file diff --git a/payload/src/collections/Authors.ts b/payload/src/collections/Authors.ts new file mode 100644 index 0000000..a09e2f8 --- /dev/null +++ b/payload/src/collections/Authors.ts @@ -0,0 +1,44 @@ +import { CollectionConfig } from "payload/types"; +import { isAdmin } from "@/access/isAdmin"; +import { isEditor } from "@/access/isEditor"; +import { isSSG } from "@/access/isSSG"; +import { isUser } from "@/access/isUser"; +import { isEditorOrSelf } from "@/access/isEditorOrSelf"; + +const Authors: CollectionConfig = { + slug: "authors", + admin: { + defaultColumns: ["avatar","name"], + useAsTitle: "name", + }, + access: { + //TODO: Author can CRUD own post + create: isUser, + read: () => true, + update: isEditor, + delete: isEditor, + }, + fields: [ + { + name: "name", + type: "text", + required: true + }, + { + name: "avatar", + type: "upload", + relationTo: "media", + required: false, + }, + { + name: "user", + type: "relationship", + relationTo: "users", + admin: { + description: 'The selected user will be able to edit this author' + }, + } + ], +}; + +export default Authors; diff --git a/payload/src/collections/Posts.ts b/payload/src/collections/Posts.ts index 45caa48..1c931ee 100644 --- a/payload/src/collections/Posts.ts +++ b/payload/src/collections/Posts.ts @@ -2,6 +2,7 @@ import { CollectionConfig } from "payload/types"; import { isAdmin } from "@/access/isAdmin"; import { isEditor } from "@/access/isEditor"; import { isSSG } from "@/access/isSSG"; +import { isUser } from "@/access/isUser"; const Posts: CollectionConfig = { slug: "posts", @@ -12,7 +13,7 @@ const Posts: CollectionConfig = { }, access: { //TODO: Author can CRUD own post - create: isEditor, + create: isUser, read: () => true, update: isEditor, delete: isEditor, @@ -89,12 +90,11 @@ const Posts: CollectionConfig = { }, }, { - name: "author", + name: "authors", //TODO: Add active user as default type: 'relationship', - relationTo: 'users', - hasMany: false, - required: false, + relationTo: 'authors', + hasMany: true, admin: { position: "sidebar", }, diff --git a/payload/src/collections/Users.ts b/payload/src/collections/Users.ts index feaa7c8..333b395 100644 --- a/payload/src/collections/Users.ts +++ b/payload/src/collections/Users.ts @@ -35,10 +35,6 @@ const Users: CollectionConfig = { update: isAdminFieldLevel, }, }, - { - name: 'name', - type: 'text', - } ], }; diff --git a/payload/src/payload.config.ts b/payload/src/payload.config.ts index dcdbb61..92cb7da 100644 --- a/payload/src/payload.config.ts +++ b/payload/src/payload.config.ts @@ -2,6 +2,7 @@ import { buildConfig } from "payload/config"; import path from "path"; import Posts from "@/collections/Posts"; import Users from "@/collections/Users"; +import Authors from "./collections/Authors"; import Media from "@/collections/Media"; export default buildConfig({ @@ -19,7 +20,7 @@ export default buildConfig({ }, }), }, - collections: [Posts, Users, Media], + collections: [Posts, Users, Authors, Media], typescript: { outputFile: path.resolve("/", "types.ts"), },