#Access control query _status throws error

2 messages · Page 1 of 1 (latest)

orchid axle
#

Hi I noticed that using an access control query that checks if _status exists or equals to published does not work when I try to create a parent collection...

I got the following error:

QueryError: The following path cannot be queried: _status
    at Function.buildQuery (xxx\node_modules\payload\src\mongoose\buildQuery.ts:653:15)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at find (xxx\node_modules\payload\src\collections\operations\find.ts:103:17)
    at xxx\node_modules\payload\src\collections\dataloader.ts:78:20
    at async Promise.all (index 0)
    at DataLoader._batchLoadFn (xxx\node_modules\payload\src\collections\dataloader.ts:109:3)  

Here is my custom access control function:

static read: Access<any, any> = ({ req }) => {
    if (this.canLoginAsAdmin({req})) return true;

    return {
      or: [
        {
          _status: {
            exists: false,
          }
        },
        {
          _status: {
            equals: 'published',
          },
        }
      ],
    }
  };

When I remove that query and just return true it all works fine. But with that query condition it throws an error.

This only happens when I try to create an order because the orders collection has a relationship field called products and only the products collection config contains that _status access control from above. Still, it is unexpected that the system throws an error on this because products does have a _status field.

#

Here is part of my orders collection config

{
      name: "items",
      type: "array",
      label: "Items",
      required: true,
      fields: [
        {
          name: "product",
          type: "relationship",
          label: "Product",
          required: true,
          relationTo: "products",
          hasMany: false,
        },
        {
          name: "quantity",
          type: "number",
          label: "Quantity",
          required: true,
          defaultValue: 1,
          min: 1,
        },
      ],
    },