Restrict user collection to admins

This commit is contained in:
tobias 2024-05-19 23:17:39 +02:00
parent 40d257a0eb
commit f017fa28dc

View File

@ -1,5 +1,7 @@
import { CollectionConfig } from 'payload/types';
const isAdmin = ({ req: { user } }) => (user && user.role === 'admin');
const Users: CollectionConfig = {
slug: 'users',
auth: true,
@ -7,10 +9,29 @@ const Users: CollectionConfig = {
useAsTitle: 'email',
},
access: {
read: () => true,
read: isAdmin,
},
fields: [
// Email added by default
fields: [
{
name: 'role',
type: 'select',
options: [
{ label: 'ssg', value: 'ssg' }, //cRud
{ label: 'Admin', value: 'admin' }, //CRUD, role creation
{ label: 'Editor', value: 'editor' }, //CRUD
{ label: 'User', value: 'user' }, //CRUd
],
required: true,
defaultValue: "user",
access: {
create: isAdmin,
read: isAdmin,
update: isAdmin,
},
admin: {
readOnly: !isAdmin
}
},
{
name: 'name',
type: 'text',