All files / modules/20-rbac/factories RbacFactory.tsx

25% Statements 5/20
0% Branches 0/10
33.33% Functions 3/9
25% Lines 5/20

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                  493x 493x               8x                                                       59x                       493x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type React from 'react'
import type { IconName } from '@harness/uicore'
import type { ResourceType, ResourceCategory } from '@rbac/interfaces/ResourceType'
import type { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { ResourceScope } from '@rbac/interfaces/ResourceScope'
import type { StringsMap } from 'framework/strings/StringsContext'
 
export interface RbacResourceModalProps {
  searchTerm: string
  selectedData: string[]
  onSelectChange: (items: string[]) => void
  resourceScope: ResourceScope
}
export interface RbacResourceRendererProps {
  identifiers: string[]
  resourceScope: ResourceScope
  onResourceSelectionChange: (resourceType: ResourceType, isAdd: boolean, identifiers?: string[] | undefined) => void
  resourceType: ResourceType
}
export interface ResourceHandler {
  icon: IconName
  label: keyof StringsMap
  labelOverride?: keyof StringsMap | undefined
  permissionLabels?: {
    [key in PermissionIdentifier]?: string | React.ReactElement
  }
  addResourceModalBody?: (props: RbacResourceModalProps) => React.ReactElement
  staticResourceRenderer?: (props: RbacResourceRendererProps) => React.ReactElement
  category?: ResourceCategory
}
 
export interface ResourceCategoryHandler {
  icon: IconName
  label: keyof StringsMap
  resourceTypes?: Set<ResourceType>
}
 
class RbacFactory {
  private map: Map<ResourceType, ResourceHandler>
  private resourceCategoryMap: Map<ResourceCategory, ResourceCategoryHandler>
 
  constructor() {
    this.map = new Map()
    this.resourceCategoryMap = new Map()
  }
 
  registerResourceCategory(resourceCategory: ResourceCategory, handler: ResourceCategoryHandler): void {
    this.resourceCategoryMap.set(resourceCategory, handler)
  }
 
  registerResourceTypeHandler(resourceType: ResourceType, handler: ResourceHandler): void {
    this.map.set(resourceType, handler)
  }
 
  getResourceCategoryList(resources: ResourceType[]): Map<ResourceCategory | ResourceType, ResourceType[] | undefined> {
    const categoryMap: Map<ResourceCategory | ResourceType, ResourceType[] | undefined> = new Map()
 
    resources.map(resourceType => {
      const handler = this.map.get(resourceType)
      if (handler) {
        if (handler.category) {
          const resourceTypes = categoryMap.get(handler.category)
          if (resourceTypes) {
            categoryMap.set(handler.category, [...resourceTypes, resourceType])
          } else categoryMap.set(handler.category, [resourceType])
        } else {
          categoryMap.set(resourceType, undefined)
        }
      }
    })
 
    return categoryMap
  }
 
  getResourceCategoryHandler(resourceCategory: ResourceCategory): ResourceCategoryHandler | undefined {
    return this.resourceCategoryMap.get(resourceCategory)
  }
 
  getResourceTypeHandler(resourceType: ResourceType): ResourceHandler | undefined {
    return this.map.get(resourceType)
  }
 
  getResourceTypeLabelKey(resourceType: ResourceType): keyof StringsMap | undefined {
    return this.map.get(resourceType)?.label
  }
 
  isRegisteredResourceType(resourceType: ResourceType): boolean {
    return this.map.has(resourceType)
  }
}
 
export default new RbacFactory()