Skip to content

Commit

Permalink
Improved role check performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
tHerrmann committed Jan 14, 2016
1 parent f33643b commit e3e3238
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/org/opencms/db/CmsSecurityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -3057,6 +3058,21 @@ public boolean hasRoleForResource(CmsDbContext dbc, CmsUser user, CmsRole role,
// any exception: return false
return false;
}
// to improve performance, sort the roles by OU and name to start the role check closest to the root
Collections.sort(roles, new Comparator<CmsGroup>() {

public int compare(CmsGroup o1, CmsGroup o2) {

String ou1 = o1.getOuFqn();
String ou2 = o2.getOuFqn();

if ((ou1 == null) || (ou2 == null)) {
// one of the OUs is the root OU, the root OU has to show up first
return ou1 == null ? -1 : 1;
}
return ou1.equals(ou2) ? o1.getName().compareTo(o2.getName()) : ou1.compareTo(ou2);
}
});

// first check the user has the role at all
if (!hasRole(role.forOrgUnit(null), roles)) {
Expand Down

0 comments on commit e3e3238

Please sign in to comment.