Providing Specific Options in Select Field Based on User Role

default discord avatar
loaialsharee
2 weeks ago
3

I am having a case where I have multiple users (for example Admin, Chef & Customers). The admin can see both the Chef and the Customers users, while the Chef can only see the customers.



I need to give the Chef the ability to create users with roles

customers

only. Currently using the

select

field, the options shown are

Admin

,

Chef

and

Customers

- but I only want to limit the Chef account to see

Customers

option only. How can I achieve that?



const Users: CollectionConfig = {
  slug: 'users',
  auth: true,
  admin: {
    useAsTitle: 'email',
  },
  labels: {
    plural: 'Admins',
    singular: 'Admin',
  },
  access: {
    read: isAuth,
    update: isAuth,
    delete: isAuth,
    create: isAuth,
  },
  fields: [
    {
      name: 'name',
      type: 'text',
    },
    {
      name: 'role',
      type: 'select',
      options: [
        // how to filter here based on roles?
        { label: 'Admin', value: ROLES.ADMIN },
        { label: 'Chef', value: ROLES.CHEF },
        { label: 'Customer', value: ROLES.CUSTOMER },
      ],
      required: true,
      defaultValue: ROLES.CUSTOMER,
    },
  ],
};

export default Users;
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.