All files / modules/33-auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/LockoutPolicy LockoutPolicy.tsx

97.83% Statements 45/46
95% Branches 19/20
87.5% Functions 7/8
97.83% Lines 45/46

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186              4x 4x 4x 4x                   4x 4x 4x   4x   4x 4x 4x 4x                 4x 16x 16x 16x 16x 16x   16x 1x     16x   16x             16x 7x     16x             1x 1x 1x               1x 1x 1x                 16x 3x 3x 3x   3x 2x 2x     1x 1x       16x                                                   16x                                                         1x           64x                                   4x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React, { Dispatch, SetStateAction } from 'react'
import cx from 'classnames'
import { useParams } from 'react-router-dom'
import {
  Layout,
  Text,
  Container,
  Switch,
  Collapse,
  Button,
  ButtonVariation,
  useConfirmationDialog
} from '@wings-software/uicore'
import { Color, Intent } from '@harness/design-system'
import { useLockoutPolicyModal } from '@auth-settings/modals/LockoutPolicy/useLockoutPolicy'
import { useToaster } from '@common/components'
import type { LoginSettings } from 'services/cd-ng'
import { usePutLoginSettings } from 'services/cd-ng'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import cssConfiguration from '@auth-settings/pages/Configuration/Configuration.module.scss'
import cssHarnessAccount from '@auth-settings/pages/Configuration/AccountAndOAuth/HarnessAccount/HarnessAccount.module.scss'
 
interface Props {
  loginSettings: LoginSettings
  refetchAuthSettings: () => void
  canEdit: boolean
  setUpdating: Dispatch<SetStateAction<boolean>>
}
 
const LockoutPolicy: React.FC<Props> = ({ loginSettings, refetchAuthSettings, canEdit, setUpdating }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { showSuccess, showError } = useToaster()
  const userLockoutSettings = loginSettings.userLockoutPolicy
 
  const onSuccess = (): void => {
    refetchAuthSettings()
  }
 
  const { openLockoutPolicyModal } = useLockoutPolicyModal({ onSuccess, loginSettings })
 
  const { mutate: updateLoginSettings, loading: updatingLoginSettings } = usePutLoginSettings({
    loginSettingsId: loginSettings.uuid,
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  React.useEffect(() => {
    setUpdating(updatingLoginSettings)
  }, [updatingLoginSettings, setUpdating])
 
  const { openDialog: confirmLockoutSettings } = useConfirmationDialog({
    titleText: getString('authSettings.disableLockoutPolicy'),
    contentText: getString('authSettings.confirmDisableLockoutPolicy'),
    confirmButtonText: getString('confirm'),
    cancelButtonText: getString('cancel'),
    intent: Intent.WARNING,
    onCloseDialog: async isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed) {
        try {
          const response = await updateLoginSettings({
            ...loginSettings,
            userLockoutPolicy: {
              ...userLockoutSettings,
              enableLockoutPolicy: false
            }
          })
 
          /* istanbul ignore else */ if (response) {
            refetchAuthSettings()
            showSuccess(getString('authSettings.lockoutPolicyDisabled'), 5000)
          }
        } catch (e) {
          /* istanbul ignore next */ showError(getRBACErrorMessage(e), 5000)
        }
      }
    }
  })
 
  const onChangeLockoutSettings = (e: React.FormEvent<HTMLInputElement>): void => {
    e.stopPropagation()
    const enable = e.currentTarget.checked
    const currentState = userLockoutSettings.enableLockoutPolicy
 
    if (!currentState && enable) {
      openLockoutPolicyModal(false)
      return
    }
 
    /* istanbul ignore else */ if (currentState && !enable) {
      confirmLockoutSettings()
    }
  }
 
  const list = [
    {
      label: getString('authSettings.failedLoginsBeforeLockedAccount'),
      value: userLockoutSettings.numberOfFailedAttemptsBeforeLockout
    },
    {
      label: getString('authSettings.lockoutDuration'),
      value: (
        <>
          <Text color={Color.GREY_800} font={{ weight: 'bold' }} padding={{ left: 'xsmall', right: 'xsmall' }}>
            {userLockoutSettings.lockOutPeriod}
          </Text>
          <Text color={Color.GREY_800}>{getString('hours')}</Text>
        </>
      )
    },
    {
      label: getString('authSettings.notifyUsersWhenTheyLocked'),
      value: userLockoutSettings.notifyUser ? getString('yes') : /* istanbul ignore next */ getString('no')
    },
    {
      label: getString('authSettings.notifyUsersWhenUserLocked'),
      value: userLockoutSettings.userGroupsToNotify?.map(userGroup => userGroup.name).join(', ') || getString('none')
    }
  ]
 
  return (
    <Collapse
      isOpen={userLockoutSettings.enableLockoutPolicy}
      collapseHeaderClassName={cx(cssConfiguration.collapseHeaderClassName, cssHarnessAccount.collapseIcon)}
      collapseClassName={cssConfiguration.collapseClassName}
      collapsedIcon="main-chevron-down"
      expandedIcon="main-chevron-up"
      heading={
        <Switch
          label={getString('authSettings.enforceLockoutPolicy')}
          checked={userLockoutSettings.enableLockoutPolicy}
          onChange={onChangeLockoutSettings}
          disabled={!canEdit || updatingLoginSettings}
          font={{ weight: 'semi-bold', size: 'normal' }}
          color={Color.GREY_800}
          data-testid="toggle-lockout-policy"
        />
      }
    >
      <Layout.Vertical
        spacing="small"
        padding={{ left: 'xxxlarge', top: 'medium', bottom: 'medium', right: 'medium' }}
        margin={{ bottom: 'large' }}
        className={cssHarnessAccount.passwordChecksDiv}
      >
        <div className={cssHarnessAccount.editIcon}>
          <Button
            variation={ButtonVariation.ICON}
            icon="Edit"
            onClick={() => openLockoutPolicyModal(true)}
            data-testid="update-lockout-policy"
            disabled={!canEdit}
          />
        </div>
        {list.map(({ label, value }) => (
          <Container flex={{ justifyContent: 'flex-start' }} key={label}>
            <Text color={Color.GREY_800} icon="dot">
              {label}:
            </Text>
            {React.isValidElement(value) ? (
              value
            ) : (
              <Text color={Color.GREY_800} font={{ weight: 'bold' }} padding={{ left: 'xsmall' }}>
                {value}
              </Text>
            )}
          </Container>
        ))}
      </Layout.Vertical>
    </Collapse>
  )
}
 
export default LockoutPolicy