From d2923bc8510d05263a6ba9ad7aaad0b93e0a32b1 Mon Sep 17 00:00:00 2001 From: Hendrik de Graaf Date: Mon, 3 Oct 2022 15:06:37 +0200 Subject: [PATCH 1/5] feat: add credentialsExpiresReminderInDays setting --- i18n/en.pot | 10 ++++++++-- src/settingsCategories.js | 1 + src/settingsFields.component.js | 20 +++++++++++++++----- src/settingsKeyMapping.js | 13 +++++++++++++ src/validators/index.js | 30 ++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 src/validators/index.js diff --git a/i18n/en.pot b/i18n/en.pot index fd3e6c6b..3105bab8 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: 2022-03-14T13:17:46.441Z\n" -"PO-Revision-Date: 2022-03-14T13:17:46.441Z\n" +"POT-Creation-Date: 2022-10-03T13:02:47.948Z\n" +"PO-Revision-Date: 2022-10-03T13:02:47.948Z\n" msgid "Failed to load: {{error}}" msgstr "Failed to load: {{error}}" @@ -725,6 +725,9 @@ msgstr "12 months" msgid "Enable password expiry alerts" msgstr "Enable password expiry alerts" +msgid "Amount of days before credential expiry reminder is sent" +msgstr "Amount of days before credential expiry reminder is sent" + msgid "Minimum characters in password" msgstr "Minimum characters in password" @@ -796,3 +799,6 @@ msgstr "Remote server username" msgid "Remote server password" msgstr "Remote server password" + +msgid "This field should contain a round number between {{min}} and {{max}}" +msgstr "This field should contain a round number between {{min}} and {{max}}" diff --git a/src/settingsCategories.js b/src/settingsCategories.js index 8891a454..9ee0f134 100644 --- a/src/settingsCategories.js +++ b/src/settingsCategories.js @@ -124,6 +124,7 @@ export const categories = { 'keyAllowObjectAssignment', 'credentialsExpires', 'credentialsExpiryAlert', + 'credentialsExpiresReminderInDays', 'minPasswordLength', 'corsWhitelist', 'recaptchaSite', diff --git a/src/settingsFields.component.js b/src/settingsFields.component.js index 242dde18..27c2decb 100644 --- a/src/settingsFields.component.js +++ b/src/settingsFields.component.js @@ -297,15 +297,24 @@ class SettingsFields extends React.Component { // Base config, common for all component types const validators = [] if (mapping.validators) { - mapping.validators.forEach((name) => { - if (wordToValidatorMap.has(name)) { - const validator = wordToValidatorMap.get(name) + mapping.validators.forEach((validator) => { + if ( + typeof validator === 'string' && + wordToValidatorMap.has(validator) + ) { + const d2Validator = + wordToValidatorMap.get(validator) validators.push({ - validator, + validator: d2Validator, message: translateValidatorMessage( - validator.message + d2Validator.message ), }) + } else if (typeof validator === 'function') { + validators.push({ + validator, + message: validator.message, + }) } }) } @@ -322,6 +331,7 @@ class SettingsFields extends React.Component { ...addConditionallyHiddenStyles(mapping), }, hintText: mapping.hintText, + type: mapping.inputType, }, validators, } diff --git a/src/settingsKeyMapping.js b/src/settingsKeyMapping.js index 9654e011..3166bf73 100644 --- a/src/settingsKeyMapping.js +++ b/src/settingsKeyMapping.js @@ -1,4 +1,5 @@ import i18n from '@dhis2/d2-i18n' +import { createIntegerRangeValidator } from './validators/index.js' const canBeOverridenLabel = i18n.t( 'This setting can be overridden by user settings' @@ -560,6 +561,18 @@ const settingsKeyMapping = { label: i18n.t('Enable password expiry alerts'), type: 'checkbox', }, + credentialsExpiresReminderInDays: { + label: i18n.t( + 'Amount of days before credential expiry reminder is sent' + ), + type: 'textfield', + validators: [createIntegerRangeValidator(1, 28)], + inputType: 'number', + hideWhen: { + settingsKey: 'credentialsExpiryAlert', + settingsValue: 'false', + }, + }, minPasswordLength: { label: i18n.t('Minimum characters in password'), type: 'dropdown', diff --git a/src/validators/index.js b/src/validators/index.js new file mode 100644 index 00000000..c2ee912a --- /dev/null +++ b/src/validators/index.js @@ -0,0 +1,30 @@ +import i18n from '@dhis2/d2-i18n' + +const isInteger = (value) => { + if (typeof value !== 'string') { + return false + } + + return ( + !isNaN(value) && + parseInt(Number(value)) === Number(value) && + !isNaN(parseInt(value, 10)) + ) +} + +export function createIntegerRangeValidator(min, max) { + function integerRangeValidator(value) { + if (!isInteger(value)) { + return false + } + + const valueAsInt = parseInt(value) + + return valueAsInt >= min && valueAsInt <= max + } + integerRangeValidator.message = i18n.t( + 'This field should contain a round number between {{min}} and {{max}}', + { min, max } + ) + return integerRangeValidator +} From 31a9dc09cf565d3dc0e977cbcf3f08146cbeda02 Mon Sep 17 00:00:00 2001 From: Hendrik de Graaf Date: Mon, 3 Oct 2022 21:17:55 +0200 Subject: [PATCH 2/5] fix: implement field-specific validator --- src/settingsKeyMapping.js | 4 ++-- src/validators/index.js | 23 ++++++++++------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/settingsKeyMapping.js b/src/settingsKeyMapping.js index 3166bf73..762345b6 100644 --- a/src/settingsKeyMapping.js +++ b/src/settingsKeyMapping.js @@ -1,5 +1,5 @@ import i18n from '@dhis2/d2-i18n' -import { createIntegerRangeValidator } from './validators/index.js' +import { credentialsExpiresReminderInDaysValidator } from './validators/index.js' const canBeOverridenLabel = i18n.t( 'This setting can be overridden by user settings' @@ -566,7 +566,7 @@ const settingsKeyMapping = { 'Amount of days before credential expiry reminder is sent' ), type: 'textfield', - validators: [createIntegerRangeValidator(1, 28)], + validators: [credentialsExpiresReminderInDaysValidator], inputType: 'number', hideWhen: { settingsKey: 'credentialsExpiryAlert', diff --git a/src/validators/index.js b/src/validators/index.js index c2ee912a..8c9baedb 100644 --- a/src/validators/index.js +++ b/src/validators/index.js @@ -12,19 +12,16 @@ const isInteger = (value) => { ) } -export function createIntegerRangeValidator(min, max) { - function integerRangeValidator(value) { - if (!isInteger(value)) { - return false - } +export function credentialsExpiresReminderInDaysValidator(value) { + if (!isInteger(value)) { + return false + } - const valueAsInt = parseInt(value) + const valueAsInt = parseInt(value) - return valueAsInt >= min && valueAsInt <= max - } - integerRangeValidator.message = i18n.t( - 'This field should contain a round number between {{min}} and {{max}}', - { min, max } - ) - return integerRangeValidator + return valueAsInt >= 1 && valueAsInt <= 28 } + +credentialsExpiresReminderInDaysValidator.message = i18n.t( + 'This field should contain a round number between 1 and 28' +) From 161b6460771375b86b1b2804a1379dc894bf8cd5 Mon Sep 17 00:00:00 2001 From: Hendrik de Graaf Date: Wed, 5 Oct 2022 10:32:06 +0200 Subject: [PATCH 3/5] fix: adjust texts and use min/max attr on input instead of js validation --- src/settingsFields.component.js | 21 +++++++-------------- src/settingsKeyMapping.js | 8 ++++---- src/validators/index.js | 27 --------------------------- 3 files changed, 11 insertions(+), 45 deletions(-) delete mode 100644 src/validators/index.js diff --git a/src/settingsFields.component.js b/src/settingsFields.component.js index 27c2decb..8f139194 100644 --- a/src/settingsFields.component.js +++ b/src/settingsFields.component.js @@ -297,24 +297,15 @@ class SettingsFields extends React.Component { // Base config, common for all component types const validators = [] if (mapping.validators) { - mapping.validators.forEach((validator) => { - if ( - typeof validator === 'string' && - wordToValidatorMap.has(validator) - ) { - const d2Validator = - wordToValidatorMap.get(validator) + mapping.validators.forEach((name) => { + if (wordToValidatorMap.has(name)) { + const validator = wordToValidatorMap.get(name) validators.push({ - validator: d2Validator, + validator, message: translateValidatorMessage( - d2Validator.message + validator.message ), }) - } else if (typeof validator === 'function') { - validators.push({ - validator, - message: validator.message, - }) } }) } @@ -332,6 +323,8 @@ class SettingsFields extends React.Component { }, hintText: mapping.hintText, type: mapping.inputType, + min: mapping.minValue, + max: mapping.maxValue, }, validators, } diff --git a/src/settingsKeyMapping.js b/src/settingsKeyMapping.js index 762345b6..ce016d2e 100644 --- a/src/settingsKeyMapping.js +++ b/src/settingsKeyMapping.js @@ -1,5 +1,4 @@ import i18n from '@dhis2/d2-i18n' -import { credentialsExpiresReminderInDaysValidator } from './validators/index.js' const canBeOverridenLabel = i18n.t( 'This setting can be overridden by user settings' @@ -558,16 +557,17 @@ const settingsKeyMapping = { }, }, credentialsExpiryAlert: { - label: i18n.t('Enable password expiry alerts'), + label: i18n.t('Send reminders to users before their password expires'), type: 'checkbox', }, credentialsExpiresReminderInDays: { label: i18n.t( - 'Amount of days before credential expiry reminder is sent' + 'Number of days before password expiry to send reminder (0–28)' ), type: 'textfield', - validators: [credentialsExpiresReminderInDaysValidator], inputType: 'number', + minValue: 1, + maxValue: 28, hideWhen: { settingsKey: 'credentialsExpiryAlert', settingsValue: 'false', diff --git a/src/validators/index.js b/src/validators/index.js deleted file mode 100644 index 8c9baedb..00000000 --- a/src/validators/index.js +++ /dev/null @@ -1,27 +0,0 @@ -import i18n from '@dhis2/d2-i18n' - -const isInteger = (value) => { - if (typeof value !== 'string') { - return false - } - - return ( - !isNaN(value) && - parseInt(Number(value)) === Number(value) && - !isNaN(parseInt(value, 10)) - ) -} - -export function credentialsExpiresReminderInDaysValidator(value) { - if (!isInteger(value)) { - return false - } - - const valueAsInt = parseInt(value) - - return valueAsInt >= 1 && valueAsInt <= 28 -} - -credentialsExpiresReminderInDaysValidator.message = i18n.t( - 'This field should contain a round number between 1 and 28' -) From d3dfbb448b50a4d43e9104a9274fa7ecaa4ef81b Mon Sep 17 00:00:00 2001 From: Hendrik de Graaf Date: Wed, 5 Oct 2022 10:33:38 +0200 Subject: [PATCH 4/5] chore: update pot file --- i18n/en.pot | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index 3105bab8..70241824 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: 2022-10-03T13:02:47.948Z\n" -"PO-Revision-Date: 2022-10-03T13:02:47.948Z\n" +"POT-Creation-Date: 2022-10-05T08:32:39.671Z\n" +"PO-Revision-Date: 2022-10-05T08:32:39.671Z\n" msgid "Failed to load: {{error}}" msgstr "Failed to load: {{error}}" @@ -722,11 +722,11 @@ msgstr "6 months" msgid "12 months" msgstr "12 months" -msgid "Enable password expiry alerts" -msgstr "Enable password expiry alerts" +msgid "Send reminders to users before their password expires" +msgstr "Send reminders to users before their password expires" -msgid "Amount of days before credential expiry reminder is sent" -msgstr "Amount of days before credential expiry reminder is sent" +msgid "Number of days before password expiry to send reminder (0–28)" +msgstr "Number of days before password expiry to send reminder (0–28)" msgid "Minimum characters in password" msgstr "Minimum characters in password" @@ -799,6 +799,3 @@ msgstr "Remote server username" msgid "Remote server password" msgstr "Remote server password" - -msgid "This field should contain a round number between {{min}} and {{max}}" -msgstr "This field should contain a round number between {{min}} and {{max}}" From 67be38b147ea134eaf81b102e5404fc166099bc1 Mon Sep 17 00:00:00 2001 From: Stian Sandvold Date: Mon, 14 Aug 2023 09:40:00 +0200 Subject: [PATCH 5/5] Update src/settingsKeyMapping.js --- src/settingsKeyMapping.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/settingsKeyMapping.js b/src/settingsKeyMapping.js index ce016d2e..e0e96fc4 100644 --- a/src/settingsKeyMapping.js +++ b/src/settingsKeyMapping.js @@ -562,7 +562,7 @@ const settingsKeyMapping = { }, credentialsExpiresReminderInDays: { label: i18n.t( - 'Number of days before password expiry to send reminder (0–28)' + 'Number of days before password expiry to send reminder (1–28)' ), type: 'textfield', inputType: 'number',