diff --git a/i18n/en.pot b/i18n/en.pot index fd723b7b..c8b1e83f 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -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: 2025-01-07T11:04:56.155Z\n" -"PO-Revision-Date: 2025-01-07T11:04:56.155Z\n" +"POT-Creation-Date: 2025-01-09T11:55:44.018Z\n" +"PO-Revision-Date: 2025-01-09T11:55:44.019Z\n" msgid "Never" msgstr "Never" @@ -156,11 +156,13 @@ msgid "New password" msgstr "New password" msgid "" -"Password should be at least 8 characters with at least 1 digit, 1 uppercase " -"letter and 1 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 with at least 1 digit, 1 uppercase " -"letter and 1 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" diff --git a/src/account/AccountEditor.component.js b/src/account/AccountEditor.component.js index 2f02b98c..1bfd0d75 100644 --- a/src/account/AccountEditor.component.js +++ b/src/account/AccountEditor.component.js @@ -6,7 +6,6 @@ import React, { Component } from 'react' import appActions from '../app.actions.js' import i18n from '../locales/index.js' import accountActions from './account.actions.js' -import isValidPassword from './isValidPassword.js' const styles = { notification: { @@ -96,8 +95,16 @@ class AccountEditor extends Component { this.setState({ [e]: v }) } + validatePassword = (val, regEx) => (!val ? true : regEx.test(val)) + render() { const usesOpenIdConnect = this.context.d2.currentUser.externalAuth + const { + minPasswordLength, + maxPasswordLength, + passwordValidationPattern = '^(?=.*[A-Z])(?=.*\\d)(?=.*[\\W_])[A-Za-z\\d\\W_]{8,32}$', + } = this.context.d2.system.settings.settings + const passwordRegEx = new RegExp(passwordValidationPattern) const fields = [ { name: 'username', @@ -142,9 +149,11 @@ class AccountEditor extends Component { }, validators: [ { - validator: isValidPassword, + validator: (val) => + this.validatePassword(val, passwordRegEx), message: i18n.t( - 'Password should be at least 8 characters with at least 1 digit, 1 uppercase letter and 1 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 } ), }, ], diff --git a/src/account/isValidPassword.js b/src/account/isValidPassword.js deleted file mode 100644 index 4e46e774..00000000 --- a/src/account/isValidPassword.js +++ /dev/null @@ -1,23 +0,0 @@ -// Atleast one digit -const oneDigit = /^(?=.*\d)/ - -// Atleast one uppercase character -const oneUpperCase = /^(?=.*[A-Z])/ - -// Atleast one special character -// Using this regex to match all non-alphanumeric characters to match server-side implementation -// https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/SpecialCharacterValidationRule.java#L39 -const oneSpecialCharacter = /[^a-zA-Z0-9]/ - -export default function isValidPassword(value) { - if (!value) { - return true - } - return ( - oneDigit.test(value) && - oneUpperCase.test(value) && - oneSpecialCharacter.test(value) && - value.length > 7 && - value.length < 36 - ) -}