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 | 4x 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 1x 1x 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, Text, Formik, FormikForm, FormInput, Checkbox, Button, ModalErrorHandler, ModalErrorHandlerBinding, ButtonVariation } from '@wings-software/uicore' import { Color } from '@harness/design-system' import { useToaster } from '@common/components' import type { LoginSettings, PasswordStrengthPolicy } from 'services/cd-ng' import { usePutLoginSettings } from 'services/cd-ng' import type { AccountPathProps } from '@common/interfaces/RouteInterfaces' import { useStrings } from 'framework/strings' import { MIN_NUMBER_OF_CHARACTERS, MAX_NUMBER_OF_CHARACTERS } from '@common/constants/Utils' import useRBACError from '@rbac/utils/useRBACError/useRBACError' interface Props { onSubmit?: () => void onCancel: () => void loginSettings: LoginSettings editing: boolean } interface FormValues { minNumberOfCharacters: number atLeastOneUppercase: boolean atLeastOneLowercase: boolean atLeastOneDigit: boolean atLeastOneSpecialChar: boolean } const PasswordStrengthForm: React.FC<Props> = ({ onSubmit, onCancel, loginSettings, editing }) => { const { getRBACErrorMessage } = useRBACError() const { getString } = useStrings() const { accountId } = useParams<AccountPathProps>() const { showSuccess } = useToaster() const passwordStrengthSettings = loginSettings.passwordStrengthPolicy const [modalErrorHandler, setModalErrorHandler] = React.useState<ModalErrorHandlerBinding>() const { mutate: updateLoginSettings, loading: updatingLoginSettings } = usePutLoginSettings({ loginSettingsId: loginSettings.uuid, queryParams: { accountIdentifier: accountId } }) const handleSubmit = async (values: FormValues): Promise<void> => { const passwordStrengthPolicy: PasswordStrengthPolicy = { enabled: editing ? passwordStrengthSettings.enabled : /* istanbul ignore next */ true, minNumberOfCharacters: values.minNumberOfCharacters, minNumberOfUppercaseCharacters: Number(values.atLeastOneUppercase), minNumberOfLowercaseCharacters: Number(values.atLeastOneLowercase), minNumberOfDigits: Number(values.atLeastOneDigit), minNumberOfSpecialCharacters: Number(values.atLeastOneSpecialChar) } try { const response = await updateLoginSettings({ ...loginSettings, passwordStrengthPolicy }) /* istanbul ignore else */ if (response) { showSuccess( getString(editing ? 'authSettings.passwordStrengthUpdated' : 'authSettings.passwordStrengthEnabled'), 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.passwordStrength')} </Text> <Formik formName="passwordStrengthForm" initialValues={{ minNumberOfCharacters: passwordStrengthSettings?.minNumberOfCharacters || /* istanbul ignore next */ MIN_NUMBER_OF_CHARACTERS, atLeastOneUppercase: !!passwordStrengthSettings?.minNumberOfUppercaseCharacters, atLeastOneLowercase: !!passwordStrengthSettings?.minNumberOfLowercaseCharacters, atLeastOneDigit: !!passwordStrengthSettings?.minNumberOfDigits, atLeastOneSpecialChar: !!passwordStrengthSettings?.minNumberOfSpecialCharacters }} validationSchema={yup.object().shape({ minNumberOfCharacters: yup .number() .typeError(getString('common.validation.valueMustBeANumber')) .min( MIN_NUMBER_OF_CHARACTERS, getString('common.validation.valueMustBeGreaterThanOrEqualToN', { n: MIN_NUMBER_OF_CHARACTERS }) ) .max( MAX_NUMBER_OF_CHARACTERS, getString('common.validation.valueMustBeLessThanOrEqualToN', { n: MAX_NUMBER_OF_CHARACTERS }) ) .required(getString('validation.minLengthRequired')) })} onSubmit={values => { handleSubmit(values) }} > {({ values, setFieldValue }) => ( <FormikForm> <FormInput.Text name="minNumberOfCharacters" label={getString('authSettings.minLength')} inputGroup={{ type: 'number' }} /> <Layout.Vertical spacing="medium" padding={{ top: 'xxlarge' }}> <Checkbox label={getString('authSettings.haveOneUppercase')} checked={values.atLeastOneUppercase} onChange={e => setFieldValue('atLeastOneUppercase', e.currentTarget.checked)} /> <Checkbox label={getString('authSettings.haveOneLowercase')} checked={values.atLeastOneLowercase} onChange={e => setFieldValue('atLeastOneLowercase', e.currentTarget.checked)} /> <Checkbox label={getString('authSettings.haveOneDigit')} checked={values.atLeastOneDigit} onChange={e => setFieldValue('atLeastOneDigit', e.currentTarget.checked)} /> <Checkbox label={getString('authSettings.haveOneSpecialChar')} checked={values.atLeastOneSpecialChar} onChange={e => setFieldValue('atLeastOneSpecialChar', e.currentTarget.checked)} /> </Layout.Vertical> <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 PasswordStrengthForm |