All files / modules/20-rbac/utils/useRBACError useRBACError.tsx

100% Statements 37/37
83.33% Branches 55/66
100% Functions 4/4
100% Lines 37/37

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119              420x 420x 420x   420x 420x   420x 420x 420x 420x           420x                 420x 3265x 3262x 3262x   3262x   1x 1x           3262x 3x 3x   1x     1x     1x         3262x 122x 122x           3x   3x 3x 3x             3x   3x 1x   3x                                                 119x   3262x         420x  
/*
 * 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 { Color, getErrorInfoFromErrorObject, Layout, Text } from '@harness/uicore'
import React from 'react'
import { useParams } from 'react-router-dom'
import type { GetDataError } from 'restful-react'
import { defaultTo } from 'lodash-es'
import { getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { Scope } from '@common/interfaces/SecretsInterface'
import RbacFactory from '@rbac/factories/RbacFactory'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useStrings } from 'framework/strings'
import type { ResourceType } from '@rbac/interfaces/ResourceType'
import type { StringsMap } from 'framework/strings/StringsContext'
import type { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { AccessControlCheckError, Error, Failure, ResourceScope } from 'services/cd-ng'
import type { ErrorHandlerProps } from '../utils'
import css from '@rbac/components/RBACTooltip/RBACTooltip.module.scss'
 
// Giving this type the name of RBACError for the time being.
// Once RBAC Error is renamed to a more generic solution and naming, this name should be renamed and made generic as well
export type RBACError = ErrorHandlerProps | GetDataError<Failure | AccessControlCheckError | Error>
interface RbacErrorReturn {
  getRBACErrorMessage: (error: RBACError) => React.ReactElement | string
}
 
const useRBACError = (): RbacErrorReturn => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { selectedProject } = useAppStore()
 
  const getProjectScopeSuffix = (resourceScope: ResourceScope): string => {
    /* istanbul ignore else */
    if (selectedProject && [resourceScope.projectIdentifier, projectIdentifier].includes(selectedProject.identifier)) {
      return selectedProject.name
    } else {
      return resourceScope.projectIdentifier || projectIdentifier
    }
  }
 
  const getScopeSuffix = (currentScope: Scope, resourceScope: ResourceScope): string => {
    const currentScopeLabel = getString(`rbac.${currentScope}` as keyof StringsMap)
    switch (currentScope) {
      case Scope.PROJECT: {
        return `${currentScopeLabel} "${getProjectScopeSuffix(resourceScope)}"`
      }
      case Scope.ORG: {
        return `${currentScopeLabel} "${resourceScope.orgIdentifier || orgIdentifier}"`
      }
      default: {
        return getString('rbac.accountScope')
      }
    }
  }
 
  const getRBACErrorMessage = (error: RBACError): React.ReactElement | string => {
    const err = error?.data
    if (
      err &&
      (err as AccessControlCheckError).code === 'NG_ACCESS_DENIED' &&
      (err as AccessControlCheckError)?.failedPermissionChecks
    ) {
      const { permission, resourceType, resourceScope } =
        (err as AccessControlCheckError)?.failedPermissionChecks?.[0] || {}
      /* istanbul ignore else */
      if (permission && resourceType && resourceScope) {
        const resourceTypeHandler = RbacFactory.getResourceTypeHandler(resourceType as ResourceType)
        const currentScope = getScopeFromDTO(
          defaultTo(resourceScope, {
            projectIdentifier,
            orgIdentifier,
            accountIdentifier: accountId
          })
        )
        let permissionLabel = resourceTypeHandler?.permissionLabels?.[permission as PermissionIdentifier] || permission
        /* istanbul ignore else */
        if (typeof permissionLabel !== 'string') {
          permissionLabel = getString(permissionLabel.props.stringID)
        }
        return (
          <Layout.Vertical padding="small" spacing="small" className={css.main}>
            <Text font={{ size: 'small', weight: 'semi-bold' }} color={Color.GREY_800}>
              {`${getString('rbac.youAreNotAuthorizedTo')} `}
              <span className={css.textToLowercase}>{permissionLabel}</span>
              <span className={css.textToLowercase}>
                {` ${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}>
              {'"'}
              {permissionLabel}
              <span className={css.textToLowercase}>
                {` ${resourceTypeHandler?.label && getString(resourceTypeHandler?.label)}`}
              </span>
              {'"'}
              <span>{` ${getString('rbac.in')} ${getScopeSuffix(currentScope, resourceScope)}`}</span>
            </Text>
          </Layout.Vertical>
        )
      }
    }
    return getErrorInfoFromErrorObject(error)
  }
  return {
    getRBACErrorMessage
  }
}
 
export default useRBACError