Add posts full CRUD for editor role

This commit is contained in:
tobias 2024-05-20 09:42:31 +02:00
parent 1ccf660f5b
commit d7f22fdd5f
2 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1,12 @@
import { Access, FieldAccess } from "payload/types";
import { User } from "../payload-types";
export const isEditor: Access<any, User> = ({ 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'));
}

View File

@ -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",