Hey!
I am trying to set the access permissions across all of my globals, does anyone know how to do this without having to paste the code into each one? Would additionally like to add a field at this stage.
Have tried a couple methods but feel like I'm missing something obvious.
Thanks!
Just put it into a variable. 😊
eg:
const myCustomAccess = ({ req: { user } }) => {
return Boolean(user)
}
export default buildConfig({
globals: [
{
slug: 'global1',
access: myCustomAccess,
fields: []
},
{
slug: 'global2',
access: myCustomAccess,
fields: []
},
]
})
how would you override it?
Like if for some globals or collections you want to have almost the same logic, but slightly different?
yes, for example I want some specific collections to have a different access
Hey thanks for this, I think I was more wondering if I can just set it once and forget, with the proposed method I would still have to fill out 20 or more objects
appreciate the response :payloadlove:
@Nvskx The beauty of Payload it's that it's all just Typescript, so however you would abstract/override things in Javascript, that's how you're gonna be doing it in Payload. So for example, if only
someof your access logic is reusable, abstract that piece into it's own function and call it where needed.
@Jacko depends on your needs really. For example, if you have 5 globals that all use pretty much the same config, you could make a constructor function. eg:
export const baseGlobal = (slug: string): GlobalConfig => {
return {
slug,
access: () => {}
fields: []
}
}
and then use it like this, for example:
const globals: string[] = ['global1', 'global2', 'global3', 'global4', 'global5'];
export default buildConfig({
globals: globals.map((slug) => baseGlobal(slug))
})
Leaving this for anyone else, in my case I was adding a tenant field and permissions to all my globals:
export const baseGlobal = (existingConfig: GlobalConfig): GlobalConfig => {
return {
slug: existingConfig.slug,
access: {
read: isSuperAdminOrHasSiteAccess(),
update: isSuperAdminOrHasSiteAccess()
},
fields: [
{
name: "tenant",
type: "relationship",
relationTo: "tenants",
admin: {
hidden: true
}
},
...existingConfig.fields
]
}
}
Thanks again @Tinouti !
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.