When using Access Controls I can for example create a function
isSuperAdminto only let users with a certain role create new content, like.
const isSuperAdmin: Access<any, User> = ({req: {user}}) => {
return (user?.role?.value as Role).hierarchy <= 1
}and
access: {
create: isSuperAdmin,
...and within the function,
userwould be recognised and typed as a
User.
How could I achieve the same without having to create a separate function? The following code works but in the editor the
uservariable is typed as
any(not
User) hence not recognising the
rolefield:
create: ({req: {user}}) => {
return (user?.role?.value as Role).hierarchy <= 1
}I realise I'd probably have to specify
Access<any, User>(or, at least
Usersomewhere) but I can't figure out where.
(I'm aware that creating a function is good for reusability, but the admin case is just an example. Please ignore the reusability aspect when answering the question)
Thanks!
Solution:
create: ({req: {user}}) => {
return ((user as User)?.role?.value as Role).hierarchy <= 1
}Star
Discord
online
Get dedicated engineering support directly from the Payload team.