Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How can i do ``req.user.roles.some((role)=>roles.includes(role))`` in collection access

default discord avatar
vpjmlast year
6

How can I do

req.user.roles.some((role)=>roles.includes(role))

to restrict access to a collection ?



Definition of roles field


{
  name: 'roles',
  type: 'select',
  options: [
    //...
  ],
  hasMany: true,
}


How can i do

req.user.roles.some((role)=>roles.includes(role))

in collection access

  • default discord avatar
    notchrlast year

    I do....



    export const isSuperAdmin: Access<
      any, // eslint-disable-line @typescript-eslint/no-explicit-any
      Admin
    > = ({ req: { user } }) => {
      return Boolean(user?.role.includes('superAdmin'));
    };


    and an example of it on a collection



      access: {
        read: () => true,
        create: isUserAdmin,
        update: isUserAdmin,
        delete: isUserAdmin,
        admin: ({ req: { user } }) => {
          return (
            user.role.includes('userAdmin') || user.role.includes('superAdmin')
          );
        },
      },


    either as an imported fn, or written out

  • default discord avatar
    vpjmlast year

    is it possible with query only ?

  • default discord avatar
    notchrlast year

    hmm wdym by query only?

  • default discord avatar
    vpjmlast year

    I already do it like that, but it's not great because I often have to mix a Drizzle query with post-processing in the then



    https://payloadcms.com/docs/queries/overview#operators
  • default discord avatar
    notchrlast year

    Can you give an example of that?

  • default discord avatar
    vpjmlast year

    something like this but obviously it dont work because access_right.roles is an array



    Thank you for replying to me once again

    :blobchefskiss:
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.