Files
payload/test/plugin-multi-tenant/collections/MenuItems.ts
Jarrod Flesch 5cf215d9cb feat(plugin-multi-tenant): visible tenant field on documents (#13379)
The goal of this PR is to show the selected tenant on the document level
instead of using the global selector to sync the state to the document.


Should merge https://github.com/payloadcms/payload/pull/13316 before
this one.

### Video of what this PR implements

**Would love feedback!**


https://github.com/user-attachments/assets/93ca3d2c-d479-4555-ab38-b77a5a9955e8
2025-08-21 13:15:24 -04:00

97 lines
2.1 KiB
TypeScript

import type { Access, CollectionConfig, Where } from 'payload'
import { getUserTenantIDs } from '@payloadcms/plugin-multi-tenant/utilities'
import { menuItemsSlug } from '../shared.js'
const collectionTenantReadAccess: Access = ({ req }) => {
// admins can access all tenants
if (req?.user?.roles?.includes('admin')) {
return true
}
if (req.user) {
const assignedTenants = getUserTenantIDs(req.user, {
tenantsArrayFieldName: 'tenants',
tenantsArrayTenantFieldName: 'tenant',
})
// if the user has assigned tenants, add id constraint
if (assignedTenants.length > 0) {
return {
or: [
{
tenant: {
in: assignedTenants,
},
},
{
'tenant.isPublic': {
equals: true,
},
},
],
}
}
}
// if the user has no assigned tenants, return a filter that allows access to public tenants
return {
'tenant.isPublic': {
equals: true,
},
} as Where
}
const collectionTenantUpdateAndDeleteAccess: Access = ({ req }) => {
// admins can update all tenants
if (req?.user?.roles?.includes('admin')) {
return true
}
if (req.user) {
const assignedTenants = getUserTenantIDs(req.user, {
tenantsArrayFieldName: 'tenants',
tenantsArrayTenantFieldName: 'tenant',
})
// if the user has assigned tenants, add id constraint
if (assignedTenants.length > 0) {
return {
tenant: {
in: assignedTenants,
},
}
}
}
return false
}
export const MenuItems: CollectionConfig = {
slug: menuItemsSlug,
access: {
read: collectionTenantReadAccess,
create: ({ req }) => {
return Boolean(req?.user?.roles?.includes('admin') || req.user?.tenants?.length)
},
update: collectionTenantUpdateAndDeleteAccess,
delete: collectionTenantUpdateAndDeleteAccess,
},
admin: {
useAsTitle: 'name',
group: 'Tenant Collections',
},
fields: [
{
name: 'name',
type: 'text',
required: true,
},
{
name: 'content',
type: 'richText',
},
],
}