diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientStatusChecker.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientStatusChecker.java index 32dde68f1..b4de63702 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientStatusChecker.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientStatusChecker.java @@ -10,38 +10,39 @@ import static org.junit.Assert.assertEquals; import java.util.HashMap; public class ClientStatusChecker { - public static void verifyClientIsActive(final HashMap clientStatusHashMap) { - assertEquals((int) clientStatusHashMap.get("id"), 300); - } - - public static void verifyClientClosed(final HashMap clientStatusHashMap) { - System.out.println("\n-------------------------------------- VERIFYING CLIENT IS CLOSED ------------------------------------"); - assertEquals((int) clientStatusHashMap.get("id"), 600); - System.out.println("Client Status:" + clientStatusHashMap + "\n"); - } - public static void verifyClientPending(final HashMap clientStatusHashMap) { - System.out.println("\n-------------------------------------- VERIFYING CLIENT IS PENDING ------------------------------------"); - assertEquals((int) clientStatusHashMap.get("id"), 100); - System.out.println("Client Status:" + clientStatusHashMap + "\n"); - } - public static void verifyClientRejected(final HashMap clientStatusHashMap) { - System.out.println("\n-------------------------------------- VERIFYING CLIENT IS REJECTED ------------------------------------"); - assertEquals((int) clientStatusHashMap.get("id"), 700); - System.out.println("Client Status:" + clientStatusHashMap + "\n"); - } - - public static void verifyClientActiavted(final HashMap clientStatusHashMap) { - System.out.println("\n-------------------------------------- VERIFYING CLIENT IS ACTIVATED ------------------------------------"); - assertEquals((int) clientStatusHashMap.get("id"), 300); - System.out.println("Client Status:" + clientStatusHashMap + "\n"); - } - - public static void verifyClientWithdrawn(final HashMap clientStatusHashMap) { - System.out.println("\n-------------------------------------- VERIFYING CLIENT IS WITHDRAWN ------------------------------------"); - assertEquals((int) clientStatusHashMap.get("id"), 800); - System.out.println("Client Status:" + clientStatusHashMap + "\n"); - } - - + public static void verifyClientIsActive(final HashMap clientStatusHashMap) { + assertEquals((int) clientStatusHashMap.get("id"), 300); + } + + public static void verifyClientClosed(final HashMap clientStatusHashMap) { + System.out.println("\n-------------------------------------- VERIFYING CLIENT IS CLOSED ------------------------------------"); + assertEquals((int) clientStatusHashMap.get("id"), 600); + System.out.println("Client Status:" + clientStatusHashMap + "\n"); + } + + public static void verifyClientPending(final HashMap clientStatusHashMap) { + System.out.println("\n-------------------------------------- VERIFYING CLIENT IS PENDING ------------------------------------"); + assertEquals((int) clientStatusHashMap.get("id"), 100); + System.out.println("Client Status:" + clientStatusHashMap + "\n"); + } + + public static void verifyClientRejected(final HashMap clientStatusHashMap) { + System.out.println("\n-------------------------------------- VERIFYING CLIENT IS REJECTED ------------------------------------"); + assertEquals((int) clientStatusHashMap.get("id"), 700); + System.out.println("Client Status:" + clientStatusHashMap + "\n"); + } + + public static void verifyClientActiavted(final HashMap clientStatusHashMap) { + System.out.println("\n-------------------------------------- VERIFYING CLIENT IS ACTIVATED ------------------------------------"); + assertEquals((int) clientStatusHashMap.get("id"), 300); + System.out.println("Client Status:" + clientStatusHashMap + "\n"); + } + + public static void verifyClientWithdrawn(final HashMap clientStatusHashMap) { + System.out.println("\n-------------------------------------- VERIFYING CLIENT IS WITHDRAWN ------------------------------------"); + assertEquals((int) clientStatusHashMap.get("id"), 800); + System.out.println("Client Status:" + clientStatusHashMap + "\n"); + } + } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientTest.java index 8bb8194c1..3351cd230 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/ClientTest.java @@ -40,10 +40,10 @@ public class ClientTest { final Integer clientId = ClientHelper.createClient(this.requestSpec, this.responseSpec); Assert.assertNotNull(clientId); - HashMap status = ClientHelper.getClientStatus(requestSpec, responseSpec, String.valueOf(clientId)); + HashMap status = ClientHelper.getClientStatus(requestSpec, responseSpec, String.valueOf(clientId)); ClientStatusChecker.verifyClientIsActive(status); - HashMap clientStatusHashMap = this.clientHelper.closeClient(clientId); + HashMap clientStatusHashMap = this.clientHelper.closeClient(clientId); ClientStatusChecker.verifyClientClosed(clientStatusHashMap); clientStatusHashMap = this.clientHelper.reactivateClient(clientId); diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/RolesTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/RolesTest.java new file mode 100644 index 000000000..8bea0ce89 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/RolesTest.java @@ -0,0 +1,116 @@ +/** + * 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.integrationtests; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mifosplatform.integrationtests.common.Utils; +import org.mifosplatform.integrationtests.useradministration.roles.RolesHelper; + +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.builder.ResponseSpecBuilder; +import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +public class RolesTest { + + private ResponseSpecification responseSpec; + private RequestSpecification requestSpec; + + @Before + public void setup() { + Utils.initializeRESTAssured(); + this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); + this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); + this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + } + + @SuppressWarnings("cast") + @Test + public void testCreateRolesStatus() { + + System.out.println("---------------------------------CREATING A ROLE---------------------------------------------"); + final Integer roleId = RolesHelper.createRole(this.requestSpec, this.responseSpec); + Assert.assertNotNull(roleId); + + System.out.println("--------------------------------- Getting ROLE -------------------------------"); + HashMap role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + + } + + @SuppressWarnings("cast") + @Test + public void testDisableRolesStatus() { + + System.out.println("---------------------------------CREATING A ROLE---------------------------------------------"); + final Integer roleId = RolesHelper.createRole(this.requestSpec, this.responseSpec); + Assert.assertNotNull(roleId); + + System.out.println("--------------------------------- Getting ROLE -------------------------------"); + HashMap role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + + System.out.println("--------------------------------- DISABLING ROLE -------------------------------"); + final Integer disableRoleId = RolesHelper.disableRole(this.requestSpec, this.responseSpec, roleId); + assertEquals(disableRoleId, roleId); + role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + assertEquals((Boolean) role.get("disabled"), true); + + } + + @SuppressWarnings("cast") + @Test + public void testEnableRolesStatus() { + + System.out.println("---------------------------------CREATING A ROLE---------------------------------------------"); + final Integer roleId = RolesHelper.createRole(this.requestSpec, this.responseSpec); + Assert.assertNotNull(roleId); + + System.out.println("--------------------------------- Getting ROLE -------------------------------"); + HashMap role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + + System.out.println("--------------------------------- DISABLING ROLE -------------------------------"); + final Integer disableRoleId = RolesHelper.disableRole(this.requestSpec, this.responseSpec, roleId); + assertEquals(disableRoleId, roleId); + role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + assertEquals((Boolean) role.get("disabled"), true); + + System.out.println("--------------------------------- ENABLING ROLE -------------------------------"); + final Integer enableRoleId = RolesHelper.enableRole(this.requestSpec, this.responseSpec, roleId); + assertEquals(enableRoleId, roleId); + role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + assertEquals((Boolean) role.get("disabled"), false); + + } + + @SuppressWarnings("cast") + @Test + public void testDeleteRoleStatus() { + + System.out.println("-------------------------------- CREATING A ROLE---------------------------------------------"); + final Integer roleId = RolesHelper.createRole(this.requestSpec, this.responseSpec); + Assert.assertNotNull(roleId); + + System.out.println("--------------------------------- Getting ROLE -------------------------------"); + HashMap role = RolesHelper.getRoleDetails(requestSpec, responseSpec, roleId); + assertEquals((Integer) role.get("id"), roleId); + + System.out.println("--------------------------------- DELETE ROLE -------------------------------"); + final Integer deleteRoleId = RolesHelper.deleteRole(this.requestSpec, this.responseSpec, roleId); + assertEquals(deleteRoleId, roleId); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java index b06b24e06..689e1c00a 100755 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/ClientHelper.java @@ -133,7 +133,7 @@ public class ClientHelper { /* Client status is a map.So adding SuppressWarnings */ @SuppressWarnings("unchecked") - public static HashMap getClientStatus(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + public static HashMap getClientStatus(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final String clientId) { return (HashMap) getClient(requestSpec, responseSpec, clientId, "status"); } @@ -151,9 +151,9 @@ public class ClientHelper { Integer clientClosureCodeId = (Integer) code.get("id"); /* Retrieve/Create Code Values for the Code "ClientClosureReason" */ - HashMap codeValue = CodeHelper.retrieveOrCreateCodeValue(clientClosureCodeId,this.requestSpec,this.responseSpec); - Integer closureReasonId = (Integer)codeValue.get("id"); - + HashMap codeValue = CodeHelper.retrieveOrCreateCodeValue(clientClosureCodeId, this.requestSpec, this.responseSpec); + Integer closureReasonId = (Integer) codeValue.get("id"); + map.put("closureReasonId", closureReasonId.toString()); map.put("locale", CommonConstants.locale); map.put("dateFormat", CommonConstants.dateFormat); @@ -180,12 +180,13 @@ public class ClientHelper { final HashMap map = new HashMap<>(); /* Retrieve Code id for the Code "ClientRejectReason" */ String codeName = "ClientRejectReason"; - HashMap code = CodeHelper.getCodeByName(this.requestSpec, this.responseSpec, codeName); - Integer clientRejectionReasonCodeId = (Integer)code.get("id"); - + HashMap code = CodeHelper.getCodeByName(this.requestSpec, this.responseSpec, codeName); + Integer clientRejectionReasonCodeId = (Integer) code.get("id"); + /* Retrieve/Create Code Values for the Code "ClientRejectReason" */ - HashMap codeValue = CodeHelper.retrieveOrCreateCodeValue(clientRejectionReasonCodeId,this.requestSpec,this.responseSpec); - Integer rejectionReasonId = (Integer)codeValue.get("id"); + HashMap codeValue = CodeHelper.retrieveOrCreateCodeValue(clientRejectionReasonCodeId, this.requestSpec, + this.responseSpec); + Integer rejectionReasonId = (Integer) codeValue.get("id"); map.put("locale", CommonConstants.locale); map.put("dateFormat", CommonConstants.dateFormat); @@ -212,13 +213,14 @@ public class ClientHelper { final HashMap map = new HashMap<>(); /* Retrieve Code id for the Code "ClientWithdrawReason" */ String codeName = "ClientWithdrawReason"; - HashMap code = CodeHelper.getCodeByName(this.requestSpec, this.responseSpec, codeName); - Integer clientWithdrawReasonCodeId = (Integer)code.get("id"); - + HashMap code = CodeHelper.getCodeByName(this.requestSpec, this.responseSpec, codeName); + Integer clientWithdrawReasonCodeId = (Integer) code.get("id"); + /* Retrieve/Create Code Values for the Code "ClientWithdrawReason" */ - HashMap codeValue = CodeHelper.retrieveOrCreateCodeValue(clientWithdrawReasonCodeId,this.requestSpec,this.responseSpec); - Integer withdrawalReasonId = (Integer)codeValue.get("id"); - + HashMap codeValue = CodeHelper.retrieveOrCreateCodeValue(clientWithdrawReasonCodeId, this.requestSpec, + this.responseSpec); + Integer withdrawalReasonId = (Integer) codeValue.get("id"); + map.put("locale", CommonConstants.locale); map.put("dateFormat", CommonConstants.dateFormat); map.put("withdrawalDate", CREATED_DATE_PLUS_ONE); @@ -229,27 +231,27 @@ public class ClientHelper { } - public HashMap closeClient(final Integer clientId) { + public HashMap closeClient(final Integer clientId) { System.out.println("--------------------------------- CLOSE CLIENT -------------------------------"); return performClientActions(createClientOperationURL(CLOSE_CLIENT_COMMAND, clientId), getCloseClientAsJSON(), clientId); } - public HashMap reactivateClient(final Integer clientId) { + public HashMap reactivateClient(final Integer clientId) { System.out.println("--------------------------------- REACTIVATE CLIENT -------------------------------"); return performClientActions(createClientOperationURL(REACTIVATE_CLIENT_COMMAND, clientId), getReactivateClientAsJSON(), clientId); } - public HashMap rejectClient(final Integer clientId) { + public HashMap rejectClient(final Integer clientId) { System.out.println("--------------------------------- REJECT CLIENT -------------------------------"); return performClientActions(createClientOperationURL(REJECT_CLIENT_COMMAND, clientId), getRejectClientAsJSON(), clientId); } - public HashMap activateClient(final Integer clientId) { + public HashMap activateClient(final Integer clientId) { System.out.println("--------------------------------- ACTIVATE CLIENT -------------------------------"); return performClientActions(createClientOperationURL(ACTIVATE_CLIENT_COMMAND, clientId), getActivateClientAsJSON(), clientId); } - public HashMap withdrawClient(final Integer clientId) { + public HashMap withdrawClient(final Integer clientId) { System.out.println("--------------------------------- WITHDRAWN CLIENT -------------------------------"); return performClientActions(createClientOperationURL(WITHDRAW_CLIENT_COMMAND, clientId), getWithdrawClientAsJSON(), clientId); } @@ -258,9 +260,9 @@ public class ClientHelper { return CLIENT_URL + "/" + clientId + "?command=" + command + "&" + Utils.TENANT_IDENTIFIER; } - private HashMap performClientActions(final String postURLForClient, final String jsonToBeSent, final Integer clientId) { + private HashMap performClientActions(final String postURLForClient, final String jsonToBeSent, final Integer clientId) { Utils.performServerPost(this.requestSpec, this.responseSpec, postURLForClient, jsonToBeSent, CommonConstants.RESPONSE_STATUS); - HashMap response = ClientHelper.getClientStatus(requestSpec, responseSpec, String.valueOf(clientId)); + HashMap response = ClientHelper.getClientStatus(requestSpec, responseSpec, String.valueOf(clientId)); return response; } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/Utils.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/Utils.java index ae7e31429..de6378e75 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/Utils.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/Utils.java @@ -25,10 +25,9 @@ import com.jayway.restassured.path.json.JsonPath; import com.jayway.restassured.specification.RequestSpecification; import com.jayway.restassured.specification.ResponseSpecification; - /** - * Util for RestAssured tests. - * This class here in src/integrationTest is copy/pasted to src/test; please keep them in sync. + * Util for RestAssured tests. This class here in src/integrationTest is + * copy/pasted to src/test; please keep them in sync. */ @SuppressWarnings("unchecked") public class Utils { @@ -116,7 +115,7 @@ public class Utils { public static String randomNameGenerator(final String prefix, final int lenOfRandomSuffix) { return randomStringGenerator(prefix, lenOfRandomSuffix); } - + public static String convertDateToURLFormat(final Calendar dateToBeConvert) { return new SimpleDateFormat("dd MMMMMM yyyy").format(dateToBeConvert.getTime()); } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/useradministration/roles/RolesHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/useradministration/roles/RolesHelper.java new file mode 100644 index 000000000..7a7586ed3 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/useradministration/roles/RolesHelper.java @@ -0,0 +1,57 @@ +/** + * 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.integrationtests.useradministration.roles; + +import java.util.HashMap; + +import org.mifosplatform.integrationtests.common.Utils; + +import com.google.gson.Gson; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +public class RolesHelper { + + private static final String CREATE_ROLE_URL = "/mifosng-provider/api/v1/roles?" + Utils.TENANT_IDENTIFIER; + private static final String ROLE_URL = "/mifosng-provider/api/v1/roles"; + private static final String DISABLE_ROLE_COMMAND = "disable"; + private static final String ENABLE_ROLE_COMMAND = "enable"; + + public static Integer createRole(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { + return Utils.performServerPost(requestSpec, responseSpec, CREATE_ROLE_URL, getTestCreaRoleAsJSON(), "resourceId"); + } + + public static String getTestCreaRoleAsJSON() { + final HashMap map = new HashMap<>(); + map.put("name", Utils.randomNameGenerator("Role_Name_", 5)); + map.put("description", Utils.randomNameGenerator("Role_Description_", 10)); + return new Gson().toJson(map); + } + + public static HashMap getRoleDetails(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final Integer roleId) { + final String GET_ROLE_URL = "/mifosng-provider/api/v1/roles/" + roleId + "?" + Utils.TENANT_IDENTIFIER; + HashMap role = Utils.performServerGet(requestSpec, responseSpec, GET_ROLE_URL, ""); + return role; + } + + public static Integer disableRole(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer roleId) { + return Utils.performServerPost(requestSpec, responseSpec, createRoleOperationURL(DISABLE_ROLE_COMMAND, roleId), "", "resourceId"); + } + + public static Integer enableRole(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer roleId) { + return Utils.performServerPost(requestSpec, responseSpec, createRoleOperationURL(ENABLE_ROLE_COMMAND, roleId), "", "resourceId"); + } + + public static Integer deleteRole(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer roleId) { + return Utils.performServerDelete(requestSpec, responseSpec, createRoleOperationURL(ENABLE_ROLE_COMMAND, roleId), "resourceId"); + } + + private static String createRoleOperationURL(final String command, final Integer roleId) { + return ROLE_URL + "/" + roleId + "?command=" + command + "&" + Utils.TENANT_IDENTIFIER; + } + +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java index b0ea071f4..8dc89280a 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java @@ -967,17 +967,32 @@ public class CommandWrapper { public boolean isInactivateSavingsAccountCharge() { return this.actionName.equalsIgnoreCase("INACTIVATE") && this.entityName.equalsIgnoreCase("SAVINGSACCOUNTCHARGE"); } - + public boolean isRefundByTransfer() { return this.actionName.equalsIgnoreCase("REFUNDBYTRANSFER"); } - + public boolean isLoanRefundByCash() { return this.actionName.equalsIgnoreCase("REFUNDBYCASH") && this.entityName.equalsIgnoreCase("LOAN"); } - + public boolean isUndoLoanRefund() { return this.actionName.equalsIgnoreCase("UNDOREFUND") && this.entityName.equalsIgnoreCase("LOAN"); } + public boolean isDisable() { + return isDisableOperation() && this.entityId != null; + } + + public boolean isDisableOperation() { + return this.actionName.equalsIgnoreCase("DISABLE"); + } + + public boolean isEnable() { + return isEnableOperation() && this.entityId != null; + } + + public boolean isEnableOperation() { + return this.actionName.equalsIgnoreCase("ENABLE"); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java index 0046396d1..3214fd856 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java @@ -61,7 +61,7 @@ public class CommandWrapperBuilder { this.subentityId = withSubEntityId; return this; } - + public CommandWrapperBuilder withTellerId(final Long withTellerId) { this.tellerId = withTellerId; return this; @@ -703,7 +703,7 @@ public class CommandWrapperBuilder { this.href = "/loans/" + loanId + "/transactions/" + transactionId; return this; } - + public CommandWrapperBuilder refundLoanTransactionByCash(final Long loanId) { this.actionName = "REFUNDBYCASH"; this.entityName = "LOAN"; @@ -2200,7 +2200,7 @@ public class CommandWrapperBuilder { this.json = "{}"; return this; } - + public CommandWrapperBuilder refundByTransfer() { this.actionName = "REFUNDBYTRANSFER"; this.entityName = "ACCOUNTTRANSFER"; @@ -2208,7 +2208,7 @@ public class CommandWrapperBuilder { this.href = "/refundByTransfer"; return this; } - + public CommandWrapperBuilder createTeller() { this.actionName = "CREATE"; this.entityName = "TELLER"; @@ -2258,7 +2258,7 @@ public class CommandWrapperBuilder { this.href = "/tellers/" + tellerId + "/cashiers/" + cashierId; return this; } - + public CommandWrapperBuilder allocateCashToCashier(final Long tellerId, final Long cashierId) { this.actionName = "ALLOCATECASHTOCASHIER"; this.entityName = "TELLER"; @@ -2267,7 +2267,7 @@ public class CommandWrapperBuilder { this.href = "/tellers/" + tellerId + "/cashiers/" + cashierId + "/allocate"; return this; } - + public CommandWrapperBuilder settleCashFromCashier(final Long tellerId, final Long cashierId) { this.actionName = "SETTLECASHFROMCASHIER"; this.entityName = "TELLER"; @@ -2277,4 +2277,31 @@ public class CommandWrapperBuilder { return this; } + public CommandWrapperBuilder deleteRole(Long roleId) { + this.actionName = "DELETE"; + this.entityName = "ROLE"; + this.entityId = roleId; + this.href = "/roles/" + roleId; + this.json = "{}"; + return this; + } + + public CommandWrapperBuilder disableRole(Long roleId) { + this.actionName = "DISABLE"; + this.entityName = "ROLE"; + this.entityId = roleId; + this.href = "/roles/" + roleId + "/disbales"; + this.json = "{}"; + return this; + } + + public CommandWrapperBuilder enableRole(Long roleId) { + this.actionName = "ENABLE"; + this.entityName = "ROLE"; + this.entityId = roleId; + this.href = "/roles/" + roleId + "/enable"; + this.json = "{}"; + return this; + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java index 86f210042..e458e041e 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java @@ -136,6 +136,6 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio final AppUser maker = this.context.authenticatedUser(); commandSourceInput.markAsRejected(maker, DateTime.now()); this.commandSourceRepository.save(commandSourceInput); - return makerCheckerId ; + return makerCheckerId; } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java index 6cc04721e..131f527b2 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java @@ -221,15 +221,19 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer } else if (wrapper.isPermissionResource()) { handler = this.applicationContext.getBean("updateMakerCheckerPermissionsCommandHandler", NewCommandSourceHandler.class); } else if (wrapper.isRoleResource()) { - if (wrapper.isCreate()) { handler = this.applicationContext.getBean("createRoleCommandHandler", NewCommandSourceHandler.class); } else if (wrapper.isUpdate()) { handler = this.applicationContext.getBean("updateRoleCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isDelete()) { + handler = this.applicationContext.getBean("deleteRoleCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isDisable()) { + handler = this.applicationContext.getBean("disableRoleCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isEnable()) { + handler = this.applicationContext.getBean("enableRoleCommandHandler", NewCommandSourceHandler.class); } else { throw new UnsupportedCommandException(wrapper.commandName()); } - } else if (wrapper.isUserResource()) { if (wrapper.isCreate()) { handler = this.applicationContext.getBean("createUserCommandHandler", NewCommandSourceHandler.class); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/RolesApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/RolesApiResource.java index 92eecbe4d..191922abc 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/RolesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/api/RolesApiResource.java @@ -11,16 +11,19 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriInfo; +import org.apache.commons.lang.StringUtils; import org.mifosplatform.commands.domain.CommandWrapper; import org.mifosplatform.commands.service.CommandWrapperBuilder; import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService; @@ -46,8 +49,8 @@ public class RolesApiResource { /** * The set of parameters that are supported in response for {@link RoleData} */ - private final Set RESPONSE_DATA_PARAMETERS = new HashSet<>(Arrays.asList("id", "name", "description", - "availablePermissions", "selectedPermissions")); + private final Set RESPONSE_DATA_PARAMETERS = new HashSet<>(Arrays.asList("id", "name", "description", "availablePermissions", + "selectedPermissions")); /** * The set of parameters that are supported in response for {@link RoleData} @@ -124,6 +127,35 @@ public class RolesApiResource { return this.toApiJsonSerializer.serialize(settings, role, this.RESPONSE_DATA_PARAMETERS); } + /** + * Roles enable or disable + * + * @param roleId + * @param commandParam + * @param apiRequestBodyAsJson + * @return + */ + @POST + @Path("{roleId}") + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) + public String actionsOnRoles(@PathParam("roleId") final Long roleId, @QueryParam("command") final String commandParam, + final String apiRequestBodyAsJson) { + + final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(apiRequestBodyAsJson); + + CommandProcessingResult result = null; + + if (is(commandParam, "disable")) { + final CommandWrapper commandRequest = builder.disableRole(roleId).build(); + result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + } else if (is(commandParam, "enable")) { + final CommandWrapper commandRequest = builder.enableRole(roleId).build(); + result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + } + return this.toApiJsonSerializer.serialize(result); + } + @PUT @Path("{roleId}") @Consumes({ MediaType.APPLICATION_JSON }) @@ -171,4 +203,30 @@ public class RolesApiResource { return this.toApiJsonSerializer.serialize(result); } + + /** + * Delete Role + * + * @param roleId + * @return + */ + @DELETE + @Path("{roleId}") + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) + public String deleteRole(@PathParam("roleId") final Long roleId) { + + final CommandWrapper commandRequest = new CommandWrapperBuilder() // + .deleteRole(roleId) // + .build(); + + final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); + + return this.toApiJsonSerializer.serialize(result); + } + + private boolean is(final String commandParam, final String commandValue) { + return StringUtils.isNotBlank(commandParam) && commandParam.trim().equalsIgnoreCase(commandValue); + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RoleData.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RoleData.java index ef25de8af..1c2e236f9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RoleData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RoleData.java @@ -16,15 +16,17 @@ public class RoleData implements Serializable { private final Long id; private final String name; private final String description; + private final Boolean disabled; public RolePermissionsData toRolePermissionData(final Collection permissionUsageData) { - return new RolePermissionsData(this.id, this.name, this.description, permissionUsageData); + return new RolePermissionsData(this.id, this.name, this.description, this.disabled, permissionUsageData); } - public RoleData(final Long id, final String name, final String description) { + public RoleData(final Long id, final String name, final String description, final Boolean disabled) { this.id = id; this.name = name; this.description = description; + this.disabled = disabled; } @Override diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RolePermissionsData.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RolePermissionsData.java index 3b5539df7..3fa0372a4 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RolePermissionsData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/data/RolePermissionsData.java @@ -18,15 +18,18 @@ public class RolePermissionsData { private final String name; @SuppressWarnings("unused") private final String description; + @SuppressWarnings("unused") + private final Boolean disabled; @SuppressWarnings("unused") private final Collection permissionUsageData; - public RolePermissionsData(final Long id, final String name, final String description, + public RolePermissionsData(final Long id, final String name, final String description, final Boolean disabled, final Collection permissionUsageData) { this.id = id; this.name = name; this.description = description; + this.disabled = disabled; this.permissionUsageData = permissionUsageData; } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java index e69a0b9f1..deaab498c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUser.java @@ -494,7 +494,6 @@ public class AppUser extends AbstractPersistable implements PlatformUser { } public void validateHasCheckerPermissionTo(final String function) { - final String checkerPermissionName = function.toUpperCase() + "_CHECKER"; if (hasNotPermissionTo("CHECKER_SUPER_USER") && hasNotPermissionTo(checkerPermissionName)) { final String authorizationMessage = "User has no authority to be a checker for: " + function; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUserRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUserRepository.java index b15053cd3..b27bfc9f4 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUserRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/AppUserRepository.java @@ -10,5 +10,5 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface AppUserRepository extends JpaRepository, JpaSpecificationExecutor, PlatformUserRepository { - // no behaviour added + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/Role.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/Role.java index 5daaf914c..35115cd9a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/Role.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/Role.java @@ -34,6 +34,9 @@ public class Role extends AbstractPersistable { @Column(name = "description", nullable = false, length = 500) private String description; + @Column(name = "is_disabled", nullable = false) + private Boolean disabled; + @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "m_role_permission", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "permission_id")) private final Set permissions = new HashSet<>(); @@ -51,6 +54,7 @@ public class Role extends AbstractPersistable { public Role(final String name, final String description) { this.name = name.trim(); this.description = description.trim(); + this.disabled = false; } public Map update(final JsonCommand command) { @@ -109,6 +113,22 @@ public class Role extends AbstractPersistable { } public RoleData toData() { - return new RoleData(getId(), this.name, this.description); + return new RoleData(getId(), this.name, this.description, this.disabled); + } + + public void disableRole() { + this.disabled = true; + } + + public Boolean isDisabled() { + return this.disabled; + } + + public void enableRole() { + this.disabled = false; + } + + public Boolean isEnabled() { + return this.disabled; } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/RoleRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/RoleRepository.java index 0bcbcfd42..9f5b2260b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/RoleRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/domain/RoleRepository.java @@ -7,7 +7,12 @@ package org.mifosplatform.useradministration.domain; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface RoleRepository extends JpaRepository, JpaSpecificationExecutor { - // no added behaviour + + @Query("SELECT COUNT(*) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId") + Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId); + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/exception/RoleAssociatedException.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/exception/RoleAssociatedException.java new file mode 100644 index 000000000..092a966c4 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/exception/RoleAssociatedException.java @@ -0,0 +1,16 @@ +/** + * 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.useradministration.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException; + + +public class RoleAssociatedException extends AbstractPlatformDomainRuleException { + + public RoleAssociatedException(final String errorcode, final Long id) { + super(errorcode, "Role with identifier " + id + " associated with users", id); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/DeleteRoleCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/DeleteRoleCommandHandler.java new file mode 100644 index 000000000..83ee8f9a9 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/DeleteRoleCommandHandler.java @@ -0,0 +1,32 @@ +/** + * 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.useradministration.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.useradministration.service.RoleWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class DeleteRoleCommandHandler implements NewCommandSourceHandler { + + private final RoleWritePlatformService writePlatformService; + + @Autowired + public DeleteRoleCommandHandler(final RoleWritePlatformService writePlatformService) { + this.writePlatformService = writePlatformService; + } + + @Override + @Transactional + public CommandProcessingResult processCommand(final JsonCommand command) { + return this.writePlatformService.deleteRole(command.entityId()); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/DisableRoleCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/DisableRoleCommandHandler.java new file mode 100644 index 000000000..42e259ac6 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/DisableRoleCommandHandler.java @@ -0,0 +1,30 @@ +/** + * 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.useradministration.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.useradministration.service.RoleWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DisableRoleCommandHandler implements NewCommandSourceHandler { + + private final RoleWritePlatformService writePlatformService; + + @Autowired + public DisableRoleCommandHandler(final RoleWritePlatformService writePlatformService) { + this.writePlatformService = writePlatformService; + } + + @Override + public CommandProcessingResult processCommand(JsonCommand command) { + return this.writePlatformService.disableRole(command.entityId()); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/EnableRoleCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/EnableRoleCommandHandler.java new file mode 100644 index 000000000..a6b5255b4 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/handler/EnableRoleCommandHandler.java @@ -0,0 +1,32 @@ +/** + * 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.useradministration.handler; + +import org.mifosplatform.commands.handler.NewCommandSourceHandler; +import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; +import org.mifosplatform.useradministration.service.RoleWritePlatformService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class EnableRoleCommandHandler implements NewCommandSourceHandler { + + private final RoleWritePlatformService writePlatformService; + + @Autowired + public EnableRoleCommandHandler(final RoleWritePlatformService writePlatformService) { + this.writePlatformService = writePlatformService; + } + + @Transactional + @Override + public CommandProcessingResult processCommand(JsonCommand command) { + return this.writePlatformService.enableRole(command.entityId()); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserReadPlatformServiceImpl.java index 0feaa5bc5..d6148b280 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/AppUserReadPlatformServiceImpl.java @@ -89,7 +89,7 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic public AppUserData retrieveNewUserDetails() { final Collection offices = this.officeReadPlatformService.retrieveAllOfficesForDropdown(); - final Collection availableRoles = this.roleReadPlatformService.retrieveAll(); + final Collection availableRoles = this.roleReadPlatformService.retrieveAllActiveRoles(); return AppUserData.template(offices, availableRoles); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformService.java index bec415bba..1383648e1 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformService.java @@ -13,6 +13,8 @@ public interface RoleReadPlatformService { Collection retrieveAll(); + Collection retrieveAllActiveRoles(); + RoleData retrieveOne(Long roleId); Collection retrieveAppUserRoles(Long appUserId); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformServiceImpl.java index 02bf88e1d..7f53a1f48 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleReadPlatformServiceImpl.java @@ -38,6 +38,13 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService { return this.jdbcTemplate.query(sql, this.roleRowMapper); } + @Override + public Collection retrieveAllActiveRoles() { + final String sql = "select " + this.roleRowMapper.schema() + " where r.is_disabled = 0 order by r.id"; + + return this.jdbcTemplate.query(sql, this.roleRowMapper); + } + @Override public RoleData retrieveOne(final Long id) { @@ -58,12 +65,13 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService { final Long id = JdbcSupport.getLong(rs, "id"); final String name = rs.getString("name"); final String description = rs.getString("description"); - - return new RoleData(id, name, description); + final Boolean disabled = rs.getBoolean("disabled"); + + return new RoleData(id, name, description, disabled); } public String schema() { - return " r.id as id, r.name as name, r.description as description from m_role r"; + return " r.id as id, r.name as name, r.description as description, r.is_disabled as disabled from m_role r"; } } @@ -72,6 +80,6 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService { final String sql = "select " + this.roleRowMapper.schema() + " inner join m_appuser_role" + " ar on ar.role_id = r.id where ar.appuser_id= ?"; - return this.jdbcTemplate.query(sql,this.roleRowMapper,new Object[]{appUserId}); + return this.jdbcTemplate.query(sql, this.roleRowMapper, new Object[] { appUserId }); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformService.java index fb47dd71e..9d0a02f58 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformService.java @@ -15,4 +15,10 @@ public interface RoleWritePlatformService { CommandProcessingResult updateRole(Long roleId, JsonCommand command); CommandProcessingResult updateRolePermissions(Long roleId, JsonCommand command); + + CommandProcessingResult deleteRole(Long roleId); + + CommandProcessingResult disableRole(Long roleId); + + CommandProcessingResult enableRole(Long roleId); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformServiceJpaRepositoryImpl.java index 7a66aacc3..2adecb149 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/useradministration/service/RoleWritePlatformServiceJpaRepositoryImpl.java @@ -20,6 +20,7 @@ import org.mifosplatform.useradministration.domain.PermissionRepository; import org.mifosplatform.useradministration.domain.Role; import org.mifosplatform.useradministration.domain.RoleRepository; import org.mifosplatform.useradministration.exception.PermissionNotFoundException; +import org.mifosplatform.useradministration.exception.RoleAssociatedException; import org.mifosplatform.useradministration.exception.RoleNotFoundException; import org.mifosplatform.useradministration.serialization.PermissionsCommandFromApiJsonDeserializer; import org.slf4j.Logger; @@ -62,7 +63,6 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf this.roleCommandFromApiJsonDeserializer.validateForCreate(command.json()); final Role entity = Role.fromJson(command); - this.roleRepository.save(entity); return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(entity.getId()).build(); @@ -172,7 +172,91 @@ public class RoleWritePlatformServiceJpaRepositoryImpl implements RoleWritePlatf if (permission.hasCode(permissionCode)) { return permission; } } } - throw new PermissionNotFoundException(permissionCode); } + + /** + * Method for Delete Role + */ + @Transactional + @Override + public CommandProcessingResult deleteRole(Long roleId) { + + try { + /** + * Checking the role present in DB or not using role_id + */ + final Role role = this.roleRepository.findOne(roleId); + if (role == null) { throw new RoleNotFoundException(roleId); } + + /** + * Roles associated with users can't be deleted + */ + final Integer count = this.roleRepository.getCountOfRolesAssociatedWithUsers(roleId); + if (count > 0) { throw new RoleAssociatedException("error.msg.role.associated.with.users.deleted", roleId); } + + this.roleRepository.delete(role); + return new CommandProcessingResultBuilder().withEntityId(roleId).build(); + } catch (final DataIntegrityViolationException e) { + throw new PlatformDataIntegrityException("error.msg.unknown.data.integrity.issue", + "Unknown data integrity issue with resource: " + e.getMostSpecificCause()); + } + } + + /** + * Method for disabling the role + */ + @Transactional + @Override + public CommandProcessingResult disableRole(Long roleId) { + try { + /** + * Checking the role present in DB or not using role_id + */ + final Role role = this.roleRepository.findOne(roleId); + if (role == null) { throw new RoleNotFoundException(roleId); } + //if(role.isDisabled()){throw new RoleNotFoundException(roleId);} + + /** + * Roles associated with users can't be disable + */ + final Integer count = this.roleRepository.getCountOfRolesAssociatedWithUsers(roleId); + if (count > 0) { throw new RoleAssociatedException("error.msg.role.associated.with.users.disabled", roleId); } + + /** + * Disabling the role + */ + role.disableRole(); + this.roleRepository.save(role); + return new CommandProcessingResultBuilder().withEntityId(roleId).build(); + + } catch (final DataIntegrityViolationException e) { + throw new PlatformDataIntegrityException("error.msg.unknown.data.integrity.issue", + "Unknown data integrity issue with resource: " + e.getMostSpecificCause()); + } + } + + /** + * Method for Enabling the role + */ + @Transactional + @Override + public CommandProcessingResult enableRole(Long roleId) { + try { + /** + * Checking the role present in DB or not using role_id + */ + final Role role = this.roleRepository.findOne(roleId); + if (role == null) { throw new RoleNotFoundException(roleId); } + //if(!role.isEnabled()){throw new RoleNotFoundException(roleId);} + + role.enableRole(); + this.roleRepository.save(role); + return new CommandProcessingResultBuilder().withEntityId(roleId).build(); + + } catch (final DataIntegrityViolationException e) { + throw new PlatformDataIntegrityException("error.msg.unknown.data.integrity.issue", + "Unknown data integrity issue with resource: " + e.getMostSpecificCause()); + } + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/resources/sql/migrations/core_db/V230__role_status_and_correspoding_permissions.sql b/mifosng-provider/src/main/resources/sql/migrations/core_db/V230__role_status_and_correspoding_permissions.sql new file mode 100644 index 000000000..bc574437f --- /dev/null +++ b/mifosng-provider/src/main/resources/sql/migrations/core_db/V230__role_status_and_correspoding_permissions.sql @@ -0,0 +1,5 @@ +ALTER TABLE `m_role` ADD COLUMN `is_disabled` BIT NULL DEFAULT b'0' AFTER `description`; +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('authorisation', 'DISABLE_ROLE', 'ROLE', 'DISABLE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('authorisation', 'DISABLE_ROLE_CHECKER', 'ROLE', 'DISABLE_CHECKER', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('authorisation', 'ENABLE_ROLE', 'ROLE', 'ENABLE', 0); +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('authorisation', 'ENABLE_ROLE_CHECKER', 'ROLE', 'ENABLE_CHECKER', 0); \ No newline at end of file