All files / modules/20-rbac/components/RBACTooltip RBACTooltip.tsx

96.43% Statements 27/28
83.33% Branches 70/84
100% Functions 3/3
96.43% Lines 27/28

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              451x 451x 451x 451x 451x       451x 451x   451x 451x   451x                 451x 6x 6x 6x 6x 6x               6x 4x       4x           6x 6x 6x   4x     1x     1x         6x                                         451x  
/*
 * 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 React from 'react'
import { useParams } from 'react-router-dom'
import { Layout, Text } from '@harness/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { ResourceType } from '@rbac/interfaces/ResourceType'
import type { ResourceScope } from 'services/rbac/index'
import RbacFactory from '@rbac/factories/RbacFactory'
import { getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { Scope } from '@common/interfaces/SecretsInterface'
import type { StringsMap } from 'stringTypes'
import css from './RBACTooltip.module.scss'
 
interface Props {
  permission: PermissionIdentifier
  resourceType: ResourceType
  resourceScope?: ResourceScope
  className?: string
}
 
const RBACTooltip: React.FC<Props> = ({ permission, resourceType, resourceScope, className }) => {
  const { getString } = useStrings()
  const { selectedProject } = useAppStore()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const resourceTypeHandler = RbacFactory.getResourceTypeHandler(resourceType)
  const currentScope = getScopeFromDTO(
    resourceScope || {
      projectIdentifier,
      orgIdentifier,
      accountIdentifier: accountId
    }
  )
 
  const getProjectScopeSuffix = (): string => {
    Eif (
      selectedProject?.identifier === resourceScope?.projectIdentifier ||
      selectedProject?.identifier === projectIdentifier
    ) {
      return selectedProject?.name || resourceScope?.projectIdentifier || projectIdentifier
    } else {
      return resourceScope?.projectIdentifier || projectIdentifier
    }
  }
 
  const getScopeSuffix = (): string => {
    const currentScopeLabel = getString(`rbac.${currentScope}` as keyof StringsMap)
    switch (currentScope) {
      case Scope.PROJECT: {
        return `${currentScopeLabel} "${getProjectScopeSuffix()}"`
      }
      case Scope.ORG: {
        return `${currentScopeLabel} "${resourceScope?.orgIdentifier || orgIdentifier}"`
      }
      case Scope.ACCOUNT: {
        return getString('rbac.accountScope')
      }
    }
  }
 
  return (
    <Layout.Vertical padding="small" spacing="small" className={className}>
      <Text font={{ size: 'small', weight: 'semi-bold' }} color={Color.GREY_800}>
        {`${getString('rbac.youAreNotAuthorizedTo')} `}
        <span className={css.textToLowercase}>{resourceTypeHandler?.permissionLabels?.[permission] || permission}</span>
        <span>{` ${resourceTypeHandler?.label && getString(resourceTypeHandler?.label)}.`}</span>
      </Text>
      <Text font={{ size: 'small' }} color={Color.GREY_800}>
        {getString('rbac.youAreMissingTheFollowingPermission')}
      </Text>
      <Text font={{ size: 'small' }} color={Color.GREY_800}>
        {'"'}
        {resourceTypeHandler?.permissionLabels?.[permission] || permission}
        <span>{` ${resourceTypeHandler?.label && getString(resourceTypeHandler?.label)}`}</span>
        {'"'}
        <span>{` ${getString('rbac.in')} ${getScopeSuffix()}`}</span>
      </Text>
    </Layout.Vertical>
  )
}
 
export default RBACTooltip