All files / src/access checkUIVisibility.ts

100% Statements 18/18
92.3% Branches 24/26
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                    2x   83x   17x     15x     13x 13x 11x         13x         13x                 2x 24x   19x 19x 17x     19x          
/**
 * UI Visibility check utilities
 * Collections should only be visible in the admin UI if the user has 'manage' permission
 */
 
/**
 * Create a hidden function for collections that checks for manage permission
 * @param collectionSlug - The slug of the collection
 * @returns A function that can be used in collection.admin.hidden
 */
export function createUIVisibilityCheck(collectionSlug: string) {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return ({ user }: any) => {
    // Not logged in - hide
    if (!user) return true
 
    // No role - hide
    if (!user.role) return true
 
    // Get permissions from role
    let permissions: string[] = []
    if (typeof user.role === 'object' && user.role.permissions) {
      permissions = user.role.permissions || []
    }
 
    // Check for manage permission (controls UI visibility)
    const hasManagePermission = 
      permissions.includes('*') ||                          // Super admin
      permissions.includes(`${collectionSlug}.*`) ||        // All collection permissions
      permissions.includes(`${collectionSlug}.manage`)      // Specific manage permission
 
    // Return true to hide, false to show
    return !hasManagePermission
  }
}
 
/**
 * Check if user has permission to see a collection in the UI
 * This is a helper for custom implementations
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function canSeeInUI(user: any, collectionSlug: string): boolean {
  if (!user || !user.role) return false
 
  let permissions: string[] = []
  if (typeof user.role === 'object' && user.role.permissions) {
    permissions = user.role.permissions || []
  }
 
  return (
    permissions.includes('*') ||
    permissions.includes(`${collectionSlug}.*`) ||
    permissions.includes(`${collectionSlug}.manage`)
  )
}