diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserOperationEventListener.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserOperationEventListener.java index 71825da23ff..5cf8efbae6b 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserOperationEventListener.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/common/AbstractUserOperationEventListener.java @@ -400,6 +400,14 @@ public boolean doPreGetUserListWithID(Condition condition, String domain, String return true; } + @Override + public boolean doPreGetUserListWithID(Condition condition, String domain, String profileName, int limit, + String cursor, String direction, String sortBy, String sortOrder, + UserStoreManager userStoreManager) throws UserStoreException { + + return true; + } + @Override public boolean doPreGetUserListWithID(String claimUri, String claimValue, int limit, int offset, final List returnUsersList, UserStoreManager userStoreManager) throws UserStoreException { 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 6e181919af5..185523eab58 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 @@ -86,7 +86,18 @@ import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.Comparator; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -94,8 +105,14 @@ import javax.sql.DataSource; -import static org.wso2.carbon.user.core.UserCoreConstants.*; import static org.wso2.carbon.user.core.UserCoreConstants.ClaimTypeURIs.IDENTITY_CLAIM_URI; +import static org.wso2.carbon.user.core.UserCoreConstants.DOMAIN_SEPARATOR; +import static org.wso2.carbon.user.core.UserCoreConstants.INTERNAL_DOMAIN; +import static org.wso2.carbon.user.core.UserCoreConstants.INTERNAL_SYSTEM_ROLE_PREFIX; +import static org.wso2.carbon.user.core.UserCoreConstants.INTERNAL_ROLES_CLAIM; +import static org.wso2.carbon.user.core.UserCoreConstants.ROLE_CLAIM; +import static org.wso2.carbon.user.core.UserCoreConstants.SYSTEM_DOMAIN_NAME; +import static org.wso2.carbon.user.core.UserCoreConstants.USER_STORE_GROUPS_CLAIM; import static org.wso2.carbon.user.core.UserStoreConfigConstants.RESOLVE_GROUP_NAME_FROM_USER_ID_CACHE_NAME; import static org.wso2.carbon.user.core.UserStoreConfigConstants.RESOLVE_USER_ID_FROM_USER_NAME_CACHE_NAME; import static org.wso2.carbon.user.core.UserStoreConfigConstants.RESOLVE_USER_NAME_FROM_UNIQUE_USER_ID_CACHE_NAME; @@ -2662,23 +2679,40 @@ private void handlePreGetUserList(Condition condition, String domain, String pro } private void handlePreGetUserListWithID(Condition condition, String domain, String profileName, int limit, - int offset, - String sortBy, String sortOrder) throws UserStoreException { + Integer offset, String cursor, String direction, String sortBy, String sortOrder) + throws UserStoreException { try { for (UserOperationEventListener listener : UMListenerServiceComponent .getUserOperationEventListeners()) { if (listener instanceof AbstractUserOperationEventListener) { AbstractUserOperationEventListener newListener = (AbstractUserOperationEventListener) listener; - if (!newListener.doPreGetUserListWithID(condition, domain, profileName, limit, offset, sortBy, - sortOrder, this)) { - - handleGetUserListFailure(ErrorMessages.ERROR_CODE_ERROR_DURING_PRE_GET__CONDITIONAL_USER_LIST.getCode(), - String.format(ErrorMessages.ERROR_CODE_ERROR_DURING_PRE_GET__CONDITIONAL_USER_LIST.getMessage(), - UserCoreErrorConstants.PRE_LISTENER_TASKS_FAILED_MESSAGE), condition, domain, - profileName, limit, offset, sortBy, sortOrder); - break; + if (cursor == null) { + if (!newListener.doPreGetUserListWithID(condition, domain, profileName, limit, offset, sortBy, + sortOrder, this)) { + + handleGetUserListFailure(ErrorMessages. + ERROR_CODE_ERROR_DURING_PRE_GET__CONDITIONAL_USER_LIST.getCode(), + String.format(ErrorMessages. + ERROR_CODE_ERROR_DURING_PRE_GET__CONDITIONAL_USER_LIST.getMessage(), + UserCoreErrorConstants.PRE_LISTENER_TASKS_FAILED_MESSAGE), condition, + domain, profileName, limit, offset, sortBy, sortOrder); + break; + } + } else { + if (!newListener.doPreGetUserListWithID(condition, domain, profileName, limit, cursor, + direction, sortBy, sortOrder, this)) { + + handleGetUserListFailure(ErrorMessages. + ERROR_CODE_ERROR_DURING_PRE_GET__CONDITIONAL_USER_LIST.getCode(), + String.format(ErrorMessages. + ERROR_CODE_ERROR_DURING_PRE_GET__CONDITIONAL_USER_LIST.getMessage(), + UserCoreErrorConstants.PRE_LISTENER_TASKS_FAILED_MESSAGE), condition, + domain, profileName, limit, offset, sortBy, sortOrder); + break; + } } + } } } catch (UserStoreException ex) { @@ -15536,7 +15570,7 @@ public List getUserListWithID(Condition condition, String domain, String p profileName = UserCoreConstants.DEFAULT_PROFILE; } - handlePreGetUserListWithID(condition, domain, profileName, limit, offset, sortBy, sortOrder); + handlePreGetUserListWithID(condition, domain, profileName, limit, offset, cursor, direction, sortBy, sortOrder); if (log.isDebugEnabled()) { log.debug("Pre listener get conditional user list for domain: " + domain); } @@ -15680,7 +15714,7 @@ public List getUserListWithID(Condition condition, String domain, String p String cursor, String direction, String sortBy, String sortOrder) throws UserStoreException, NullPointerException { - // This method will flow down to use offset pagination, so the cursor will be null. + // This method will flow down to use cursor pagination, so the offset will be null. Integer offset = null; validateCondition(condition); @@ -15696,11 +15730,10 @@ public List getUserListWithID(Condition condition, String domain, String p profileName = UserCoreConstants.DEFAULT_PROFILE; } - if (offset != null) { - handlePreGetUserListWithID(condition, domain, profileName, limit, offset, sortBy, sortOrder); - if (log.isDebugEnabled()) { - log.debug("Pre listener get conditional user list for domain: " + domain); - } + handlePreGetUserListWithID(condition, domain, profileName, limit, offset, cursor, direction, sortBy, + sortOrder); + if (log.isDebugEnabled()) { + log.debug("Pre listener get conditional user list for domain: " + domain); } UserStoreManager secondaryUserStoreManager = getSecondaryUserStoreManager(domain); @@ -15727,8 +15760,8 @@ public List getUserListWithID(Condition condition, String domain, String p // Call the listeners to get the filtered users from relevant identity store. if (secondaryUserStoreManager != null) { - handlePreGetUserListWithIdentityClaims(duplicateCondition, domain, profileName, limit, offset, cursor, direction, - sortBy, sortOrder, secondaryUserStoreManager, identityClaimFilteredUserNames, + handlePreGetUserListWithIdentityClaims(duplicateCondition, domain, profileName, limit, offset, cursor, + direction, sortBy, sortOrder, secondaryUserStoreManager, identityClaimFilteredUserNames, hasNonIdentityClaimFilterConditions); } @@ -15751,10 +15784,8 @@ public List getUserListWithID(Condition condition, String domain, String p } for (String username : identityClaimFilteredUserNames) { - User user = new User(); - user.setUsername(username); - user.setUserID(getUserIDFromUserName(username)); - user.setUserStoreDomain(UserCoreUtil.extractDomainFromName(user.getUsername())); + User user = getUser(getUserIDFromUserName(username), username); + user.setUserStoreDomain(UserCoreUtil.extractDomainFromName(username)); identityClaimFilteredUsers.add(user); } @@ -15793,7 +15824,7 @@ public List getUserListWithID(Condition condition, String domain, String p if (UserCoreConstants.PREVIOUS.equals(direction)) { cursorCounter = tempFilteredUsers.get(0).getPreferredUsername(); } else { - cursorCounter = tempFilteredUsers.get(tempFilteredUsers.size()-1).getPreferredUsername(); + cursorCounter = tempFilteredUsers.get(tempFilteredUsers.size() - 1).getPreferredUsername(); } // Taking the interception of the user list. tempFilteredUsers.retainAll(identityClaimFilteredUsers); @@ -15877,7 +15908,7 @@ private List getFilteredUsers(Condition condition, String profileName, int addUsersToUserIdCache(users.getUsers()); addUsersToUserNameCache(users.getUsers()); filteredUsers = users.getUsers(); - } else { + } else { //TODO - Handle situation where NOT uniqueIDEnabled PaginatedSearchResult users = ((AbstractUserStoreManager) secManager) .doGetUserList(condition, profileName, limit, offset, sortBy, sortOrder); filteredUsers = userUniqueIDManger.listUsers(users.getUsers(), this); diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java index 817d4a51e37..128d9356601 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java @@ -26,7 +26,12 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.user.api.Property; import org.wso2.carbon.user.api.RealmConfiguration; -import org.wso2.carbon.user.core.*; +import org.wso2.carbon.user.core.NotImplementedException; +import org.wso2.carbon.user.core.UserCoreConstants; +import org.wso2.carbon.user.core.UserRealm; +import org.wso2.carbon.user.core.UserStoreClientException; +import org.wso2.carbon.user.core.UserStoreException; +import org.wso2.carbon.user.core.UserStoreManager; import org.wso2.carbon.user.core.claim.ClaimManager; import org.wso2.carbon.user.core.common.AuthenticationResult; import org.wso2.carbon.user.core.common.FailureReason; @@ -3321,19 +3326,13 @@ protected UniqueIDPaginatedSearchResult doGetUserListWithID(Condition condition, limit = initialOffset + 1; } - sqlBuilder = getQueryString(isGroupFiltering, isUsernameFiltering, isClaimFiltering, - expressionConditions, limit, offset, sortBy, sortOrder, profileName, type, - totalMultiGroupFilters, totalMultiClaimFilters); + sqlBuilder = getQueryStringCursor(isGroupFiltering, isUsernameFiltering, isClaimFiltering, + expressionConditions, limit, offset, null, null, sortBy, sortOrder, profileName, + type, totalMultiGroupFilters, totalMultiClaimFilters); } else { -// if (cursor.equals("") || direction.equals("next")) { - sqlBuilder = getQueryStringCursor(isGroupFiltering, isUsernameFiltering, isClaimFiltering, - expressionConditions, limit, cursor, direction, sortBy, sortOrder, profileName, type, - totalMultiGroupFilters, totalMultiClaimFilters); -// } else if (direction.equals("prev")) { -// sqlBuilder = getQueryStringCursorPrev(isGroupFiltering, isUsernameFiltering, isClaimFiltering, -// expressionConditions, limit, cursor, sortBy, sortOrder, profileName, type, -// totalMultiGroupFilters, totalMultiClaimFilters); -// } + sqlBuilder = getQueryStringCursor(isGroupFiltering, isUsernameFiltering, isClaimFiltering, + expressionConditions, limit, null, cursor, direction, sortBy, sortOrder, profileName, + type, totalMultiGroupFilters, totalMultiClaimFilters); } if ((MYSQL.equals(type) || MARIADB.equals(type)) && totalMultiGroupFilters > 1 @@ -3422,9 +3421,225 @@ private void populatePrepareStatement(SqlBuilder sqlBuilder, PreparedStatement p } } - protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsernameFiltering, boolean isClaimFiltering, - List expressionConditions, int limit, int offset, String sortBy, String sortOrder, - String profileName, String dbType, int totalMultiGroupFilters, int totalMultiClaimFilters) +// protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsernameFiltering, boolean isClaimFiltering, +// List expressionConditions, int limit, int offset, String sortBy, String sortOrder, +// String profileName, String dbType, int totalMultiGroupFilters, int totalMultiClaimFilters) +// throws UserStoreException { +// +// StringBuilder sqlStatement; +// SqlBuilder sqlBuilder; +// boolean hitGroupFilter = false; +// boolean hitClaimFilter = false; +// int groupFilterCount = 0; +// int claimFilterCount = 0; +// +// if (isGroupFiltering && isUsernameFiltering && isClaimFiltering || isGroupFiltering && isClaimFiltering) { +// +// if (DB2.equals(dbType)) { +// sqlStatement = new StringBuilder("SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT " +// + "ROW_NUMBER() OVER (ORDER BY UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME " +// + "FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN UM_USER U " +// + "ON UR.UM_USER_ID =U.UM_ID INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); +// } else if (MSSQL.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " +// + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT U.UM_USER_ID, " + +// "UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID " + +// "INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN UM_USER_ATTRIBUTE UA ON" + +// " U.UM_ID = UA.UM_USER_ID"); +// } else if (ORACLE.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT UM_USER_NAME, rownum AS rnum " +// + "FROM (SELECT UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR" +// + ".UM_ROLE_ID INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN " +// + "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); +// } else if (POSTGRE_SQL.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " + +// "UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN" + +// " UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN " + +// "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); +// } else { +// sqlStatement = new StringBuilder( +// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " +// + "UM_USER_ROLE UR INNER JOIN UM_USER U INNER JOIN UM_USER_ATTRIBUTE UA ON R.UM_ID = " +// + "UR.UM_ROLE_ID AND UR.UM_USER_ID =" + " U.UM_ID AND U.UM_ID = UA.UM_USER_ID"); +// } +// sqlBuilder = new SqlBuilder(sqlStatement).where("R.UM_TENANT_ID = ?", tenantId) +// .where("U.UM_TENANT_ID = ?", tenantId).where("UR.UM_TENANT_ID = ?", tenantId) +// .where("UA.UM_TENANT_ID = ?", tenantId).where("UA.UM_PROFILE_ID = ?", profileName); +// } else if (isGroupFiltering && isUsernameFiltering || isGroupFiltering) { +// if (DB2.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " +// + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME FROM UM_ROLE R INNER" +// + " JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN UM_USER U ON UR" +// + ".UM_USER_ID " +// + "=U.UM_ID "); +// } else if (MSSQL.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " +// + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT U.UM_USER_ID, " + +// "UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID " + +// "INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID"); +// } else if (ORACLE.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT UM_USER_NAME, rownum AS rnum " +// + "FROM (SELECT UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR" +// + ".UM_ROLE_ID INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID"); +// } else if (POSTGRE_SQL.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " + +// "UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN " + +// "UM_USER U ON UR.UM_USER_ID=U.UM_ID"); +// } else { +// sqlStatement = new StringBuilder( +// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " +// + "UM_USER_ROLE UR INNER JOIN UM_USER U ON R.UM_ID = UR.UM_ROLE_ID AND UR.UM_USER_ID " +// + "=U.UM_ID"); +// } +// +// sqlBuilder = new SqlBuilder(sqlStatement).where("R.UM_TENANT_ID = ?", tenantId) +// .where("U.UM_TENANT_ID = ?", tenantId).where("UR.UM_TENANT_ID = ?", tenantId); +// } else if (isUsernameFiltering && isClaimFiltering || isClaimFiltering) { +// if (DB2.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " +// + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME FROM UM_USER U " +// + "INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); +// } else if (MSSQL.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " + +// "(ORDER BY UM_USER_NAME) AS RowNum FROM " + +// "(SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U " + +// "INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); +// } else if (ORACLE.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum FROM " +// + "(SELECT U.UM_USER_ID, UM_USER_NAME FROM UM_USER U INNER JOIN UM_USER_ATTRIBUTE UA " +// + "ON U.UM_ID = UA.UM_USER_ID"); +// } else { +// sqlStatement = new StringBuilder( +// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U INNER JOIN " +// + "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); +// } +// sqlBuilder = new SqlBuilder(sqlStatement).where("U.UM_TENANT_ID = ?", tenantId) +// .where("UA.UM_TENANT_ID = ?", tenantId).where("UA.UM_PROFILE_ID = ?", profileName); +// } else if (isUsernameFiltering) { +// if (DB2.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " +// + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_ID, UM_USER_NAME FROM " + +// "UM_USER U"); +// } else if (MSSQL.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " +// + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT UM_USER_NAME, UM_USER_ID" + +// " FROM UM_USER U"); +// } else if (ORACLE.equals(dbType)) { +// sqlStatement = new StringBuilder( +// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum " +// + "FROM (SELECT UM_USER_ID, UM_USER_NAME FROM UM_USER U"); +// } else { +// sqlStatement = new StringBuilder("SELECT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U"); +// } +// +// sqlBuilder = new SqlBuilder(sqlStatement).where("U.UM_TENANT_ID = ?", tenantId); +// } else { +// throw new UserStoreException("Condition is not valid."); +// } +// +// SqlBuilder header = new SqlBuilder(new StringBuilder(sqlBuilder.getSql())); +// addingWheres(sqlBuilder, header); +// +// for (ExpressionCondition expressionCondition : expressionConditions) { +// if (ExpressionAttribute.ROLE.toString().equals(expressionCondition.getAttributeName())) { +// if (!(MYSQL.equals(dbType) || MARIADB.equals(dbType)) || totalMultiGroupFilters > 1 +// && totalMultiClaimFilters > 1) { +// multiGroupQueryBuilder(sqlBuilder, header, hitGroupFilter, expressionCondition); +// hitGroupFilter = true; +// } else { +// multiGroupMySqlQueryBuilder(sqlBuilder, groupFilterCount, expressionCondition); +// groupFilterCount++; +// } +// } else if (ExpressionOperation.EQ.toString().equals(expressionCondition.getOperation()) +// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { +// if (isCaseSensitiveUsername()) { +// sqlBuilder.where("U.UM_USER_NAME = ?", expressionCondition.getAttributeValue()); +// } else { +// sqlBuilder.where("U.UM_USER_NAME = LOWER(?)", expressionCondition.getAttributeValue()); +// } +// } else if (ExpressionOperation.CO.toString().equals(expressionCondition.getOperation()) +// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { +// if (isCaseSensitiveUsername()) { +// sqlBuilder.where("U.UM_USER_NAME LIKE ?", "%" + expressionCondition.getAttributeValue() + "%"); +// } else { +// sqlBuilder +// .where("U.UM_USER_NAME LIKE LOWER(?)", "%" + expressionCondition.getAttributeValue() + "%"); +// } +// } else if (ExpressionOperation.EW.toString().equals(expressionCondition.getOperation()) +// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { +// if (isCaseSensitiveUsername()) { +// sqlBuilder.where("U.UM_USER_NAME LIKE ?", "%" + expressionCondition.getAttributeValue()); +// } else { +// sqlBuilder.where("U.UM_USER_NAME LIKE LOWER(?)", "%" + expressionCondition.getAttributeValue()); +// } +// } else if (ExpressionOperation.SW.toString().equals(expressionCondition.getOperation()) +// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { +// if (isCaseSensitiveUsername()) { +// sqlBuilder.where("U.UM_USER_NAME LIKE ?", expressionCondition.getAttributeValue() + "%"); +// } else { +// sqlBuilder.where("U.UM_USER_NAME LIKE LOWER(?)", expressionCondition.getAttributeValue() + "%"); +// } +// } else { +// // Claim filtering +// if (!(MYSQL.equals(dbType) || MARIADB.equals(dbType)) || totalMultiGroupFilters > 1 +// && totalMultiClaimFilters > 1) { +// multiClaimQueryBuilder(sqlBuilder, header, hitClaimFilter, expressionCondition); +// hitClaimFilter = true; +// } else { +// multiClaimMySqlQueryBuilder(sqlBuilder, claimFilterCount, expressionCondition); +// claimFilterCount++; +// } +// } +// } +// +// if (MYSQL.equals(dbType) || MARIADB.equals(dbType)) { +// sqlBuilder.updateSql(" GROUP BY U.UM_USER_NAME, U.UM_USER_ID "); +// if (groupFilterCount > 0 && claimFilterCount > 0) { +// sqlBuilder.updateSql(" HAVING (COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount + +// " AND COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount + ")"); +// } else if (groupFilterCount > 0) { +// sqlBuilder.updateSql(" HAVING COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount); +// } else if (claimFilterCount > 0) { +// sqlBuilder.updateSql(" HAVING COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount); +// } +// } +// +// if (!((MYSQL.equals(dbType) || MARIADB.equals(dbType)) && totalMultiGroupFilters > 1 +// && totalMultiClaimFilters > 1)) { +// if (DB2.equals(dbType)) { +// sqlBuilder.setTail(") AS p) WHERE rn BETWEEN ? AND ?", limit, offset); +// } else if (MSSQL.equals(dbType)) { +// if (isClaimFiltering && !isGroupFiltering && totalMultiClaimFilters > 1) { +// // Handle multi attribute filtering without group filtering. +// sqlBuilder.setTail(") AS Q) AS S) AS R) AS P WHERE P.RowNum BETWEEN ? AND ?", limit, offset); +// } else { +// sqlBuilder.setTail(") AS R) AS P WHERE P.RowNum BETWEEN ? AND ?", limit, offset); +// } +// } else if (ORACLE.equals(dbType)) { +// sqlBuilder.setTail(" ORDER BY UM_USER_NAME) where rownum <= ?) WHERE rnum > ?", limit, offset); +// } else { +// sqlBuilder.setTail(" ORDER BY UM_USER_NAME ASC LIMIT ? OFFSET ?", limit, offset); +// } +// } +// return sqlBuilder; +// } + + protected SqlBuilder getQueryStringCursor(boolean isGroupFiltering, boolean isUsernameFiltering, + boolean isClaimFiltering, List expressionConditions, int limit, + Integer offset, String cursor, String direction, String sortBy, String sortOrder, + String profileName, String dbType, int totalMultiGroupFilters, int totalMultiClaimFilters) throws UserStoreException { StringBuilder sqlStatement; @@ -3550,6 +3765,9 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername throw new UserStoreException("Condition is not valid."); } + if (cursor != null && UserCoreConstants.PREVIOUS.equals(direction)) { + sqlBuilder.prependSql("WITH this_set AS("); + } SqlBuilder header = new SqlBuilder(new StringBuilder(sqlBuilder.getSql())); addingWheres(sqlBuilder, header); @@ -3605,6 +3823,14 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername } } + if (cursor != null) { + if (UserCoreConstants.PREVIOUS.equals(direction)) { + sqlBuilder.where("U.UM_USER_NAME < ?", cursor); + } else if (UserCoreConstants.NEXT.equals(direction)) { + sqlBuilder.where("U.UM_USER_NAME > ?", cursor); + } + } + if (MYSQL.equals(dbType) || MARIADB.equals(dbType)) { sqlBuilder.updateSql(" GROUP BY U.UM_USER_NAME, U.UM_USER_ID "); if (groupFilterCount > 0 && claimFilterCount > 0) { @@ -3613,498 +3839,55 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername } else if (groupFilterCount > 0) { sqlBuilder.updateSql(" HAVING COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount); } else if (claimFilterCount > 0) { - sqlBuilder.updateSql(" HAVING COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount); + sqlBuilder.updateSql(" HAVING COUNT(DISTINCT UA.UM_ATTR_NAME) = " + claimFilterCount); } } - if (!((MYSQL.equals(dbType) || MARIADB.equals(dbType)) && totalMultiGroupFilters > 1 - && totalMultiClaimFilters > 1)) { - if (DB2.equals(dbType)) { - sqlBuilder.setTail(") AS p) WHERE rn BETWEEN ? AND ?", limit, offset); - } else if (MSSQL.equals(dbType)) { - if (isClaimFiltering && !isGroupFiltering && totalMultiClaimFilters > 1) { - // Handle multi attribute filtering without group filtering. - sqlBuilder.setTail(") AS Q) AS S) AS R) AS P WHERE P.RowNum BETWEEN ? AND ?", limit, offset); + if (cursor != null) { + if (!((MYSQL.equals(dbType) || MARIADB.equals(dbType)) && totalMultiGroupFilters > 1 + && totalMultiClaimFilters > 1)) { + if (DB2.equals(dbType)) { + sqlBuilder.setTail(") AS p) WHERE rn BETWEEN ?", limit); + } else if (MSSQL.equals(dbType)) { + if (isClaimFiltering && !isGroupFiltering && totalMultiClaimFilters > 1) { + // Handle multi attribute filtering without group filtering. + sqlBuilder.setTail(") AS Q) AS S) AS R) AS P WHERE P.RowNum BETWEEN ?", limit); + } else { + sqlBuilder.setTail(") AS R) AS P WHERE P.RowNum BETWEEN ?", limit); + } + } else if (ORACLE.equals(dbType)) { + sqlBuilder.setTail(" ORDER BY UM_USER_NAME) where rownum <= ?)", limit); } else { - sqlBuilder.setTail(") AS R) AS P WHERE P.RowNum BETWEEN ? AND ?", limit, offset); + if (UserCoreConstants.PREVIOUS.equals(direction)) { + sqlBuilder.setTail(" ORDER BY UM_USER_NAME DESC LIMIT ? )SELECT * " + + "from this_set ORDER BY UM_USER_NAME ASC;", limit); + } else if (UserCoreConstants.NEXT.equals(direction)) { + sqlBuilder.setTail(" ORDER BY UM_USER_NAME ASC LIMIT ?", limit); + } + } + } + } else { + if (!((MYSQL.equals(dbType) || MARIADB.equals(dbType)) && totalMultiGroupFilters > 1 + && totalMultiClaimFilters > 1)) { + if (DB2.equals(dbType)) { + sqlBuilder.setTail(") AS p) WHERE rn BETWEEN ? AND ?", limit, offset); + } else if (MSSQL.equals(dbType)) { + if (isClaimFiltering && !isGroupFiltering && totalMultiClaimFilters > 1) { + // Handle multi attribute filtering without group filtering. + sqlBuilder.setTail(") AS Q) AS S) AS R) AS P WHERE P.RowNum BETWEEN ? AND ?", limit, offset); + } else { + sqlBuilder.setTail(") AS R) AS P WHERE P.RowNum BETWEEN ? AND ?", limit, offset); + } + } else if (ORACLE.equals(dbType)) { + sqlBuilder.setTail(" ORDER BY UM_USER_NAME) where rownum <= ?) WHERE rnum > ?", limit, offset); + } else { + sqlBuilder.setTail(" ORDER BY UM_USER_NAME ASC LIMIT ? OFFSET ?", limit, offset); } - } else if (ORACLE.equals(dbType)) { - sqlBuilder.setTail(" ORDER BY UM_USER_NAME) where rownum <= ?) WHERE rnum > ?", limit, offset); - } else { - sqlBuilder.setTail(" ORDER BY UM_USER_NAME ASC LIMIT ? OFFSET ?", limit, offset); } } return sqlBuilder; } - protected SqlBuilder getQueryStringCursor(boolean isGroupFiltering, boolean isUsernameFiltering, - boolean isClaimFiltering, List expressionConditions, int limit, - String cursor, String direction, String sortBy, String sortOrder, String profileName, String dbType, - int totalMultiGroupFilters, int totalMultiClaimFilters) throws UserStoreException { - - StringBuilder sqlStatement; - SqlBuilder sqlBuilder; - boolean hitGroupFilter = false; - boolean hitClaimFilter = false; - int groupFilterCount = 0; - int claimFilterCount = 0; - - if (isGroupFiltering && isUsernameFiltering && isClaimFiltering || isGroupFiltering && isClaimFiltering) { - - if (DB2.equals(dbType)) { - sqlStatement = new StringBuilder("SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT " - + "ROW_NUMBER() OVER (ORDER BY UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME " - + "FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN UM_USER U " - + "ON UR.UM_USER_ID =U.UM_ID INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); - } else if (MSSQL.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " - + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT U.UM_USER_ID, " + - "UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID " + - "INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN UM_USER_ATTRIBUTE UA ON" + - " U.UM_ID = UA.UM_USER_ID"); - } else if (ORACLE.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT UM_USER_NAME, rownum AS rnum " - + "FROM (SELECT UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR" - + ".UM_ROLE_ID INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN " - + "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); - } else if (POSTGRE_SQL.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " + - "UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN" + - " UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN " + - "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); - } else { - sqlStatement = new StringBuilder( - "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " - + "UM_USER_ROLE UR INNER JOIN UM_USER U INNER JOIN UM_USER_ATTRIBUTE UA ON R.UM_ID = " - + "UR.UM_ROLE_ID AND UR.UM_USER_ID =" + " U.UM_ID AND U.UM_ID = UA.UM_USER_ID"); - } - sqlBuilder = new SqlBuilder(sqlStatement).where("R.UM_TENANT_ID = ?", tenantId) - .where("U.UM_TENANT_ID = ?", tenantId).where("UR.UM_TENANT_ID = ?", tenantId) - .where("UA.UM_TENANT_ID = ?", tenantId).where("UA.UM_PROFILE_ID = ?", profileName); - } else if (isGroupFiltering && isUsernameFiltering || isGroupFiltering) { - if (DB2.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " - + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME FROM UM_ROLE R INNER" - + " JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN UM_USER U ON UR" - + ".UM_USER_ID " - + "=U.UM_ID "); - } else if (MSSQL.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " - + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT U.UM_USER_ID, " + - "UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID " + - "INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID"); - } else if (ORACLE.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT UM_USER_NAME, rownum AS rnum " - + "FROM (SELECT UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR" - + ".UM_ROLE_ID INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID"); - } else if (POSTGRE_SQL.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " + - "UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN " + - "UM_USER U ON UR.UM_USER_ID=U.UM_ID"); - } else { - sqlStatement = new StringBuilder( - "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " - + "UM_USER_ROLE UR INNER JOIN UM_USER U ON R.UM_ID = UR.UM_ROLE_ID AND UR.UM_USER_ID " - + "=U.UM_ID"); - } - - sqlBuilder = new SqlBuilder(sqlStatement).where("R.UM_TENANT_ID = ?", tenantId) - .where("U.UM_TENANT_ID = ?", tenantId).where("UR.UM_TENANT_ID = ?", tenantId); - } else if (isUsernameFiltering && isClaimFiltering || isClaimFiltering) { - if (DB2.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " - + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME FROM UM_USER U " - + "INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); - } else if (MSSQL.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " + - "(ORDER BY UM_USER_NAME) AS RowNum FROM " + - "(SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U " + - "INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); - } else if (ORACLE.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum FROM " - + "(SELECT U.UM_USER_ID, UM_USER_NAME FROM UM_USER U INNER JOIN UM_USER_ATTRIBUTE UA " - + "ON U.UM_ID = UA.UM_USER_ID"); - } else { - sqlStatement = new StringBuilder( - "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U INNER JOIN " - + "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); - } - sqlBuilder = new SqlBuilder(sqlStatement).where("U.UM_TENANT_ID = ?", tenantId) - .where("UA.UM_TENANT_ID = ?", tenantId).where("UA.UM_PROFILE_ID = ?", profileName); - } else if (isUsernameFiltering) { - - if (UserCoreConstants.PREVIOUS.equals(direction)) { - sqlStatement = new StringBuilder("WITH this_set AS("); - - if (DB2.equals(dbType)) { - sqlStatement.append( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " - + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_ID, UM_USER_NAME FROM " + - "UM_USER U"); - } else if (MSSQL.equals(dbType)) { - sqlStatement.append( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " - + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT UM_USER_NAME, UM_USER_ID" + - " FROM UM_USER U"); - } else if (ORACLE.equals(dbType)) { - sqlStatement.append( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum " - + "FROM (SELECT UM_USER_ID, UM_USER_NAME FROM UM_USER U"); - } else { - sqlStatement.append("SELECT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U"); - } - } else { - if (DB2.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " - + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_ID, UM_USER_NAME FROM " + - "UM_USER U"); - } else if (MSSQL.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " - + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT UM_USER_NAME, UM_USER_ID" + - " FROM UM_USER U"); - } else if (ORACLE.equals(dbType)) { - sqlStatement = new StringBuilder( - "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum " - + "FROM (SELECT UM_USER_ID, UM_USER_NAME FROM UM_USER U"); - } else { - sqlStatement = new StringBuilder("SELECT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U"); - } - } - - sqlBuilder = new SqlBuilder(sqlStatement).where("U.UM_TENANT_ID = ?", tenantId); - } else { - throw new UserStoreException("Condition is not valid."); - } - - SqlBuilder header = new SqlBuilder(new StringBuilder(sqlBuilder.getSql())); - addingWheres(sqlBuilder, header); - - for (ExpressionCondition expressionCondition : expressionConditions) { - if (ExpressionAttribute.ROLE.toString().equals(expressionCondition.getAttributeName())) { - if (!(MYSQL.equals(dbType) || MARIADB.equals(dbType)) || totalMultiGroupFilters > 1 - && totalMultiClaimFilters > 1) { - multiGroupQueryBuilder(sqlBuilder, header, hitGroupFilter, expressionCondition); - hitGroupFilter = true; - } else { - multiGroupMySqlQueryBuilder(sqlBuilder, groupFilterCount, expressionCondition); - groupFilterCount++; - } - } else if (ExpressionOperation.EQ.toString().equals(expressionCondition.getOperation()) - && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { - if (isCaseSensitiveUsername()) { - sqlBuilder.where("U.UM_USER_NAME = ?", expressionCondition.getAttributeValue()); - } else { - sqlBuilder.where("U.UM_USER_NAME = LOWER(?)", expressionCondition.getAttributeValue()); - } - } else if (ExpressionOperation.CO.toString().equals(expressionCondition.getOperation()) - && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { - if (isCaseSensitiveUsername()) { - sqlBuilder.where("U.UM_USER_NAME LIKE ?", "%" + expressionCondition.getAttributeValue() + "%"); - } else { - sqlBuilder - .where("U.UM_USER_NAME LIKE LOWER(?)", "%" + expressionCondition.getAttributeValue() + "%"); - } - } else if (ExpressionOperation.EW.toString().equals(expressionCondition.getOperation()) - && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { - if (isCaseSensitiveUsername()) { - sqlBuilder.where("U.UM_USER_NAME LIKE ?", "%" + expressionCondition.getAttributeValue()); - } else { - sqlBuilder.where("U.UM_USER_NAME LIKE LOWER(?)", "%" + expressionCondition.getAttributeValue()); - } - } else if (ExpressionOperation.SW.toString().equals(expressionCondition.getOperation()) - && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { - if (isCaseSensitiveUsername()) { - sqlBuilder.where("U.UM_USER_NAME LIKE ?", expressionCondition.getAttributeValue() + "%"); - } else { - sqlBuilder.where("U.UM_USER_NAME LIKE LOWER(?)", expressionCondition.getAttributeValue() + "%"); - } - } else { - // Claim filtering - if (!(MYSQL.equals(dbType) || MARIADB.equals(dbType)) || totalMultiGroupFilters > 1 - && totalMultiClaimFilters > 1) { - multiClaimQueryBuilder(sqlBuilder, header, hitClaimFilter, expressionCondition); - hitClaimFilter = true; - } else { - multiClaimMySqlQueryBuilder(sqlBuilder, claimFilterCount, expressionCondition); - claimFilterCount++; - } - } - } - - if (UserCoreConstants.PREVIOUS.equals(direction)) { - sqlBuilder.where("U.UM_USER_NAME < ?", cursor); - } else if (UserCoreConstants.NEXT.equals(direction)) { - sqlBuilder.where("U.UM_USER_NAME > ?", cursor); - } - - if (MYSQL.equals(dbType) || MARIADB.equals(dbType)) { - sqlBuilder.updateSql(" GROUP BY U.UM_USER_NAME, U.UM_USER_ID "); - if (groupFilterCount > 0 && claimFilterCount > 0) { - sqlBuilder.updateSql(" HAVING (COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount + - " AND COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount + ")"); - } else if (groupFilterCount > 0) { - sqlBuilder.updateSql(" HAVING COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount); - } else if (claimFilterCount > 0) { - sqlBuilder.updateSql(" HAVING COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount); - } - } - - if (!((MYSQL.equals(dbType) || MARIADB.equals(dbType)) && totalMultiGroupFilters > 1 - && totalMultiClaimFilters > 1)) { - if (DB2.equals(dbType)) { - sqlBuilder.setTail(") AS p) WHERE rn BETWEEN ?", limit); - } else if (MSSQL.equals(dbType)) { - if (isClaimFiltering && !isGroupFiltering && totalMultiClaimFilters > 1) { - // Handle multi attribute filtering without group filtering. - sqlBuilder.setTail(") AS Q) AS S) AS R) AS P WHERE P.RowNum BETWEEN ?", limit); - } else { - sqlBuilder.setTail(") AS R) AS P WHERE P.RowNum BETWEEN ?", limit); - } - } else if (ORACLE.equals(dbType)) { - sqlBuilder.setTail(" ORDER BY UM_USER_NAME) where rownum <= ?)", limit); - } else { - if (UserCoreConstants.PREVIOUS.equals(direction)) { - sqlBuilder.setTail(" ORDER BY UM_USER_NAME DESC LIMIT ? )SELECT * " + - "from this_set ORDER BY UM_USER_NAME ASC;", limit); - } else if (UserCoreConstants.NEXT.equals(direction)) { - sqlBuilder.setTail(" ORDER BY UM_USER_NAME ASC LIMIT ?", limit); - } - } - } - return sqlBuilder; - } - -// protected SqlBuilder getQueryStringCursorPrev(boolean isGroupFiltering, boolean isUsernameFiltering, -// boolean isClaimFiltering, List expressionConditions, int limit, -// String cursor, String sortBy, String sortOrder, String profileName, String dbType, -// int totalMultiGroupFilters, int totalMultiClaimFilters) -// throws UserStoreException { -// -// StringBuilder sqlStatement; -// SqlBuilder sqlBuilder; -// boolean hitGroupFilter = false; -// boolean hitClaimFilter = false; -// int groupFilterCount = 0; -// int claimFilterCount = 0; -// -// if (isGroupFiltering && isUsernameFiltering && isClaimFiltering || isGroupFiltering && isClaimFiltering) { -// -// if (DB2.equals(dbType)) { -// sqlStatement = new StringBuilder("SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT " -// + "ROW_NUMBER() OVER (ORDER BY UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME " -// + "FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN UM_USER U " -// + "ON UR.UM_USER_ID =U.UM_ID INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); -// } else if (MSSQL.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " -// + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT U.UM_USER_ID, " + -// "UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID " + -// "INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN UM_USER_ATTRIBUTE UA ON" + -// " U.UM_ID = UA.UM_USER_ID"); -// } else if (ORACLE.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT UM_USER_NAME, rownum AS rnum " -// + "FROM (SELECT UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR" -// + ".UM_ROLE_ID INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN " -// + "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); -// } else if (POSTGRE_SQL.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " + -// "UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN" + -// " UM_USER U ON UR.UM_USER_ID =U.UM_ID INNER JOIN " + -// "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); -// } else { -// sqlStatement = new StringBuilder( -// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " -// + "UM_USER_ROLE UR INNER JOIN UM_USER U INNER JOIN UM_USER_ATTRIBUTE UA ON R.UM_ID = " -// + "UR.UM_ROLE_ID AND UR.UM_USER_ID =" + " U.UM_ID AND U.UM_ID = UA.UM_USER_ID"); -// } -// sqlBuilder = new SqlBuilder(sqlStatement).where("R.UM_TENANT_ID = ?", tenantId) -// .where("U.UM_TENANT_ID = ?", tenantId).where("UR.UM_TENANT_ID = ?", tenantId) -// .where("UA.UM_TENANT_ID = ?", tenantId).where("UA.UM_PROFILE_ID = ?", profileName); -// } else if (isGroupFiltering && isUsernameFiltering || isGroupFiltering) { -// if (DB2.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " -// + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME FROM UM_ROLE R INNER" -// + " JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN UM_USER U ON UR" -// + ".UM_USER_ID " -// + "=U.UM_ID "); -// } else if (MSSQL.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " -// + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT U.UM_USER_ID, " + -// "UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID " + -// "INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID"); -// } else if (ORACLE.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT UM_USER_NAME, rownum AS rnum " -// + "FROM (SELECT UM_USER_NAME FROM UM_ROLE R INNER JOIN UM_USER_ROLE UR ON R.UM_ID = UR" -// + ".UM_ROLE_ID INNER JOIN UM_USER U ON UR.UM_USER_ID =U.UM_ID"); -// } else if (POSTGRE_SQL.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " + -// "UM_USER_ROLE UR ON R.UM_ID = UR.UM_ROLE_ID INNER JOIN " + -// "UM_USER U ON UR.UM_USER_ID=U.UM_ID"); -// } else { -// sqlStatement = new StringBuilder( -// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_ROLE R INNER JOIN " -// + "UM_USER_ROLE UR INNER JOIN UM_USER U ON R.UM_ID = UR.UM_ROLE_ID AND UR.UM_USER_ID " -// + "=U.UM_ID"); -// } -// -// sqlBuilder = new SqlBuilder(sqlStatement).where("R.UM_TENANT_ID = ?", tenantId) -// .where("U.UM_TENANT_ID = ?", tenantId).where("UR.UM_TENANT_ID = ?", tenantId); -// } else if (isUsernameFiltering && isClaimFiltering || isClaimFiltering) { -// if (DB2.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " -// + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_NAME FROM UM_USER U " -// + "INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); -// } else if (MSSQL.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " + -// "(ORDER BY UM_USER_NAME) AS RowNum FROM " + -// "(SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U " + -// "INNER JOIN UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); -// } else if (ORACLE.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum FROM " -// + "(SELECT U.UM_USER_ID, UM_USER_NAME FROM UM_USER U INNER JOIN UM_USER_ATTRIBUTE UA " -// + "ON U.UM_ID = UA.UM_USER_ID"); -// } else { -// sqlStatement = new StringBuilder( -// "SELECT DISTINCT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U INNER JOIN " -// + "UM_USER_ATTRIBUTE UA ON U.UM_ID = UA.UM_USER_ID"); -// } -// sqlBuilder = new SqlBuilder(sqlStatement).where("U.UM_TENANT_ID = ?", tenantId) -// .where("UA.UM_TENANT_ID = ?", tenantId).where("UA.UM_PROFILE_ID = ?", profileName); -// } else if (isUsernameFiltering) { -// if (DB2.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT ROW_NUMBER() OVER (ORDER BY " -// + "UM_USER_NAME) AS rn, p.* FROM (SELECT DISTINCT UM_USER_ID, UM_USER_NAME FROM " + -// "UM_USER U"); -// } else if (MSSQL.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, ROW_NUMBER() OVER " -// + "(ORDER BY UM_USER_NAME) AS RowNum FROM (SELECT DISTINCT UM_USER_NAME, UM_USER_ID" + -// " FROM UM_USER U"); -// } else if (ORACLE.equals(dbType)) { -// sqlStatement = new StringBuilder( -// "SELECT UM_USER_ID, UM_USER_NAME FROM (SELECT UM_USER_ID, UM_USER_NAME, rownum AS rnum " -// + "FROM (SELECT UM_USER_ID, UM_USER_NAME FROM UM_USER U"); -// } else { -// sqlStatement = new StringBuilder("WITH this_set AS(\n" + -// "SELECT U.UM_USER_ID, U.UM_USER_NAME FROM UM_USER U"); -// } -// -// sqlBuilder = new SqlBuilder(sqlStatement).where("U.UM_TENANT_ID = ?", tenantId); -// } else { -// throw new UserStoreException("Condition is not valid."); -// } -// -// SqlBuilder header = new SqlBuilder(new StringBuilder(sqlBuilder.getSql())); -// addingWheres(sqlBuilder, header); -// -// for (ExpressionCondition expressionCondition : expressionConditions) { -// if (ExpressionAttribute.ROLE.toString().equals(expressionCondition.getAttributeName())) { -// if (!(MYSQL.equals(dbType) || MARIADB.equals(dbType)) || totalMultiGroupFilters > 1 -// && totalMultiClaimFilters > 1) { -// multiGroupQueryBuilder(sqlBuilder, header, hitGroupFilter, expressionCondition); -// hitGroupFilter = true; -// } else { -// multiGroupMySqlQueryBuilder(sqlBuilder, groupFilterCount, expressionCondition); -// groupFilterCount++; -// } -// } else if (ExpressionOperation.EQ.toString().equals(expressionCondition.getOperation()) -// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { -// if (isCaseSensitiveUsername()) { -// sqlBuilder.where("U.UM_USER_NAME = ?", expressionCondition.getAttributeValue()); -// } else { -// sqlBuilder.where("U.UM_USER_NAME = LOWER(?)", expressionCondition.getAttributeValue()); -// } -// } else if (ExpressionOperation.CO.toString().equals(expressionCondition.getOperation()) -// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { -// if (isCaseSensitiveUsername()) { -// sqlBuilder.where("U.UM_USER_NAME LIKE ?", "%" + expressionCondition.getAttributeValue() + "%"); -// } else { -// sqlBuilder -// .where("U.UM_USER_NAME LIKE LOWER(?)", "%" + expressionCondition.getAttributeValue() + "%"); -// } -// } else if (ExpressionOperation.EW.toString().equals(expressionCondition.getOperation()) -// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { -// if (isCaseSensitiveUsername()) { -// sqlBuilder.where("U.UM_USER_NAME LIKE ?", "%" + expressionCondition.getAttributeValue()); -// } else { -// sqlBuilder.where("U.UM_USER_NAME LIKE LOWER(?)", "%" + expressionCondition.getAttributeValue()); -// } -// } else if (ExpressionOperation.SW.toString().equals(expressionCondition.getOperation()) -// && ExpressionAttribute.USERNAME.toString().equals(expressionCondition.getAttributeName())) { -// if (isCaseSensitiveUsername()) { -// sqlBuilder.where("U.UM_USER_NAME LIKE ?", expressionCondition.getAttributeValue() + "%"); -// } else { -// sqlBuilder.where("U.UM_USER_NAME LIKE LOWER(?)", expressionCondition.getAttributeValue() + "%"); -// } -// } else { -// // Claim filtering -// if (!(MYSQL.equals(dbType) || MARIADB.equals(dbType)) || totalMultiGroupFilters > 1 -// && totalMultiClaimFilters > 1) { -// multiClaimQueryBuilder(sqlBuilder, header, hitClaimFilter, expressionCondition); -// hitClaimFilter = true; -// } else { -// multiClaimMySqlQueryBuilder(sqlBuilder, claimFilterCount, expressionCondition); -// claimFilterCount++; -// } -// } -// } -// sqlBuilder.where("U.UM_USER_NAME < ?", cursor); -// -// if (MYSQL.equals(dbType) || MARIADB.equals(dbType)) { -// sqlBuilder.updateSql(" GROUP BY U.UM_USER_NAME, U.UM_USER_ID "); -// if (groupFilterCount > 0 && claimFilterCount > 0) { -// sqlBuilder.updateSql(" HAVING (COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount + -// " AND COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount + ")"); -// } else if (groupFilterCount > 0) { -// sqlBuilder.updateSql(" HAVING COUNT(DISTINCT R.UM_ROLE_NAME) = " + groupFilterCount); -// } else if (claimFilterCount > 0) { -// sqlBuilder.updateSql(" HAVING COUNT(DISTINCT UA.UM_ATTR_VALUE) = " + claimFilterCount); -// } -// } -// -// if (!((MYSQL.equals(dbType) || MARIADB.equals(dbType)) && totalMultiGroupFilters > 1 -// && totalMultiClaimFilters > 1)) { -// if (DB2.equals(dbType)) { -// sqlBuilder.setTail(") AS p) WHERE rn BETWEEN ?", limit); -// } else if (MSSQL.equals(dbType)) { -// if (isClaimFiltering && !isGroupFiltering && totalMultiClaimFilters > 1) { -// // Handle multi attribute filtering without group filtering. -// sqlBuilder.setTail(") AS Q) AS S) AS R) AS P WHERE P.RowNum BETWEEN ?", limit); -// } else { -// sqlBuilder.setTail(") AS R) AS P WHERE P.RowNum BETWEEN ?", limit); -// } -// } else if (ORACLE.equals(dbType)) { -// sqlBuilder.setTail(" ORDER BY UM_USER_NAME) where rownum <= ?)", limit); -// } else { -// sqlBuilder.setTail(" ORDER BY UM_USER_NAME DESC LIMIT ? \n )SELECT * " + -// "from this_set ORDER BY UM_USER_NAME ASC;", limit); -// } -// } -// return sqlBuilder; -// } - private void multiGroupQueryBuilder(SqlBuilder sqlBuilder, SqlBuilder header, boolean hitFirstRound, ExpressionCondition expressionCondition) { diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/listener/UniqueIDUserOperationEventListener.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/listener/UniqueIDUserOperationEventListener.java index 28802b6cd18..84d0972fc61 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/listener/UniqueIDUserOperationEventListener.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/listener/UniqueIDUserOperationEventListener.java @@ -150,6 +150,25 @@ boolean doPreGetUserListWithID(String claimUri, String claimValue, final List