Skip to content

Commit eed8f9d

Browse files
yangxuzemneethiraj
authored andcommitted
RANGER-3500: updated policy retrieval to support sortBy and sortOrder
Signed-off-by: Madhan Neethiraj <[email protected]> (cherry picked from commit 62cbf40)
1 parent e657c77 commit eed8f9d

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

agents-common/src/main/java/org/apache/ranger/plugin/store/AbstractPredicateUtil.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ public void applyFilter(List<? extends RangerBaseModelObject> objList, SearchFil
6060

6161
Comparator<RangerBaseModelObject> sorter = getSorter(filter);
6262

63-
if(sorter != null) {
64-
Collections.sort(objList, sorter);
63+
if (sorter != null) {
64+
boolean isDesc = "desc".equalsIgnoreCase(filter.getSortType());
65+
66+
Collections.sort(objList, isDesc ? new ReverseComparator(sorter) : sorter);
6567
}
6668
}
6769

@@ -232,7 +234,6 @@ public int compare(RangerBaseModelObject o1, RangerBaseModelObject o2) {
232234
sorterMap.put(SearchFilter.SERVICE_TYPE, serviceDefNameComparator);
233235
sorterMap.put(SearchFilter.SERVICE_TYPE_ID, idComparator);
234236
sorterMap.put(SearchFilter.SERVICE_NAME, serviceNameComparator);
235-
sorterMap.put(SearchFilter.SERVICE_TYPE_ID, idComparator);
236237
sorterMap.put(SearchFilter.POLICY_NAME, policyNameComparator);
237238
sorterMap.put(SearchFilter.POLICY_ID, idComparator);
238239
sorterMap.put(SearchFilter.CREATE_TIME, createTimeComparator);
@@ -1050,4 +1051,17 @@ public boolean evaluate(Object object) {
10501051

10511052
return ret;
10521053
}
1054+
1055+
private static class ReverseComparator implements Comparator<RangerBaseModelObject> {
1056+
private final Comparator<RangerBaseModelObject> comparator;
1057+
1058+
ReverseComparator(Comparator<RangerBaseModelObject> comparator) {
1059+
this.comparator = comparator;
1060+
}
1061+
1062+
@Override
1063+
public int compare(RangerBaseModelObject o1, RangerBaseModelObject o2) {
1064+
return comparator.compare(o2, o1);
1065+
}
1066+
}
10531067
}

agents-common/src/main/java/org/apache/ranger/plugin/util/SearchFilter.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,20 @@ public class SearchFilter {
103103
private String sortType;
104104

105105
public SearchFilter() {
106-
this(null);
106+
this((Map<String, String>) null);
107+
}
108+
109+
public SearchFilter(SearchFilter other) {
110+
if (other != null) {
111+
setParams(other.params != null ? new HashMap<>(other.params) : null);
112+
setStartIndex(other.startIndex);
113+
setMaxRows(other.maxRows);
114+
setGetCount(other.getCount);
115+
setSortBy(other.sortBy);
116+
setSortType(other.sortType);
117+
} else {
118+
setParams(null);
119+
}
107120
}
108121

109122
public SearchFilter(String name, String value) {

security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,13 +2588,10 @@ private List<RangerPolicy> getServicePolicies(XXService service, SearchFilter fi
25882588
final List<RangerPolicy> policies = servicePolicies != null ? servicePolicies.getPolicies() : null;
25892589

25902590
if(policies != null && filter != null && MapUtils.isNotEmpty(filter.getParams())) {
2591-
Map<String, String> filterResources = filter.getParamsWithPrefix(SearchFilter.RESOURCE_PREFIX, true);
2592-
String resourceMatchScope = filter.getParam(SearchFilter.RESOURCE_MATCH_SCOPE);
2593-
2594-
boolean useLegacyResourceSearch = true;
2595-
2596-
Map<String, String> paramsCopy = new HashMap<>(filter.getParams());
2597-
SearchFilter searchFilter = new SearchFilter(paramsCopy);
2591+
Map<String, String> filterResources = filter.getParamsWithPrefix(SearchFilter.RESOURCE_PREFIX, true);
2592+
String resourceMatchScope = filter.getParam(SearchFilter.RESOURCE_MATCH_SCOPE);
2593+
boolean useLegacyResourceSearch = true;
2594+
SearchFilter searchFilter = new SearchFilter(filter);
25982595

25992596
if (MapUtils.isNotEmpty(filterResources) && resourceMatchScope != null) {
26002597
useLegacyResourceSearch = false;

0 commit comments

Comments
 (0)