All files / modules/33-auth-settings/modals/LockoutPolicy/views LockoutPolicyForm.tsx

100% Statements 29/29
72.73% Branches 8/11
100% Functions 5/5
100% Lines 29/29

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              4x 4x 4x 4x                         4x 4x   4x   4x 4x                 4x 6x 6x 6x 6x 6x 6x   6x             6x 1x           1x 1x         1x 1x       1x             6x                                                       1x       11x                                         1x                                                       4x  
/*
 * 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 * as yup from 'yup'
import { useParams } from 'react-router-dom'
import {
  Layout,
  Formik,
  FormikForm,
  FormInput,
  Checkbox,
  Button,
  // Select,
  Text,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  ButtonVariation
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useToaster } from '@common/components'
import type { LoginSettings, UserLockoutPolicy } 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'
 
interface Props {
  onSubmit?: () => void
  onCancel: () => void
  loginSettings: LoginSettings
  editing: boolean
}
 
const LockoutPolicyForm: React.FC<Props> = ({ onSubmit, onCancel, loginSettings, editing }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { showSuccess } = useToaster()
  const [modalErrorHandler, setModalErrorHandler] = React.useState<ModalErrorHandlerBinding>()
  const userLockoutSettings = loginSettings.userLockoutPolicy
 
  const { mutate: updateLoginSettings, loading: updatingLoginSettings } = usePutLoginSettings({
    loginSettingsId: loginSettings.uuid,
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const handleSubmit = async (values: UserLockoutPolicy): Promise<void> => {
    const userLockoutPolicy: UserLockoutPolicy = {
      ...values,
      enableLockoutPolicy: editing ? userLockoutSettings.enableLockoutPolicy : true,
      userGroupsToNotify: userLockoutSettings.userGroupsToNotify
    }
 
    try {
      const response = await updateLoginSettings({
        ...loginSettings,
        userLockoutPolicy
      })
 
      /* istanbul ignore else */ if (response) {
        showSuccess(
          getString(editing ? 'authSettings.lockoutPolicyUpdated' : 'authSettings.lockoutPolicyEnabled'),
          5000
        )
        onSubmit?.()
      }
    } catch (e) {
      /* istanbul ignore next */ modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
 
  return (
    <Layout.Vertical padding={{ left: 'huge', right: 'huge' }}>
      <ModalErrorHandler bind={setModalErrorHandler} />
      <Text color={Color.GREY_900} font={{ size: 'medium', weight: 'semi-bold' }} margin={{ bottom: 'xxlarge' }}>
        {getString('authSettings.lockoutPolicy')}
      </Text>
      <Formik
        formName="lockoutPolicyForm"
        initialValues={{
          numberOfFailedAttemptsBeforeLockout:
            userLockoutSettings.numberOfFailedAttemptsBeforeLockout || /* istanbul ignore next */ 1,
          lockOutPeriod: userLockoutSettings.lockOutPeriod || /* istanbul ignore next */ 1,
          notifyUser: !!userLockoutSettings.notifyUser
          // userGroupsToNotify: []
        }}
        validationSchema={yup.object().shape({
          numberOfFailedAttemptsBeforeLockout: yup
            .number()
            .typeError(getString('common.validation.valueMustBeANumber'))
            .min(1, getString('common.validation.valueMustBeGreaterThanOrEqualToN', { n: 1 }))
            .required(getString('validation.thisIsARequiredField')),
          lockOutPeriod: yup
            .number()
            .typeError(getString('common.validation.valueMustBeANumber'))
            .min(1, getString('common.validation.valueMustBeGreaterThanOrEqualToN', { n: 1 }))
            .required(getString('validation.thisIsARequiredField'))
        })}
        onSubmit={values => {
          handleSubmit(values)
        }}
      >
        {({ values, setFieldValue }) => (
          <FormikForm>
            <Layout.Vertical spacing="xlarge">
              <FormInput.Text
                name="numberOfFailedAttemptsBeforeLockout"
                label={getString('authSettings.failedLoginsBeforeLocked')}
                inputGroup={{
                  type: 'number'
                }}
              />
              <FormInput.Text
                name="lockOutPeriod"
                label={getString('authSettings.lockoutDuration')}
                inputGroup={{
                  type: 'number'
                }}
              />
            </Layout.Vertical>
            <Layout.Vertical padding={{ top: 'large', bottom: 'xxlarge' }}>
              <Checkbox
                label={getString('authSettings.notifyUsersWhenTheyLocked')}
                checked={values.notifyUser}
                onChange={e => setFieldValue('notifyUser', e.currentTarget.checked)}
              />
            </Layout.Vertical>
            {/* <Text color={Color.GREY_500} padding={{ top: 'xsmall', bottom: 'xsmall' }}>
              {getString('authSettings.notifyUsersWhenUserLocked')}
            </Text>
            <Select
              name="userGroupsToNotify"
              items={[]}
              inputProps={{ placeholder: getString('authSettings.selectUserGroup') }}
            /> */}
            <Layout.Horizontal margin={{ top: 'xxxlarge', bottom: 'xlarge' }}>
              <Button
                text={getString('save')}
                type="submit"
                variation={ButtonVariation.PRIMARY}
                margin={{ right: 'small' }}
                disabled={updatingLoginSettings}
              />
              <Button text={getString('cancel')} onClick={onCancel} variation={ButtonVariation.TERTIARY} />
            </Layout.Horizontal>
          </FormikForm>
        )}
      </Formik>
    </Layout.Vertical>
  )
}
 
export default LockoutPolicyForm