Add collections & access functions from paystro

This commit is contained in:
tobias 2024-06-23 10:23:51 +02:00
parent cab583964b
commit e002125805
7 changed files with 296 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import { Access, FieldAccess } from 'payload/types'
import type { User } from 'types/payload-types'
export const isAdmin = ({ req: { user } }) => {
if (user && user.roles?.includes('admin')) {
return true
}
return false
}
export const isAdminOrCreatedBy = ({ req: { user } }) => {
if (user && user.role === 'admin') {
return true
}
if (user) {
return {
createdBy: {
equals: user.id,
},
}
}
return false
}
export const isAdminOrSelf = ({ req: { user } }) => {
if (user) {
if (user.roles?.includes('admin')) {
return true
}
// Non-admin: can only access themselves
return {
id: {
equals: user.id,
},
}
}
return false
}

View File

@ -0,0 +1,10 @@
import { Access, FieldAccess } from 'payload/types'
import type { User } from 'types/payload-types'
export const isEditor = ({ req: { user } }) => {
if (user?.roles?.some((role) => ['editor', 'admin'].includes(role))) {
return true
}
return false
}

View File

@ -0,0 +1,10 @@
import { Access, FieldAccess } from 'payload/types'
import type { User } from 'types/payload-types'
export const isUser = ({ req: { user } }) => {
if (user?.roles?.some((role) => ['user', 'editor', 'admin'].includes(role))) {
return true
}
return false
}

View File

@ -0,0 +1,47 @@
import { CollectionConfig } from 'payload/types'
import { isEditor } from '@payload/access/isEditor'
import { isUser } from '@payload/access/isUser'
const Authors: CollectionConfig = {
slug: 'authors',
admin: {
defaultColumns: ['name'],
useAsTitle: 'name',
},
access: {
//TODO: Author can CRUD own post
create: isUser,
read: () => true,
update: isEditor,
delete: isEditor,
},
fields: [
{
name: 'avatar',
type: 'upload',
relationTo: 'media',
required: true,
},
{
name: 'name',
type: 'text',
required: true,
},
{
name: 'bio',
type: 'text',
required: false,
},
{
name: 'user',
type: 'upload',
relationTo: 'users',
admin: {
description: 'The selected user will be able to edit this author',
},
},
],
}
export default Authors

View File

@ -0,0 +1,23 @@
import { isUser } from '@payload/access/isUser'
import { CollectionConfig } from 'payload/types'
export const Media: CollectionConfig = {
slug: 'media',
admin: {},
access: {
read: (): boolean => true,
create: isUser,
update: isUser,
delete: isUser,
},
upload: true,
fields: [
{
name: 'alt',
type: 'text',
required: true,
},
],
}
export default Media

View File

@ -0,0 +1,124 @@
import { CollectionConfig } from 'payload/types'
import { isEditor } from '@payload/access/isEditor'
import { isUser } from '@payload/access/isUser'
const Posts: CollectionConfig = {
slug: 'posts',
versions: true,
admin: {
defaultColumns: ['title', 'author', 'status'],
useAsTitle: 'title',
},
access: {
//TODO: Author can CRUD own post
create: isUser,
read: () => true,
update: isEditor,
delete: isEditor,
},
hooks: {
afterChange: [
async () => {
console.log(process.env.TOKEN)
try {
process.env.NODE_ENV !== 'development' &&
console.log(
await fetch(`${process.env.DRONE_URL}/api/repos/${process.env.REPOSITORY}/builds`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.TOKEN}`,
},
}),
)
} catch (e) {
console.log(e)
}
},
],
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'summary',
type: 'text',
required: false,
},
{
name: 'publishedDate',
type: 'date',
required: false,
admin: {
position: 'sidebar',
},
},
{
name: 'thumbnail',
type: 'upload',
relationTo: 'media',
required: true,
},
/* {
name: 'content',
type: 'richText',
editor: slateEditor({
admin: {
elements: ['h2', 'h3', 'h4', 'link', 'ol', 'ul', 'upload', 'blockquote', 'indent'],
leaves: ['bold', 'italic', 'underline', 'strikethrough'],
upload: {
collections: {
media: {
fields: [
{
name: 'image',
type: 'upload',
relationTo: 'media',
required: true,
},
],
},
},
},
},
}),
}, */
{
name: 'author',
//TODO: Add active user as default
type: 'relationship',
relationTo: 'authors',
admin: {
position: 'sidebar',
},
},
{
name: 'status',
type: 'select',
required: true,
options: [
{
value: 'draft',
label: 'Draft',
},
{
value: 'published',
label: 'Published',
},
{
value: 'archived',
label: 'Archived',
},
],
defaultValue: 'draft',
admin: {
position: 'sidebar',
},
},
],
}
export default Posts

View File

@ -0,0 +1,40 @@
import { CollectionConfig } from 'payload/types'
import { isAdmin, isAdminOrSelf } from '@payload/access/isAdmin'
const Users: CollectionConfig = {
slug: 'users',
auth: true,
admin: {
defaultColumns: ['roles', 'email'],
useAsTitle: 'email',
},
access: {
create: isAdmin,
read: isAdminOrSelf,
update: isAdminOrSelf,
delete: isAdmin,
},
fields: [
{
name: 'roles',
type: 'select',
options: [
{ label: 'Admin', value: 'admin' }, //CRUD, role creation
{ label: 'Editor', value: 'editor' }, //CRUD
{ label: 'User', value: 'user' }, //cRud, CRUD own entries
],
required: true,
defaultValue: 'user',
// JWT so that role is accessible from 'req.user'
saveToJWT: true,
hasMany: true,
access: {
create: isAdmin,
read: () => true,
update: isAdmin,
},
},
],
}
export default Users