From 4b3e76afc264535c38644ea380f17c6d9d33b776 Mon Sep 17 00:00:00 2001 From: Chandrika Date: Wed, 13 May 2015 20:48:28 -0400 Subject: [PATCH] MIFOSX-2022 To enable force-password reset, the value of no of days should be greater than zero. --- .../GlobalConfigurationTest.java | 84 +++++++++++++++++++ .../common/GlobalConfigurationHelper.java | 19 +++++ .../domain/GlobalConfigurationProperty.java | 12 ++- .../ForcePasswordResetException.java | 17 ++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/exception/ForcePasswordResetException.java diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GlobalConfigurationTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GlobalConfigurationTest.java index 07d40dbeb..29a14e388 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GlobalConfigurationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/GlobalConfigurationTest.java @@ -11,6 +11,7 @@ import java.util.HashMap; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mifosplatform.integrationtests.common.CommonConstants; import org.mifosplatform.integrationtests.common.Utils; import org.mifosplatform.integrationtests.common.GlobalConfigurationHelper; @@ -26,6 +27,7 @@ public class GlobalConfigurationTest { private ResponseSpecification responseSpec; private RequestSpecification requestSpec; private GlobalConfigurationHelper globalConfigurationHelper; + private ResponseSpecification httpStatusForidden; @Before public void setup() { @@ -33,6 +35,7 @@ public class GlobalConfigurationTest { this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.httpStatusForidden = new ResponseSpecBuilder().expectStatusCode(403).build(); } @Test @@ -121,4 +124,85 @@ public class GlobalConfigurationTest { Assert.assertEquals("Verifying Is Cache Enabled Global Config after Updation", cacheTypeId, changes.get("cacheType")); } } + + @Test + public void testGlobalConfigForcePasswordResetDays() { + + // Retrieving All Global Configuration details + final ArrayList globalConfig = this.globalConfigurationHelper + .getAllGlobalConfigurations(this.requestSpec, this.responseSpec); + Assert.assertNotNull(globalConfig); + + String configName = "force-password-reset-days"; + String newValue = "0"; + String newBooleanValue = "true"; + + for (Integer configIndex = 0; configIndex < (globalConfig.size() - 1); configIndex++) { + if (globalConfig.get(configIndex).get("name").equals(configName)) { + Integer configId = (Integer) globalConfig.get(configIndex).get( + "id"); + Assert.assertNotNull(configId); + + /* + * Update force-password-reset-days with value as 0 and Enable + * as true - failure case + */ + ArrayList error = (ArrayList) this.globalConfigurationHelper + .updatePasswordResetDaysForGlobalConfiguration( + this.requestSpec, this.httpStatusForidden, + configId, newValue, newBooleanValue, + CommonConstants.RESPONSE_ERROR); + HashMap hash = (HashMap) error.get(0); + + Assert.assertEquals( + "Force Password Reset days value must be greater than zero.", + "error.msg.password.reset.days.value.must.be.greater.than.zero", + hash.get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE)); + + /* + * Update force-password-reset-days with value as 50 and Enable + * as true - success case + */ + final HashMap updateSuccess = (HashMap) this.globalConfigurationHelper + .updatePasswordResetDaysForGlobalConfiguration( + this.requestSpec, this.responseSpec, configId, + "50", newBooleanValue, "changes"); + Assert.assertNotNull(updateSuccess); + + /* Update with value as 0 and Enable as false - success case */ + final HashMap updateSuccess1 = (HashMap) this.globalConfigurationHelper + .updatePasswordResetDaysForGlobalConfiguration( + this.requestSpec, this.responseSpec, configId, + newValue, "false", "changes"); + Assert.assertNotNull(updateSuccess1); + + /* Update without sending value and Enable as true - failure case*/ + ArrayList failure = (ArrayList) this.globalConfigurationHelper + .updatePasswordResetDaysForGlobalConfiguration( + this.requestSpec, this.httpStatusForidden, configId, + null, newBooleanValue, CommonConstants.RESPONSE_ERROR); + HashMap failureHash = (HashMap) failure.get(0); + Assert.assertEquals( + "Force Password Reset days value must be greater than zero.", + "error.msg.password.reset.days.value.must.be.greater.than.zero", + failureHash.get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE)); + + break; + } + } + /* Update other global configuration property */ + String otherConfigName = "maker-checker"; + for (Integer configIndex = 0; configIndex < (globalConfig.size() - 1); configIndex++) { + if (globalConfig.get(configIndex).get("name") + .equals(otherConfigName)) { + String configId = (globalConfig.get(configIndex).get("id")) + .toString(); + Integer updateConfigId = this.globalConfigurationHelper + .updateValueForGlobalConfiguration(this.requestSpec, + this.responseSpec, configId, newValue); + Assert.assertNotNull(updateConfigId); + break; + } + } + } } \ No newline at end of file diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GlobalConfigurationHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GlobalConfigurationHelper.java index c603b373c..fcd5d0315 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GlobalConfigurationHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/GlobalConfigurationHelper.java @@ -7,6 +7,7 @@ package org.mifosplatform.integrationtests.common; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Random; import com.google.gson.Gson; @@ -70,6 +71,14 @@ public class GlobalConfigurationHelper { return Utils.performServerPut(requestSpec, responseSpec, IS_CACHE_GLOBAL_CONFIG_UPDATE_URL, updateIsCacheEnabledGlobalConfigUpdateAsJSON(cacheType), "changes"); } + + public static Object updatePasswordResetDaysForGlobalConfiguration(final RequestSpecification requestSpec, + final ResponseSpecification responseSpec, final Integer configId, final String value, final String enabled, final String jsonAttributeToGetBack) { + final String UPDATE_URL = "/mifosng-provider/api/v1/configurations/" + configId + "?" + Utils.TENANT_IDENTIFIER; + System.out.println("------------------UPDATE GLOBAL CONFIG FOR FORCE PASSWORD RESET DAYS----------------------"); + return Utils.performServerPut(requestSpec, responseSpec, UPDATE_URL, + updatePasswordResetDaysGlobalConfigAsJSON(value, enabled), jsonAttributeToGetBack); + } public static String updateGlobalConfigUpdateValueAsJSON(final String value) { final HashMap map = new HashMap<>(); @@ -77,6 +86,16 @@ public class GlobalConfigurationHelper { System.out.println("map : " + map); return new Gson().toJson(map); } + + public static String updatePasswordResetDaysGlobalConfigAsJSON(final String value, final String enabled) { + final HashMap map = new HashMap<>(); + if(value != null){ + map.put("value", value); + } + map.put("enabled", enabled); + System.out.println("map : " + map); + return new Gson().toJson(map); + } public static String updateGlobalConfigUpdateEnabledFlagAsJSON(final Boolean enabled) { final HashMap map = new HashMap(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/configuration/domain/GlobalConfigurationProperty.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/configuration/domain/GlobalConfigurationProperty.java index 424f5ed63..d591e1b72 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/configuration/domain/GlobalConfigurationProperty.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/configuration/domain/GlobalConfigurationProperty.java @@ -13,6 +13,7 @@ import javax.persistence.Entity; import javax.persistence.Table; import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.security.exception.ForcePasswordResetException; import org.springframework.data.jpa.domain.AbstractPersistable; @Entity @@ -72,12 +73,21 @@ public class GlobalConfigurationProperty extends AbstractPersistable { } final String valueParamName = "value"; + final Long previousValue = this.value; if (command.isChangeInLongParameterNamed(valueParamName, this.value)) { final Long newValue = command.longValueOfParameterNamed(valueParamName); actualChanges.put(valueParamName, newValue); this.value = newValue; } - + + final String passwordPropertyName = "force-password-reset-days"; + if(this.name.equalsIgnoreCase(passwordPropertyName)){ + if(this.enabled == true && command.hasParameter(valueParamName) && this.value == 0 || + this.enabled == true && !command.hasParameter(valueParamName) && previousValue == 0){ + throw new ForcePasswordResetException(); + } + } + return actualChanges; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/exception/ForcePasswordResetException.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/exception/ForcePasswordResetException.java new file mode 100644 index 000000000..577e983e3 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/exception/ForcePasswordResetException.java @@ -0,0 +1,17 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.mifosplatform.infrastructure.security.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException; + +public class ForcePasswordResetException extends AbstractPlatformDomainRuleException { + + public ForcePasswordResetException(){ + super("error.msg.password.reset.days.value.must.be.greater.than.zero" , "For enabling 'Force Password Reset Days' configuration , the value (number of days after which a user is forced to reset his password) must be set to a number greater than 0."); + } + +}