diff --git a/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java b/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java index f00ea05cac..4d03042fc6 100644 --- a/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java +++ b/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java @@ -182,6 +182,8 @@ public enum ValidationUserProfile { RangerConstants.ROLE_SYS_ADMIN, RangerConstants.ROLE_KEY_ADMIN, RangerConstants.ROLE_ADMIN_AUDITOR, RangerConstants.ROLE_KEY_ADMIN_AUDITOR)); + public static final String DEFAULT_SORT_ORDER = "asc"; + public static enum RBAC_PERM { ALLOW_NONE, ALLOW_READ, diff --git a/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java b/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java index 5ddc965a4c..62ff8e1356 100644 --- a/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java +++ b/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java @@ -194,25 +194,27 @@ public SearchFilter extractCommonCriteriasForFilter(HttpServletRequest request, StringUtil.VALIDATION_ALPHA, "Invalid value for parameter sortBy", MessageEnums.INVALID_INPUT_DATA, null, SearchFilter.SORT_BY); - boolean sortSet = false; if (!StringUtils.isEmpty(sortBy)) { + boolean sortSet = false; + for (SortField sortField : sortFields) { if (sortField.getParamName().equalsIgnoreCase(sortBy)) { ret.setSortBy(sortField.getParamName()); - String sortType = restErrorUtil.validateString(request.getParameter("sortType"), - StringUtil.VALIDATION_ALPHA, "Invalid value for parameter sortType", - MessageEnums.INVALID_INPUT_DATA, null, "sortType"); - ret.setSortType(sortType); sortSet = true; break; } } - } - if (!sortSet && !StringUtils.isEmpty(sortBy)) { - logger.info("Invalid or unsupported sortBy field passed. sortBy=" + sortBy, new Throwable()); + if (!sortSet) { + logger.info("Invalid or unsupported sortBy field passed. sortBy=" + sortBy, new Throwable()); + } } - + + String sortType = restErrorUtil.validateString(request.getParameter("sortType"), + StringUtil.VALIDATION_ALPHA, "Invalid value for parameter sortType", + MessageEnums.INVALID_INPUT_DATA, null, "sortType"); + ret.setSortType(sortType); + if(ret.getParams() == null) { ret.setParams(new HashMap()); } @@ -383,11 +385,14 @@ public void updateQueryPageSize(Query query, SearchFilter searchCriteria) { } public String constructSortClause(SearchFilter searchCriteria, List sortFields) { - String sortBy = searchCriteria.getSortBy(); + String ret = null; + String sortBy = searchCriteria.getSortBy(); + String sortType = getSortType(searchCriteria); String querySortBy = null; if (!stringUtil.isEmpty(sortBy)) { sortBy = sortBy.trim(); + for (SortField sortField : sortFields) { if (sortBy.equalsIgnoreCase(sortField.getParamName())) { querySortBy = sortField.getFieldName(); @@ -404,30 +409,40 @@ public String constructSortClause(SearchFilter searchCriteria, List s querySortBy = sortField.getFieldName(); // Override the sortBy using the default value searchCriteria.setSortBy(sortField.getParamName()); - searchCriteria.setSortType(sortField.getDefaultOrder().name()); + + if(sortType == null) { + sortType = sortField.getDefaultOrder().name(); + } + + searchCriteria.setSortType(sortType); break; } } } if (querySortBy != null) { - String sortType = searchCriteria.getSortType(); - String querySortType = "asc"; - if (sortType != null) { - if ("asc".equalsIgnoreCase(sortType) || "desc".equalsIgnoreCase(sortType)) { - querySortType = sortType; - } else { - logger.error("Invalid sortType. sortType=" + sortType); - } - } - - if(querySortType!=null){ - searchCriteria.setSortType(querySortType.toLowerCase()); - } - String sortClause = " ORDER BY " + querySortBy + " " + querySortType; + String querySortType = stringUtil.isEmpty(sortType) ? RangerConstants.DEFAULT_SORT_ORDER : sortType; + + searchCriteria.setSortType(querySortType.toLowerCase()); - return sortClause; + ret = " ORDER BY " + querySortBy + " " + querySortType; } - return null; + + return ret; + } + + private String getSortType(SearchFilter searchCriteria) { + String ret = null; + String sortType = searchCriteria.getSortType(); + + if (!stringUtil.isEmpty(sortType)) { + if ("asc".equalsIgnoreCase(sortType) || "desc".equalsIgnoreCase(sortType)) { + ret = sortType; + } else { + logger.error("Invalid sortType. sortType=" + sortType); + } + } + + return ret; } }