diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java index cf926a70a75..de4e3a361b7 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserStoreManager.java @@ -8402,22 +8402,43 @@ public List doGetRoleListOfUserWithID(String userID, String filter) thro return (List) object; } + List roleList; String username = getUserNameFromUserID(userID); if (username != null) { String[] roleListOfUserFromCache = getRoleListOfUserFromCache(this.tenantId, username); if (roleListOfUserFromCache != null) { - List roleList = Arrays.asList(roleListOfUserFromCache); + roleList = Arrays.asList(roleListOfUserFromCache); if (!roleList.isEmpty()) { return roleList; } } } - return getUserRolesWithID(userID, filter); + synchronized (userID.intern()) { + if (username != null) { + String[] roleListOfUserFromCache = getRoleListOfUserFromCache(this.tenantId, username); + if (roleListOfUserFromCache != null) { + roleList = Arrays.asList(roleListOfUserFromCache); + if (!roleList.isEmpty()) { + return roleList; + } + } + } + String[] roleListOfUserFromDatabase = getUserRolesWithIDFromDatabase(userID, filter); + roleList = Arrays.asList(roleListOfUserFromDatabase); + // Add to user role cache using username. + if (username != null) { + addToUserRolesCache(this.tenantId, username, roleListOfUserFromDatabase); + } + } + return roleList; } - private List getUserRolesWithID(String userID, String filter) throws UserStoreException { + private String[] getUserRolesWithIDFromDatabase(String userID, String filter) throws UserStoreException { + if (log.isDebugEnabled()) { + log.debug("Retrieving user role list for userID: " + userID + " from database"); + } List internalRoles = doGetInternalRoleListOfUserWithID(userID, filter); Set modifiedInternalRoles = new HashSet<>(); String[] modifiedExternalRoleList = new String[0]; @@ -8452,13 +8473,7 @@ private List getUserRolesWithID(String userID, String filter) throws Use } } } - - // Add to user role cache uisng username. - String username = getUserNameFromUserID(userID); - if (username != null) { - addToUserRolesCache(this.tenantId, username, roleList); - } - return Arrays.asList(roleList); + return roleList; } /** @@ -8482,11 +8497,23 @@ public final String[] doGetRoleListOfUser(String userName, String filter) throws return roleList; } - return getUserRoles(userName, filter); + String usernameWithTenantDomain = userName + "@" + this.getTenantDomain(this.tenantId); + synchronized (usernameWithTenantDomain.intern()) { + roleList = getRoleListOfUserFromCache(this.tenantId, userName); + if (roleList != null && roleList.length > 0) { + return roleList; + } + roleList = getUserRolesFromDatabase(userName, filter); + addToUserRolesCache(this.tenantId, userName, roleList); + } + return roleList; } - private String[] getUserRoles(String username, String filter) throws UserStoreException { + private String[] getUserRolesFromDatabase(String username, String filter) throws UserStoreException { + if (log.isDebugEnabled()) { + log.debug("Retrieving user role list for user: " + username + " from database"); + } String[] internalRoles = doGetInternalRoleListOfUser(username, filter); String[] modifiedExternalRoleList = new String[0]; @@ -8520,7 +8547,6 @@ private String[] getUserRoles(String username, String filter) throws UserStoreEx } } } - addToUserRolesCache(this.tenantId, username, roleList); return roleList; } @@ -8547,9 +8573,9 @@ public final String[] getRoleListOfUserFromDatabase(String username, String filt // According to implementation, getRoleListOfUser method would return everyone role name for all users. return new String[]{realmConfig.getEveryOneRoleName()}; } - return getUserRolesWithID(userID, filter).toArray(new String[0]); + return getUserRolesWithIDFromDatabase(userID, filter); } else { - return getUserRoles(username, filter); + return getUserRolesFromDatabase(username, filter); } }