Replies: 2 comments 1 reply
-
We used plugin to handle that: const addAccess: Plugin = (incomingConfig: Config): Config => { const addEntityOptions= (obj:Field[]) =>{ let config: Config = { config = { `import type { CollectionConfig } from 'payload/types' export const Roles: CollectionConfig = { |
Beta Was this translation helpful? Give feedback.
-
import { Access } from 'payload';
import { RolePermissions, User } from '@/payload-types'
import { Role } from '@/payload-types';
export const canUserAccessAction = (user: User | null | undefined, slugName: string, action: string) => {
if (user) {
if (user.isAdmin) return true;
if (!user.userRoles || user.userRoles.length == 0) return false;
const userRoles: Role[] = user.userRoles as Role[];
// if (! userRoles[0].permissions) return false;
const permissions: (RolePermissions | undefined) = userRoles[0].permissions
if (!permissions) return false
for (const permission of permissions)
if (((permission.entity as string[]).includes(slugName)) && ((permission.type as string[]).includes(action))) return true;
}
return false
}
export const hasAccessToAction =
(slugName: string, action: string): Access =>
({ req: { user } }) => {
if (user && ('userRoles' in user || 'isAdmin' in user)) {
const cmuser = user as unknown
return canUserAccessAction(cmuser as User, slugName, action);
}else{
return false
}
}
```
and the usage:
```
const createAccess = (slugName: string, labelName?: string) => {
const baseAccess: { read: Access; update: Access; } = {
read: hasAccessToAction(slugName, 'read'),
update: hasAccessToAction(slugName, 'write'),
}
const label = labelName ? labelName : slugName.charAt(0).toUpperCase() + slugName.slice(1)
entities.push({ label, value: slugName })
return baseAccess
}
//For Collections create and delete exists
const createAccessCollection = (slugName: string, labelName?: string) => {
const baseAccess: { read: Access; update: Access; create?: Access; delete?: Access; } = createAccess(slugName, labelName)
const access = {
...baseAccess,
create: hasAccessToAction(slugName, 'write'),
delete: hasAccessToAction(slugName, 'write'),
}
return access
} |
Beta Was this translation helpful? Give feedback.
-
Is there any way I can use a dynamic list of collections in to create a select field's options?
In my user collection, I want to have a field called
editableCollections
that I check against in my access functions. The idea being that we have an event coordinator that can edit theEvents
collection but not theProducts
collection, for example.Here is what I have for the field so far:
However, when I use the same
collections
object used in the payload config, the Users collection is included and I'm guessing there's a circular dependency issue because I get an error that isn't really an error:Is there some better way to get a list of all collections that doesn't need to be updated if a new collection is added?
Beta Was this translation helpful? Give feedback.
All reactions