From 1ff86289fdd496f1888bfa018abc11f6da53420b Mon Sep 17 00:00:00 2001 From: Megala Date: Mon, 17 Oct 2016 15:47:32 +0530 Subject: [PATCH] Fixing the problems that happend when we have special chracters in roles --- .../service/impl/RoleManagementServiceImpl.java | 13 ++++++++++--- .../cdmf.page.role.create/public/js/bottomJs.js | 7 ++++--- .../pages/cdmf.page.roles/public/js/role-listing.js | 2 +- .../app/pages/cdmf.page.users/public/js/listing.js | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java index 2f33d5a5df..29db06c3d0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -39,8 +39,10 @@ import org.wso2.carbon.user.mgt.common.UserAdminException; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -231,9 +233,9 @@ public class RoleManagementServiceImpl implements RoleManagementService { userStoreManager.addRole(roleInfo.getRoleName(), roleInfo.getUsers(), permissions); //TODO fix what's returned in the entity - return Response.created(new URI(API_BASE_PATH + "/" + roleInfo.getRoleName())).entity( - "Role '" + roleInfo.getRoleName() + "' has " + - "successfully been added").build(); + return Response.created(new URI(API_BASE_PATH + "/" + URLEncoder.encode(roleInfo.getRoleName(), "UTF-8"))). + entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been" + + " added").build(); } catch (UserStoreException e) { String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'"; log.error(msg, e); @@ -245,6 +247,11 @@ public class RoleManagementServiceImpl implements RoleManagementService { log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (UnsupportedEncodingException e) { + String msg = "Error occurred while encoding role name"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.role.create/public/js/bottomJs.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.role.create/public/js/bottomJs.js index d67c46ea8b..2cb8292af7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.role.create/public/js/bottomJs.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.role.create/public/js/bottomJs.js @@ -83,8 +83,8 @@ clearInline["role-name"] = function () { */ validateInline["role-name"] = function () { var roleNameInput = $("input#roleName"); - if (inputIsValid( roleNameInput.data("regex"), roleNameInput.val()) && roleNameInput.indexOf("@") < 0 && - roleNameInput.indexOf("/") < 0) { + var roleName = roleNameInput.val(); + if (inputIsValid( roleNameInput.data("regex"), roleName) && roleName.indexOf("@") < 0 && roleName.indexOf("/") < 0) { disableInlineError("roleNameField", "roleNameEmpty", "roleNameError"); } else { enableInlineError("roleNameField", "roleNameEmpty", "roleNameError"); @@ -173,7 +173,8 @@ $(document).ready(function () { if (!roleName) { $(errorMsg).text("Role name is a required field. It cannot be empty."); $(errorMsgWrapper).removeClass("hidden"); - } else if (!inputIsValid(roleNameInput.data("regex"), roleName)) { + } else if (!inputIsValid(roleNameInput.data("regex"), roleName) || roleName.indexOf("@") >= 0 || + roleName.indexOf("/") >= 0) { $(errorMsg).text(roleNameInput.data("error-msg")); $(errorMsgWrapper).removeClass("hidden"); } else if (!domain) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.roles/public/js/role-listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.roles/public/js/role-listing.js index 907d124f70..4a6b44fbb8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.roles/public/js/role-listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.roles/public/js/role-listing.js @@ -220,7 +220,7 @@ $("#role-grid").on("click", ".remove-role-link", function () { if (userStore) { role = userStore + '/' + role; } - $('[id=role-' + role + ']').remove(); + $('[id="role-' + role + '"]').remove(); $(modalPopupContent).html($('#remove-role-success-content').html()); $("a#remove-role-success-link").click(function () { hidePopup(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.users/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.users/public/js/listing.js index b737f4472c..af9792e18a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.users/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.users/public/js/listing.js @@ -220,7 +220,7 @@ function removeUser(username) { if (domain) { username = domain + '/' + username; } - $('[id=user-' + username + ']').remove(); + $('[id="user-' + username + '"]').remove(); // update modal-content with success message modalDialog.header("User Removed."); modalDialog.content("Done. User was successfully removed.");