Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use password regex [DHIS2-15704] #1519

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-12-18T07:54:11.061Z\n"
"PO-Revision-Date: 2024-12-18T07:54:11.062Z\n"
"POT-Creation-Date: 2025-01-09T12:18:17.999Z\n"
"PO-Revision-Date: 2025-01-09T12:18:17.999Z\n"

msgid "Yes"
msgstr "Yes"
Expand Down Expand Up @@ -603,6 +603,9 @@ msgstr "You do not have permission to assign certain user roles"
msgid "User groups this user is a member of"
msgstr "User groups this user is a member of"

msgid "Invalid password"
msgstr "Invalid password"

msgid "Security"
msgstr "Security"

Expand All @@ -619,11 +622,13 @@ msgid "Password"
msgstr "Password"

msgid ""
"Password should be at least 8 characters long, with at least one lowercase "
"character, one uppercase character and one special character."
"Password should be between {{minPasswordLength}} and {{maxPasswordLength}} "
"characters long, with at least one lowercase character, one uppercase "
"character, one number, and one special character."
msgstr ""
"Password should be at least 8 characters long, with at least one lowercase "
"character, one uppercase character and one special character."
"Password should be between {{minPasswordLength}} and {{maxPasswordLength}} "
"characters long, with at least one lowercase character, one uppercase "
"character, one number, and one special character."

msgid "Repeat new password"
msgstr "Repeat new password"
Expand Down
20 changes: 16 additions & 4 deletions src/components/UserForm/SecuritySection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import i18n from '@dhis2/d2-i18n'
import { composeValidators, hasValue, dhis2Password } from '@dhis2/ui'
import { composeValidators, hasValue, createPattern } from '@dhis2/ui'
import moment from 'moment'
import PropTypes from 'prop-types'
import React from 'react'
import { useSystemInformation } from '../../providers/index.js'
import {
FormSection,
CheckboxField,
Expand All @@ -15,6 +16,16 @@ import { createRepeatPasswordValidator } from './validators.js'

const SecuritySection = React.memo(
({ user, inviteUser, externalAuth, changePassword, password }) => {
const {
minPasswordLength,
maxPasswordLength,
passwordValidationPattern,
} = useSystemInformation()
const passwordRegex = new RegExp(passwordValidationPattern)
const passwordRegExValidator = createPattern(
passwordRegex,
i18n.t('Invalid password')
)
if (inviteUser === 'INVITE_USER') {
return null
}
Expand Down Expand Up @@ -48,16 +59,17 @@ const SecuritySection = React.memo(
: i18n.t('Password')
}
helpText={i18n.t(
'Password should be at least 8 characters long, with at least one lowercase character, one uppercase character and one special character.'
'Password should be between {{minPasswordLength}} and {{maxPasswordLength}} characters long, with at least one lowercase character, one uppercase character, one number, and one special character.',
{ minPasswordLength, maxPasswordLength }
)}
initialValue=""
autoComplete="new-password"
validate={
user && !changePassword
? dhis2Password
? passwordRegExValidator
: composeValidators(
hasValue,
dhis2Password
passwordRegExValidator
)
}
/>
Expand Down
14 changes: 11 additions & 3 deletions src/pages/UserList/ContextMenu/Modals/ReplicateModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Button,
ReactFinalForm,
InputFieldFF,
dhis2Password,
createPattern,
composeValidators,
hasValue,
} from '@dhis2/ui'
Expand All @@ -18,6 +18,7 @@ import React from 'react'
import { TextField } from '../../../../components/Form.js'
import { useUserNameValidator } from '../../../../components/UserForm/validators.js'
import { useFetchAlert } from '../../../../hooks/useFetchAlert.js'
import { useSystemInformation } from '../../../../providers/index.js'
import styles from './ReplicateModal.module.css'

const ReplicateModal = ({ user, refetchUsers, onClose }) => {
Expand All @@ -27,6 +28,9 @@ const ReplicateModal = ({ user, refetchUsers, onClose }) => {
isInviteUser: false,
})
const { showSuccess, showError } = useFetchAlert()
const { minPasswordLength, maxPasswordLength, passwordValidationPattern } =
useSystemInformation()
const passwordRegex = new RegExp(passwordValidationPattern)

const handleReplicate = async ({ username, password }) => {
try {
Expand Down Expand Up @@ -79,11 +83,15 @@ const ReplicateModal = ({ user, refetchUsers, onClose }) => {
type="password"
placeholder={i18n.t('Password for new user')}
helpText={i18n.t(
'Password should be at least 8 characters long, with at least one lowercase character, one uppercase character and one special character.'
'Password should be between {{minPasswordLength}} and {{maxPasswordLength}} characters long, with at least one lowercase character, one uppercase character, one number, and one special character.',
{ minPasswordLength, maxPasswordLength }
)}
validate={composeValidators(
hasValue,
dhis2Password
createPattern(
passwordRegex,
i18n.t('Invalid password')
)
)}
autoComplete="new-password"
className={styles.field}
Expand Down
7 changes: 6 additions & 1 deletion src/providers/system/SystemProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const query = {
resource: '/authorities',
},
systemSettings: {
resource: '/systemSettings/keyCanGrantOwnUserAuthorityGroups',
resource: '/systemSettings',
},
}

Expand Down Expand Up @@ -53,6 +53,11 @@ export const SystemProvider = ({ children }) => {
usersCanAssignOwnUserRoles: Boolean(
data.systemSettings?.keyCanGrantOwnUserAuthorityGroups
),
minPasswordLength: Number(data.systemSettings?.minPasswordLength),
maxPasswordLength: Number(data.systemSettings?.maxPasswordLength),
passwordValidationPattern:
data.systemSettings?.passwordValidationPattern ??
'^(?=.*[A-Z])(?=.*\\d)(?=.*[\\W_])[A-Za-z\\d\\W_]{8,32}$',
}

return (
Expand Down
Loading