From 86f78f0a86faee92e8d285d8dcadbbe15ae063e6 Mon Sep 17 00:00:00 2001 From: pasindu Date: Thu, 1 Feb 2024 20:01:48 +0530 Subject: [PATCH] Fix Role Management Service loger issues --- .../impl/RoleManagementServiceImpl.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java index a568035d4b..36258afa62 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -427,6 +427,16 @@ public class RoleManagementServiceImpl implements RoleManagementService { public Response addRole(RoleInfo roleInfo) { RequestValidationUtil.validateRoleDetails(roleInfo); RequestValidationUtil.validateRoleName(roleInfo.getRoleName()); + + String role; + String[] roles = roleInfo.getRoleName().split("/"); + + if (roles.length > 1) { + role = roleInfo.getRoleName().split("/")[1]; + } else { + role = roleInfo.getRoleName().split("/")[0]; + } + try { String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); @@ -436,6 +446,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { log.debug("Persisting the role in the underlying user store"); } + Permission[] permissions = null; if (roleInfo.getPermissions() != null && roleInfo.getPermissions().length > 0) { permissions = new Permission[roleInfo.getPermissions().length]; @@ -458,11 +469,11 @@ public class RoleManagementServiceImpl implements RoleManagementService { } String stringUsers = new Gson().toJson(roleInfo.getUsers()); log.info( - "Role " + roleInfo.getRoleName().split("/")[1] + " created", + "Role " + role + " created", roleMgtContextBuilder .setActionTag("ADD_ROLE") .setUserStoreDomain(roleInfo.getRoleName().split("/")[0]) - .setRoleName(roleInfo.getRoleName().split("/")[1]) + .setRoleName(role) .setUsers(stringUsers) .setTenantID(tenantId) .setTenantDomain(tenantDomain) @@ -481,8 +492,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { errorCode = e.getMessage().split("-")[0].trim(); } if (ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode().equals(errorCode)) { - String roleName = roleInfo.getRoleName().split("/")[1]; - String msg = "Role already exists with name : " + roleName + ". Try with another role name."; + String msg = "Role already exists with name : " + role + ". Try with another role name."; log.warn(msg); return Response.status(Response.Status.CONFLICT).entity(msg).build(); } else { @@ -609,12 +619,22 @@ public class RoleManagementServiceImpl implements RoleManagementService { updatePermissions(roleDetails[roleDetails.length - 1], roleInfo, userRealm); } String stringUsers = new Gson().toJson(roleInfo.getUsers()); + + String role; + String[] roles = roleInfo.getRoleName().split("/"); + + if (roles.length > 1) { + role = roleInfo.getRoleName().split("/")[1]; + } else { + role = roleInfo.getRoleName().split("/")[0]; + } + log.info( - "Role " + roleInfo.getRoleName().split("/")[1] + " updated", + "Role " + role + " updated", roleMgtContextBuilder .setActionTag("UPDATE_ROLE") .setUserStoreDomain(roleInfo.getRoleName().split("/")[0]) - .setRoleName(roleInfo.getRoleName().split("/")[1]) + .setRoleName(role) .setUsers(stringUsers) .setTenantID(tenantId) .setTenantDomain(tenantDomain) @@ -671,12 +691,21 @@ public class RoleManagementServiceImpl implements RoleManagementService { log.debug("Deleting the role in user store"); } DeviceMgtAPIUtils.getGroupManagementProviderService().deleteRoleAndRoleGroupMapping(roleName, roleToDelete, tenantId, userStoreManager, authorizationManager); + String role; + String[] roles = roleName.split("/"); + + if (roles.length > 1) { + role = roleName.split("/")[1]; + } else { + role = roleName.split("/")[0]; + } + log.info( - "Role " + roleName.split("/")[1] + " deleted", + "Role " + role + " deleted", roleMgtContextBuilder .setActionTag("DELETE_ROLE") .setUserStoreDomain(userStoreName) - .setRoleName(roleName.split("/")[1]) + .setRoleName(role) .setTenantID(String.valueOf(tenantId)) .setTenantDomain(tenantDomain) .setUserName(userName) -- 2.36.3