-
Notifications
You must be signed in to change notification settings - Fork 549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve applications list performance #4348
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import org.wso2.carbon.context.PrivilegedCarbonContext; | ||
import org.wso2.carbon.context.RegistryType; | ||
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; | ||
import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo; | ||
import org.wso2.carbon.identity.application.common.model.ApplicationPermission; | ||
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig; | ||
import org.wso2.carbon.identity.application.common.model.PermissionsAndRoleConfig; | ||
|
@@ -64,10 +65,12 @@ | |
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.xml.bind.JAXBContext; | ||
|
@@ -150,6 +153,96 @@ public static boolean isUserAuthorized(String applicationName, String username, | |
return true; | ||
} | ||
|
||
|
||
/** | ||
* @param applicationInfos | ||
* @param username | ||
* @return a filtered list of ApplicationBasicInfo | ||
* @throws IdentityApplicationManagementException | ||
*/ | ||
public static ArrayList<ApplicationBasicInfo> filterApplicationsForUser( | ||
ApplicationBasicInfo[] applicationInfos, String username | ||
) | ||
throws IdentityApplicationManagementException { | ||
|
||
// Initialize list to return | ||
ArrayList<ApplicationBasicInfo> authorizedAppInfo = new ArrayList<ApplicationBasicInfo>(); | ||
|
||
// Check whether roles validation is enabled | ||
// If we do not validate the roles, return the whole list of applications | ||
boolean validateRoles = validateRoles(); | ||
if (!validateRoles) { | ||
if (log.isDebugEnabled()) { | ||
log.debug(String.format("Validating user with application roles is disabled. Therefore, " + | ||
"user: %s will be authorized for all applications", username)); | ||
} | ||
|
||
// return new ArrayList<ApplicationBasicInfo>(applicationInfos); | ||
return new ArrayList<ApplicationBasicInfo>( | ||
(List<ApplicationBasicInfo>) Arrays.asList(applicationInfos)); | ||
|
||
} | ||
|
||
// Get user store | ||
try { | ||
UserStoreManager userStoreManager = CarbonContext.getThreadLocalCarbonContext().getUserRealm() | ||
.getUserStoreManager(); | ||
|
||
// List roles from user store | ||
String[] userRoles = userStoreManager.getRoleListOfUser(username); | ||
|
||
// If the user store is an implementation of the AbstractUserStoreManager, | ||
// Get the role lists using its methods | ||
log.debug("User roles" + Arrays.toString(userRoles)); | ||
if (userStoreManager instanceof AbstractUserStoreManager) { | ||
try { | ||
|
||
String[] userRolesAbstractUSM = (((AbstractUserStoreManager) userStoreManager) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why it is required to cast and invoke the |
||
.getRoleListOfUser(username)); | ||
|
||
// Merge the lists | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the above comment is valid, there is no point of merging. Also these operations will be costly when there large number of roles assigned for the user. |
||
Set<String> userRolesList = new HashSet(Arrays.asList(userRoles)); | ||
userRolesList.addAll(Arrays.asList(userRolesAbstractUSM)); | ||
userRoles = userRolesList.toArray(String[]::new); | ||
|
||
log.debug("AbstractUserStoreManager roles" + Arrays.toString(userRolesAbstractUSM)); | ||
|
||
|
||
} catch (UserStoreException e) { | ||
throw new IdentityApplicationManagementException( | ||
"Error while getting roles for user" + username, e); | ||
} | ||
} | ||
|
||
// For each app, check whether the user the corresponding application role | ||
for (ApplicationBasicInfo applicationBasicInfo : applicationInfos) { | ||
|
||
String applicationName = applicationBasicInfo.getApplicationName(); | ||
|
||
String applicationRoleName = getAppRoleName(applicationName); | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug( | ||
"Checking whether user has role : " + applicationRoleName | ||
+ " by retrieving role list of " + "user : " + username); | ||
} | ||
|
||
for (String userRole : userRoles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can improve the code using Java streams APIs |
||
if (applicationRoleName.equals(userRole)) { | ||
authorizedAppInfo.add(applicationBasicInfo); | ||
} | ||
} | ||
|
||
} | ||
|
||
} catch (UserStoreException e) { | ||
throw new IdentityApplicationManagementException("Error getting roles for user: " + | ||
username, e); | ||
} | ||
return authorizedAppInfo; | ||
} | ||
|
||
|
||
/** | ||
* @param applicationName | ||
* @param username | ||
|
@@ -296,7 +389,7 @@ private static String getAppRoleName(String applicationName) { | |
} | ||
|
||
/** | ||
* Delete the role of the app | ||
* Delete the role of the app. | ||
* | ||
* @param applicationName | ||
* @throws IdentityApplicationManagementException | ||
|
@@ -468,7 +561,7 @@ public static void storePermissions(String applicationName, String username, | |
} | ||
|
||
/** | ||
* Updates the permissions of the application | ||
* Updates the permissions of the application. | ||
* | ||
* @param applicationName | ||
* @param permissions | ||
|
@@ -542,7 +635,7 @@ private static void addPermission(String applicationNode, ApplicationPermission[ | |
} | ||
|
||
/** | ||
* Loads the permissions of the application | ||
* Loads the permissions of the application. | ||
* | ||
* @param applicationName | ||
* @return | ||
|
@@ -620,7 +713,7 @@ private static void permissionPath(Registry tenantGovReg, String permissionPath, | |
} | ||
|
||
/** | ||
* Delete the resource | ||
* Delete the resource. | ||
* | ||
* @param applicationName | ||
* @throws IdentityApplicationManagementException | ||
|
@@ -690,7 +783,7 @@ public static String getApplicationPermissionPath() { | |
} | ||
|
||
/** | ||
* Validate application name according to the regex | ||
* Validate application name according to the regex. | ||
* | ||
* @return validated or not | ||
*/ | ||
|
@@ -716,7 +809,7 @@ public static String getSPValidatorRegex() { | |
} | ||
|
||
/** | ||
* Get Property values | ||
* Get Property values. | ||
* | ||
* @param tenantDomain Tenant domain | ||
* @param spIssuer SP Issuer | ||
|
@@ -811,7 +904,7 @@ public static boolean isValidApplicationOwner(ServiceProvider serviceProvider) | |
} | ||
|
||
/** | ||
* Get Service provider name from XML configuration file | ||
* Get Service provider name from XML configuration file. | ||
* | ||
* @param spFileStream | ||
* @param tenantDomain | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling another method from this method without any additional steps doesn't seem fine. Either the new method could have been call from where the
getAuthorizedApplicationBasicInfo
method was called.