89 lines
1.9 KiB
TypeScript
89 lines
1.9 KiB
TypeScript
import type { CollectionConfig } from 'payload'
|
|
|
|
import { authenticated } from '../../access/authenticated'
|
|
import { isSuperAdmin } from '@/access/admin'
|
|
import { tenantsArrayField } from '@payloadcms/plugin-multi-tenant/fields'
|
|
|
|
export enum UserAccessLevel {
|
|
GUEST = 0,
|
|
CLIENT,
|
|
TRAINER,
|
|
TENANT_ADMIN,
|
|
SUPER_ADMIN,
|
|
FULL_ACCESS,
|
|
}
|
|
|
|
const defaultTenantArrayField = tenantsArrayField({
|
|
tenantsArrayFieldName: 'tenants',
|
|
tenantsArrayTenantFieldName: 'tenant',
|
|
tenantsCollectionSlug: 'tenants',
|
|
arrayFieldAccess: {},
|
|
tenantFieldAccess: {},
|
|
rowFields: [
|
|
{
|
|
name: 'roles',
|
|
type: 'select',
|
|
defaultValue: ['tenant-viewer'],
|
|
hasMany: true,
|
|
options: ['tenant-admin', 'tenant-client', 'tenant-instructor'],
|
|
required: true,
|
|
},
|
|
],
|
|
})
|
|
|
|
|
|
export const Users: CollectionConfig = {
|
|
slug: 'users',
|
|
access: {
|
|
admin: authenticated,
|
|
create: authenticated,
|
|
delete: authenticated,
|
|
read: authenticated,
|
|
update: authenticated,
|
|
},
|
|
admin: {
|
|
defaultColumns: ['name', 'email'],
|
|
useAsTitle: 'username',
|
|
},
|
|
auth: true,
|
|
fields: [
|
|
{
|
|
admin: {
|
|
position: 'sidebar',
|
|
},
|
|
name: 'roles',
|
|
type: 'select',
|
|
defaultValue: ['guest'],
|
|
hasMany: true,
|
|
options: ['full-access', 'super-admin', 'user', 'guest'],
|
|
// access: {
|
|
// update: ({ req }) => {
|
|
// return isSuperAdmin({req})
|
|
// },
|
|
// },
|
|
},
|
|
{
|
|
name: 'username',
|
|
type: 'text',
|
|
// hooks: {
|
|
// beforeValidate: [ensureUniqueUsername],
|
|
// },
|
|
index: true,
|
|
},
|
|
{
|
|
name: 'accessLevel',
|
|
type: 'number',
|
|
max: UserAccessLevel.FULL_ACCESS,
|
|
defaultValue: UserAccessLevel.GUEST,
|
|
},
|
|
{
|
|
...defaultTenantArrayField,
|
|
admin: {
|
|
...(defaultTenantArrayField?.admin || {}),
|
|
position: 'sidebar',
|
|
},
|
|
},
|
|
],
|
|
timestamps: true,
|
|
}
|