All files / modules/20-rbac/pages/UserDetails UserDetails.tsx

84.62% Statements 33/39
58% Branches 29/50
33.33% Functions 1/3
86.84% Lines 33/38

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144              10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x 10x 10x   10x 4x   3x 3x 3x   3x                 3x       3x   3x   3x     3x     3x     3x                                                                                                                                                                 10x  
/*
 * 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 { Text, Layout, Card, Avatar, Icon, ButtonVariation, PageError } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { useStrings } from 'framework/strings'
import { useGetAggregatedUser } from 'services/cd-ng'
import { Page } from '@common/exports'
import routes from '@common/RouteDefinitions'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { PageSpinner } from '@common/components'
import RoleBindingsList from '@rbac/components/RoleBindingsList/RoleBindingsList'
import type { PipelineType, ProjectPathProps, UserPathProps } from '@common/interfaces/RouteInterfaces'
import { useRoleAssignmentModal } from '@rbac/modals/RoleAssignmentModal/useRoleAssignmentModal'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PrincipalType } from '@rbac/utils/utils'
import ManagePrincipalButton from '@rbac/components/ManagePrincipalButton/ManagePrincipalButton'
import { useLicenseStore, isCDCommunity } from 'framework/LicenseStore/LicenseStoreContext'
import UserGroupTable from './views/UserGroupTable'
import css from './UserDetails.module.scss'
 
const UserDetails: React.FC = () => {
  const { getString } = useStrings()
  const { accountId, orgIdentifier, projectIdentifier, module, userIdentifier } =
    useParams<PipelineType<ProjectPathProps & UserPathProps>>()
  const { licenseInformation } = useLicenseStore()
  const isCommunity = isCDCommunity(licenseInformation)
 
  const { data, loading, error, refetch } = useGetAggregatedUser({
    userId: userIdentifier,
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const { openRoleAssignmentModal } = useRoleAssignmentModal({
    onSuccess: refetch
  })
 
  const user = data?.data?.user
 
  useDocumentTitle([user?.name || '', getString('users')])
 
  Iif (loading) {
    return <PageSpinner />
  }
  Iif (error) {
    return <PageError message={error.message} onClick={() => refetch()} />
  }
  Iif (!data?.data || !user) {
    return <></>
  }
  return (
    <>
      <Page.Header
        size="xlarge"
        className={css.header}
        breadcrumbs={
          <NGBreadcrumbs
            links={[
              {
                url: routes.toUsers({ accountId, orgIdentifier, projectIdentifier, module }),
                label: `${getString('accessControl')}: ${getString('users')}`
              }
            ]}
          />
        }
        title={
          <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }} spacing="medium">
            {user.locked ? (
              <Icon
                name="lock"
                border
                className={css.lockIcon}
                width={72}
                height={72}
                size={32}
                color={Color.WHITE}
                background={Color.GREY_300}
                flex={{ align: 'center-center' }}
                margin={{ left: 'xsmall', right: 'xsmall' }}
              />
            ) : (
              <Avatar name={user.name || user.email} email={user.email} size="large" hoverCard={false} />
            )}
 
            <Layout.Vertical padding={{ left: 'medium' }} spacing="xsmall">
              <Layout.Horizontal flex={{ alignItems: 'baseline' }} spacing="xsmall">
                <Text color={Color.BLACK} font="medium">
                  {user.name}
                </Text>
                {user.locked ? <Text color={Color.GREY_400}>{getString('rbac.usersPage.lockedOutLabel')}</Text> : null}
              </Layout.Horizontal>
              <Text>{user.email}</Text>
            </Layout.Vertical>
          </Layout.Horizontal>
        }
      />
      <Page.Body className={css.body}>
        <Layout.Vertical width="100%" padding="large">
          {!isCommunity && (
            <Layout.Vertical spacing="medium" padding={{ bottom: 'large' }}>
              <Text color={Color.BLACK} font={{ size: 'medium', weight: 'semi-bold' }}>
                {getString('rbac.roleBinding')}
              </Text>
              <Card className={css.card}>
                <RoleBindingsList data={data?.data?.roleAssignmentMetadata} showNoData={true} />
              </Card>
              <Layout.Horizontal
                flex={{ alignItems: 'center', justifyContent: 'flex-start' }}
                padding={{ top: 'medium' }}
              >
                <ManagePrincipalButton
                  data-testid={'addRole-UserGroup'}
                  text={getString('common.plusNumber', { number: getString('common.role') })}
                  variation={ButtonVariation.LINK}
                  onClick={event => {
                    event.stopPropagation()
                    openRoleAssignmentModal(PrincipalType.USER, user, data?.data?.roleAssignmentMetadata)
                  }}
                  resourceIdentifier={user.uuid}
                  resourceType={ResourceType.USER}
                />
              </Layout.Horizontal>
            </Layout.Vertical>
          )}
          <UserGroupTable user={data.data} />
        </Layout.Vertical>
      </Page.Body>
    </>
  )
}
 
export default UserDetails