/** * returns true if parentName is a valid parent for roleName (avoiding cycles) * * @param roleName * @param parentName */ public boolean isValidParent(String roleName, String parentName) { if (parentName == null || parentName.length() == 0) return true; if (roleName.equals(parentName)) return false; if (getDescendants(roleName).contains(parentName)) return false; return true; } }
/** * Get all descendant roles, the order is randomly * * @param roleName */ public List<String> getDescendants(String roleName) { checkRole(roleName); Set<String> descendants = new HashSet<String>(); fillDescendents(getChildren(roleName), descendants); List<String> result = new ArrayList<String>(); result.addAll(descendants); return result; }
/** Get a list of root roles */ public List<String> getRootRoles() { List<String> result = new ArrayList<String>(); for (String roleName : parentMappings.keySet()) { if (isRoot(roleName)) result.add(roleName); } return result; }
/** * return the parent role name, null if role has no parent * * @param roleName */ public String getParent(String roleName) { checkRole(roleName); String parentRole = parentMappings.get(roleName); if (roleName.equals(parentRole)) cycleDetected(roleName, null); return parentRole; }
/** * Calculate an ordered list of ancestors, starting with the parent * * @param roleName */ public List<String> getAncestors(String roleName) { checkRole(roleName); List<String> ancestors = new ArrayList<String>(); fillAncestors(parentMappings.get(roleName), ancestors); return ancestors; }
{"node112", "node11"}, }); RoleHierarchyHelper helper = new RoleHierarchyHelper(map); assertFalse(helper.containsRole("abc")); assertTrue(helper.containsRole("node11")); assertFalse(helper.isRoot("node11")); assertTrue(helper.isRoot("node1")); assertEquals(1, helper.getRootRoles().size()); assertTrue(helper.getRootRoles().contains("node1")); assertEquals(3, helper.getLeafRoles().size()); assertTrue(helper.getLeafRoles().contains("node111")); assertTrue(helper.getLeafRoles().contains("node112")); assertTrue(helper.getLeafRoles().contains("node12")); assertEquals("node1", helper.getParent("node11")); assertNull(helper.getParent("node1")); assertEquals(0, helper.getAncestors("node1").size()); assertEquals(1, helper.getAncestors("node12").size()); assertTrue(helper.getAncestors("node12").contains("node1")); assertEquals(2, helper.getAncestors("node112").size()); assertTrue(helper.getAncestors("node112").contains("node11")); assertTrue(helper.getAncestors("node112").contains("node1")); assertEquals(2, helper.getChildren("node1").size()); assertTrue(helper.getChildren("node1").contains("node11")); assertTrue(helper.getChildren("node1").contains("node12"));
{"node2", "node1"} }); RoleHierarchyHelper helper = new RoleHierarchyHelper(map); boolean fail; helper.getParent("node1"); // ok helper.getAncestors("node1"); } catch (RuntimeException e) { fail = false; helper.getChildren("node1"); // ok helper.getDescendants("node1"); } catch (RuntimeException e) { fail = false;
List<String> computeAllowableParentRoles(GeoServerRole role) throws IOException { Map<String, String> parentMappings = getRoleService(roleServiceName).getParentMappings(); if (role != null && StringUtils.hasLength(role.getAuthority())) { // filter out roles already used as parents RoleHierarchyHelper helper = new RoleHierarchyHelper(parentMappings); Set<String> parents = new HashSet<String>(parentMappings.keySet()); parents.removeAll(helper.getDescendants(role.getAuthority())); parents.remove(role.getAuthority()); return new ArrayList(parents); } else { // no rolename given, we are creating a new one return new ArrayList(parentMappings.keySet()); } }
public void setParentRole(GeoServerRole role, GeoServerRole parentRole) throws IOException { RoleHierarchyHelper hhelper = new RoleHierarchyHelper(getParentMappings()); if (hhelper.isValidParent( role.getAuthority(), parentRole == null ? null : parentRole.getAuthority()) == false) throw new IOException( parentRole.getAuthority() + " is not a valid parent for " + role.getAuthority()); checkRole(role); if (parentRole == null) { helper.role_parentMap.remove(role); } else { checkRole(parentRole); helper.role_parentMap.put(role, parentRole); } setModified(true); }
/** * recursive method to fill the descendant list * * @param children * @param descendants */ protected void fillDescendents(List<String> children, Set<String> descendants) { if (children == null || children.size() == 0) return; // end recursion for (String childName : children) { if (descendants.contains(childName)) // cycle cycleDetected(childName, null); descendants.add(childName); } for (String childName : children) { List<String> grandchildren = getChildren(childName); fillDescendents(grandchildren, descendants); } }
/** * Check if the role is a root role * * @param roleName */ public boolean isRoot(String roleName) { checkRole(roleName); return parentMappings.get(roleName) == null; }
@Test public void testInValidTree1() throws Exception { Map<String, String> map = createFromArray(new String[][] {{"node1", "node1"}}); RoleHierarchyHelper helper = new RoleHierarchyHelper(map); boolean fail; helper.getParent("node1"); } catch (RuntimeException e) { fail = false; helper.getAncestors("node1"); } catch (RuntimeException e) { fail = false; helper.getChildren("node1"); } catch (RuntimeException e) { fail = false; helper.getDescendants("node1"); } catch (RuntimeException e) { fail = false;
List<String> computeAllowableParentRoles(GeoServerRole role) throws IOException { Map<String, String> parentMappings = getRoleService(roleServiceName).getParentMappings(); if (role != null && StringUtils.hasLength(role.getAuthority())) { //filter out roles already used as parents RoleHierarchyHelper helper = new RoleHierarchyHelper(parentMappings); Set<String> parents = new HashSet<String>(parentMappings.keySet()); parents.removeAll(helper.getDescendants(role.getAuthority())); parents.remove(role.getAuthority()); return new ArrayList(parents); } else { // no rolename given, we are creating a new one return new ArrayList(parentMappings.keySet()); } }
public void setParentRole(GeoServerRole role, GeoServerRole parentRole) throws IOException { RoleHierarchyHelper helper = new RoleHierarchyHelper(getParentMappings()); if (helper.isValidParent(role.getAuthority(), parentRole==null ? null : parentRole.getAuthority())==false) throw new IOException(parentRole.getAuthority() + " is not a valid parent for " + role.getAuthority()); Connection con = null; PreparedStatement ps = null; try { con = getConnection(); ps = getDMLStatement("roles.parentUpdate", con); if (parentRole == null) ps.setNull(1, Types.VARCHAR); else ps.setString(1,parentRole.getAuthority()); ps.setString(2,role.getAuthority()); ps.execute(); } catch (SQLException ex) { throw new IOException(ex); } finally { closeFinally(con, ps, null); } setModified(true); }
/** * Return child roles * * @param roleName */ public List<String> getChildren(String roleName) { checkRole(roleName); List<String> children = new ArrayList<String>(); for (Entry<String, String> entry : parentMappings.entrySet()) { if (entry.getValue() != null && entry.getValue().equals(roleName)) { if (roleName.equals(entry.getKey())) cycleDetected(roleName, null); children.add(entry.getKey()); } } return children; }
List<String> computeAllowableParentRoles(GeoServerRole role) throws IOException { Map<String, String> parentMappings = getRoleService(roleServiceName).getParentMappings(); if (!role.getAuthority().equals(GeoServerRole.NULL_ROLE.getAuthority()) ) { //filter out roles already used as parents RoleHierarchyHelper helper = new RoleHierarchyHelper(parentMappings); Set<String> parents = new HashSet<String>(parentMappings.keySet()); parents.removeAll(helper.getDescendants(role.getAuthority())); parents.remove(role.getAuthority()); return new ArrayList(parents); } else { // no rolename given, we are creating a new one return new ArrayList(parentMappings.keySet()); } }