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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 1x 3x 3x 3x 3x 3x 3x 3x 4x 4x 1x 3x 3x 1x 1x 1x 1x 1x 1x 3x 4x 4x 1x 8x 2x 1x 1x 1x | /*
* Copyright 2021 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 {
Button,
Layout,
Formik,
FormikForm,
FormInput,
Container,
Text,
ModalErrorHandler,
ModalErrorHandlerBinding,
ButtonVariation,
shouldShowError
} from '@wings-software/uicore'
import { Divider } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import PasswordChecklist from '@common/components/PasswordChecklist/PasswordChecklist'
import { PASSWORD_CHECKS_RGX, MIN_NUMBER_OF_CHARACTERS, MAX_NUMBER_OF_CHARACTERS } from '@common/constants/Utils'
import { useChangeUserPassword } from 'services/cd-ng'
import type { PasswordStrengthPolicy } from 'services/cd-ng'
import { useToaster } from '@common/components'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from './ChangePasswordForm.module.scss'
interface ChangePasswordFormProps {
hideModal: () => void
passwordStrengthPolicy: PasswordStrengthPolicy
}
interface InputIcon {
isVisible: boolean
onClick: () => void
}
interface FormValues {
currentPassword: string
newPassword: string
confirmPassword: string
}
const enum PasswordTypes {
CURRENT_PASSWORD,
NEW_PASSWORD,
CONFIRM_PASSWORD
}
export enum ChangePasswordResponse {
PASSWORD_CHANGED = 'PASSWORD_CHANGED',
INCORRECT_CURRENT_PASSWORD = 'INCORRECT_CURRENT_PASSWORD',
PASSWORD_STRENGTH_VIOLATED = 'PASSWORD_STRENGTH_VIOLATED'
}
const InputIcon = ({ isVisible, onClick }: InputIcon): React.ReactElement => (
<Button
variation={ButtonVariation.ICON}
icon={isVisible ? 'eye-open' : 'eye-off'}
iconProps={{ size: 20 }}
onClick={onClick}
margin={{ right: 'xsmall' }}
/>
)
const ChangePasswordForm: React.FC<ChangePasswordFormProps> = ({ hideModal, passwordStrengthPolicy }) => {
const { getString } = useStrings()
const { getRBACErrorMessage } = useRBACError()
const { showSuccess } = useToaster()
const [visiblePasswords, setVisiblePasswords] = React.useState<Array<PasswordTypes>>([])
const [modalErrorHandler, setModalErrorHandler] = React.useState<ModalErrorHandlerBinding>()
const { mutate: changeUserPassword, loading: changingUserPassword } = useChangeUserPassword({})
const handlePasswordVisibility = (passwordType: PasswordTypes): void => {
setVisiblePasswords(prevState => {
if (prevState.includes(passwordType)) {
return prevState.filter((type: PasswordTypes) => type !== passwordType)
} else {
return [...prevState, passwordType]
}
})
}
const handleSubmit = async (values: FormValues): Promise<void> => {
try {
const response = await changeUserPassword({
currentPassword: values.currentPassword,
newPassword: values.newPassword
})
/* istanbul ignore else */ if (response) {
/* istanbul ignore else */ if (response.data === ChangePasswordResponse.PASSWORD_CHANGED) {
showSuccess(getString('userProfile.passwordChangedSuccessfully'), 5000)
hideModal()
} /* istanbul ignore next */ else if (response.data === ChangePasswordResponse.INCORRECT_CURRENT_PASSWORD) {
modalErrorHandler?.showDanger(getString('userProfile.yourCurrentPasswordIncorrect'))
} /* istanbul ignore next */ else if (response.data === ChangePasswordResponse.PASSWORD_STRENGTH_VIOLATED) {
modalErrorHandler?.showDanger(getString('userProfile.newPasswordShouldMeetTheRequirements'))
}
}
} catch (e) {
/* istanbul ignore next */ if (shouldShowError(e)) {
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('userProfile.changePassword')}
</Text>
<Formik
initialValues={{
currentPassword: '',
newPassword: '',
confirmPassword: ''
}}
formName="changePasswordForm"
validationSchema={yup.object().shape({
currentPassword: yup
.string()
.required(`${getString('userProfile.currentPassword')} ${getString('userProfile.requiredField')}`),
newPassword: yup
.string()
.matches(
PASSWORD_CHECKS_RGX(passwordStrengthPolicy),
passwordStrengthPolicy.enabled
? getString('userProfile.passwordReqs')
: getString('userProfile.passwordMustBeBetweenMinAndMax', {
min: MIN_NUMBER_OF_CHARACTERS,
max: MAX_NUMBER_OF_CHARACTERS
})
)
.test(
'passwords-should-not-be-same',
getString('userProfile.newPasswordShouldNotBeCurrentPassword'),
function (value) {
return value ? this.parent.currentPassword !== value : true
}
)
.required(`${getString('userProfile.newPassword')} ${getString('userProfile.requiredField')}`),
confirmPassword: yup
.string()
.test('passwords-match', getString('userProfile.passwordMatch'), function (value) {
return this.parent.newPassword === value
})
.required(`${getString('userProfile.confirmPassword')} ${getString('userProfile.requiredField')}`)
})}
onSubmit={values => {
handleSubmit(values)
}}
>
{({ values }) => (
<FormikForm>
<Container width={300}>
<FormInput.Text
name="currentPassword"
label={getString('userProfile.currentPassword')}
inputGroup={{
type: visiblePasswords.includes(PasswordTypes.CURRENT_PASSWORD)
? /* istanbul ignore next */ 'text'
: 'password',
rightElement: (
<InputIcon
isVisible={visiblePasswords.includes(PasswordTypes.CURRENT_PASSWORD)}
onClick={() => {
handlePasswordVisibility(PasswordTypes.CURRENT_PASSWORD)
}}
/>
)
}}
/>
</Container>
<Divider className={css.divider} />
<Layout.Horizontal>
<Container width={300}>
<FormInput.Text
name="newPassword"
label={getString('userProfile.newPassword')}
inputGroup={{
type: visiblePasswords.includes(PasswordTypes.NEW_PASSWORD) ? 'text' : 'password',
rightElement: (
<InputIcon
isVisible={visiblePasswords.includes(PasswordTypes.NEW_PASSWORD)}
onClick={() => {
handlePasswordVisibility(PasswordTypes.NEW_PASSWORD)
}}
/>
)
}}
/>
</Container>
{passwordStrengthPolicy.enabled && (
<Container padding={{ left: 'xxlarge', top: 'large' }}>
<PasswordChecklist value={values.newPassword} passwordStrengthPolicy={passwordStrengthPolicy} />
</Container>
)}
</Layout.Horizontal>
<Container width={300}>
<FormInput.Text
name="confirmPassword"
label={getString('userProfile.confirmPassword')}
inputGroup={{
type: visiblePasswords.includes(PasswordTypes.CONFIRM_PASSWORD) ? 'text' : 'password',
rightElement: (
<InputIcon
isVisible={visiblePasswords.includes(PasswordTypes.CONFIRM_PASSWORD)}
onClick={() => {
handlePasswordVisibility(PasswordTypes.CONFIRM_PASSWORD)
}}
/>
)
}}
/>
</Container>
<Layout.Horizontal margin={{ top: 'xxxlarge', bottom: 'xlarge' }}>
<Button
text={getString('userProfile.changePassword')}
type="submit"
variation={ButtonVariation.PRIMARY}
margin={{ right: 'small' }}
disabled={changingUserPassword}
/>
<Button text={getString('cancel')} onClick={hideModal} variation={ButtonVariation.TERTIARY} />
</Layout.Horizontal>
</FormikForm>
)}
</Formik>
</Layout.Vertical>
)
}
export default ChangePasswordForm
|