payloadcms / payload

The best way to build a modern backend + admin UI. No black magic, all TypeScript, and fully open-source, Payload is both an app framework and a headless CMS.
https://payloadcms.com
MIT License
21k stars 1.26k forks source link

req.locale incorrect for access control functions #6915

Closed karirogg closed 18 hours ago

karirogg commented 4 days ago

Link to reproduction

No response

Describe the Bug

The access control functions (i.e. the AccessArgs type) include a req object in the request, which includes the locale variable.

In my case, I want to restrict access to some locales for different users, i.e. be able to only allow a user to access a certain locale (f.ex. only allow the user to edit documents on the 'is' locale, but not the ones on 'en').

My attempt at this was to have the access function in the following format:

read: ({ req }) => {
      console.log(req.locale);

      if(req.user.locale === req.locale) return true;

      return false;
}

However, when I load the dashboard on the 'is' locale, the function is called with req.locale = 'en', while it naturally should be 'is'.

Additionally, the access functions do not get called when the locale state updates, i.e. when I change the locale in the dashboard.

To Reproduce

I wrote a small create-payload-app repo that recreates the bug: https://github.com/karirogg/payload-access-control-locale-bug.

In short, I added this collection to the blank template:

import { CollectionConfig } from "payload/types";

export const MyCollection: CollectionConfig = {
  slug: "my-collection",
  access: {
    read: ({ req }) => {
      console.log(req.locale);

      return true;
    },
  },
  fields: [
    {
      name: "title",
      type: "text",
    },
  ],
};

and added the following to the payload.config.ts:

localization: {
    locales: ["en", "is"],
    defaultLocale: "en",
  }

When I refresh the page on the 'is' part of the dashboard, this function logs the locale being 'en'.

Payload Version

2.22.0

Adapters and Plugins

bundler-webpack, db-mongodb, plugin-cloud, richtext-slate

JarrodMFlesch commented 18 hours ago

@karirogg this will be included in the next 2.x release.