diff --git a/src/access/admin.ts b/src/access/admin.ts new file mode 100644 index 0000000..e6aafd0 --- /dev/null +++ b/src/access/admin.ts @@ -0,0 +1,7 @@ +import { User } from '@/payload-types' +import type { AccessArgs } from 'payload' + +type isAdmin = (args: AccessArgs) => boolean + +export const admin: isAdmin = ({ req: { user } }) => user?.role === 'admin' + diff --git a/src/access/anyone.ts b/src/access/anyone.ts new file mode 100644 index 0000000..bf37c3a --- /dev/null +++ b/src/access/anyone.ts @@ -0,0 +1,3 @@ +import type { Access } from 'payload' + +export const anyone: Access = () => true diff --git a/src/access/authenticated.ts b/src/access/authenticated.ts new file mode 100644 index 0000000..e2dc34d --- /dev/null +++ b/src/access/authenticated.ts @@ -0,0 +1,9 @@ +import type { AccessArgs } from 'payload' + +import type { User } from '@/payload-types' + +type isAuthenticated = (args: AccessArgs) => boolean + +export const authenticated: isAuthenticated = ({ req: { user } }) => { + return Boolean(user) +} diff --git a/src/collections/Authors/Authors.ts b/src/collections/Authors/Authors.ts index bec6ec6..24cc4d5 100644 --- a/src/collections/Authors/Authors.ts +++ b/src/collections/Authors/Authors.ts @@ -1,3 +1,5 @@ +import { admin } from '@/access/admin' +import { authenticated } from '@/access/authenticated' import type { CollectionConfig } from 'payload' export const Authors: CollectionConfig = { @@ -10,9 +12,9 @@ export const Authors: CollectionConfig = { }, access: { read: () => true, - update: () => true, - create: () => true, - admin: () => true, + update: admin, + create: authenticated, + delete: admin, }, fields: [ { diff --git a/src/collections/Books/Books.ts b/src/collections/Books/Books.ts index 48cb529..3cdaf65 100644 --- a/src/collections/Books/Books.ts +++ b/src/collections/Books/Books.ts @@ -1,3 +1,5 @@ +import { admin } from "@/access/admin"; +import { authenticated } from "@/access/authenticated"; import { CollectionConfig } from "payload"; export const Books: CollectionConfig = { @@ -7,9 +9,9 @@ export const Books: CollectionConfig = { }, access: { read: () => true, - update: () => true, - create: () => true, - admin: () => true, + update: admin, + create: authenticated, + delete: admin, }, fields: [ { diff --git a/src/collections/Books/Genre.ts b/src/collections/Books/Genre.ts index 8792347..1ed3482 100644 --- a/src/collections/Books/Genre.ts +++ b/src/collections/Books/Genre.ts @@ -1,3 +1,4 @@ +import { admin } from "@/access/admin"; import { CollectionConfig } from "payload"; export const Genre: CollectionConfig = { @@ -7,9 +8,9 @@ export const Genre: CollectionConfig = { }, access: { read: () => true, - update: () => true, + update: admin, create: () => true, - admin: () => true, + delete: admin, }, fields: [ { diff --git a/src/collections/Checkouts/Checkouts.ts b/src/collections/Checkouts/Checkouts.ts index e1ec68b..4797ea2 100644 --- a/src/collections/Checkouts/Checkouts.ts +++ b/src/collections/Checkouts/Checkouts.ts @@ -1,7 +1,15 @@ +import { admin } from "@/access/admin"; +import { authenticated } from "@/access/authenticated"; import { CollectionConfig } from "payload"; const Checkouts: CollectionConfig = { slug: 'checkouts', + access: { + read: () => true, + update: admin, + create: authenticated, + delete: admin, + }, fields: [ { name: 'fromHold', diff --git a/src/collections/Checkouts/HoldRequests.ts b/src/collections/Checkouts/HoldRequests.ts index cb6a0c7..23c8d1d 100644 --- a/src/collections/Checkouts/HoldRequests.ts +++ b/src/collections/Checkouts/HoldRequests.ts @@ -1,7 +1,15 @@ +import { admin } from "@/access/admin"; +import { authenticated } from "@/access/authenticated"; import { CollectionConfig } from "payload"; const HoldRequests: CollectionConfig = { slug: 'holdRequests', + access: { + read: () => true, + update: admin, + create: authenticated, + delete: admin, + }, fields: [ { name: 'copy', diff --git a/src/collections/Copies/Copies.ts b/src/collections/Copies/Copies.ts index 76e437f..5abae7b 100644 --- a/src/collections/Copies/Copies.ts +++ b/src/collections/Copies/Copies.ts @@ -28,17 +28,10 @@ const beforeValidate: CollectionBeforeValidateHook = async ({ data, req }) => { } else return doc return { ...doc, label: `[${repositoryName}] ${bookName}` } - } export const Copies: CollectionConfig = { slug: 'copies', - access: { - read: () => true, - update: () => true, - create: () => true, - admin: () => true, - }, admin: { useAsTitle: 'label', pagination: { @@ -96,6 +89,7 @@ export const Copies: CollectionConfig = { } ], hooks: { - beforeValidate: [beforeValidate] + beforeValidate: [beforeValidate], + //beforeRead: [beforeRead], }, } diff --git a/src/collections/Repositories/Repositories.ts b/src/collections/Repositories/Repositories.ts index 85c8872..5f5cb76 100644 --- a/src/collections/Repositories/Repositories.ts +++ b/src/collections/Repositories/Repositories.ts @@ -1,3 +1,5 @@ +import { admin } from "@/access/admin"; +import { authenticated } from "@/access/authenticated"; import { CollectionConfig } from "payload"; export const Repositories: CollectionConfig = { @@ -5,6 +7,12 @@ export const Repositories: CollectionConfig = { admin: { useAsTitle: 'name' }, + access: { + read: () => true, + update: admin, + create: authenticated, + delete: admin, + }, fields: [ { name: 'name', diff --git a/src/collections/Users.ts b/src/collections/Users.ts index 2bb211a..7fb3e4e 100644 --- a/src/collections/Users.ts +++ b/src/collections/Users.ts @@ -1,4 +1,4 @@ -import { defaultAccess } from '@/lib/utils' +import { admin } from '@/access/admin' import type { CollectionConfig } from 'payload' const expirationInMinutes = parseInt(process.env.PASSWORD_RESET_EXPIRATION_IN_MINUTES || '30') @@ -9,6 +9,9 @@ export const Users: CollectionConfig = { admin: { useAsTitle: 'email', }, + access: { + admin: admin + }, auth: { // verify: { // generateEmailSubject: () => { @@ -26,7 +29,6 @@ export const Users: CollectionConfig = { // // // ` - // // }, // }, forgotPassword: { @@ -54,14 +56,6 @@ export const Users: CollectionConfig = { }, } }, - access: { - ...defaultAccess, - update: ({ req, data }) => { - if (req.user?.role === 'admin') return true - else if (data?.user.id === req.user?.id) return true - else return false - } - }, fields: [ // Email added by default // Add more fields as needed diff --git a/src/components/ForgotPasswordForm.tsx b/src/components/ForgotPasswordForm.tsx index 3e7b901..8ac9399 100644 --- a/src/components/ForgotPasswordForm.tsx +++ b/src/components/ForgotPasswordForm.tsx @@ -88,10 +88,7 @@ export function ForgotPasswordForm({ className, ...props }: React.ComponentProps -
+
{!token ? (