diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMap.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMap.java index 2928df10ff4f8060d244f1d1ab0c37ae94c2e621..b9c2d659013a033e02a9556e87e97e32ef1aca76 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMap.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMap.java @@ -83,37 +83,41 @@ class CapabilityMap { continue; } - final String[] splitted = StringUtils.split(trimmed); + final String[] splitted = StringUtils.split(trimmed, " \t:"); if (splitted.length != 2) { operationLog.warn(String.format("Ignoring mal-formed line '%s' in %s.", trimmed, filePath)); continue; } - final String methodName = - (splitted[0].endsWith(":") ? splitted[0].substring(0, splitted[0].length() - 1) - : splitted[0]); - final String roleName = splitted[1]; - try + final String methodName = splitted[0]; + final String roleNames = splitted[1]; + final String[] roleNameArray = StringUtils.split(roleNames, ","); + for (String roleName : roleNameArray) { - final RoleWithHierarchy role = RoleWithHierarchy.valueOf(roleName); - Collection<RoleWithHierarchy> roles = new HashSet<RoleWithHierarchy>(); - roles.add(role); - roles = capMap.put(methodName, roles); - if (roles != null) + try { - capMap.get(methodName).addAll(roles); - } + final RoleWithHierarchy role = RoleWithHierarchy.valueOf(roleName); + Collection<RoleWithHierarchy> roles = capMap.get(methodName); + if (roles == null) + { + roles = new HashSet<RoleWithHierarchy>(); + capMap.put(methodName, roles); + } + roles.add(role); - if (operationLog.isDebugEnabled()) + if (operationLog.isDebugEnabled()) + { + operationLog.debug(String + .format("Add to map: '%s' -> %s", methodName, role)); + } + } catch (IllegalArgumentException ex) { - operationLog.debug(String.format("Add to map: '%s' -> %s", methodName, role)); + operationLog.warn(String.format( + "Ignoring mal-formed line '%s' in %s [role '%s' doesn't exist].", + trimmed, + filePath, roleName)); } - } catch (IllegalArgumentException ex) - { - operationLog.warn(String.format( - "Ignoring mal-formed line '%s' in %s [role '%s' doesn't exist].", trimmed, - filePath, roleName)); } } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMapTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMapTest.java index ad747cfeee41bee52ad14ab6eafa43ce97e1d9db..deda70438d058e1e137a192d6751918c48c394c3 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMapTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/authorization/CapabilityMapTest.java @@ -80,6 +80,91 @@ public class CapabilityMapTest assertNull(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyC"))); } + @Test + public void testHappyCaseAlternative() throws SecurityException, NoSuchMethodException + { + CapabilityMap capMap = + new CapabilityMap(Arrays.asList("A:SPACE_POWER_USER\t", "# Some comment", "", + " B INSTANCE_ETL_SERVER"), "<memory>"); + assertEquals( + RoleWithHierarchy.SPACE_POWER_USER, + capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")).toArray()[0]); + assertEquals( + RoleWithHierarchy.SPACE_POWER_USER, + capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA2")).toArray()[0]); + assertEquals( + RoleWithHierarchy.INSTANCE_ETL_SERVER, + capMap.tryGetRoles(CapabilityMapTest.class + .getDeclaredMethod("dummyB", String.class)).toArray()[0]); + assertNull(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyC"))); + } + + @Test + public void testHappyCaseAlternative2() throws SecurityException, NoSuchMethodException + { + CapabilityMap capMap = + new CapabilityMap(Arrays.asList("A :SPACE_POWER_USER\t", "# Some comment", "", + " B INSTANCE_ETL_SERVER"), "<memory>"); + assertEquals( + RoleWithHierarchy.SPACE_POWER_USER, + capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")).toArray()[0]); + assertEquals( + RoleWithHierarchy.SPACE_POWER_USER, + capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA2")).toArray()[0]); + assertEquals( + RoleWithHierarchy.INSTANCE_ETL_SERVER, + capMap.tryGetRoles(CapabilityMapTest.class + .getDeclaredMethod("dummyB", String.class)).toArray()[0]); + assertNull(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyC"))); + } + + @Test + public void testHappyCaseAlternative3() throws SecurityException, NoSuchMethodException + { + CapabilityMap capMap = + new CapabilityMap(Arrays.asList("A : SPACE_POWER_USER\t", "# Some comment", "", + " B INSTANCE_ETL_SERVER"), "<memory>"); + assertEquals( + RoleWithHierarchy.SPACE_POWER_USER, + capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")).toArray()[0]); + assertEquals( + RoleWithHierarchy.SPACE_POWER_USER, + capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA2")).toArray()[0]); + assertEquals( + RoleWithHierarchy.INSTANCE_ETL_SERVER, + capMap.tryGetRoles(CapabilityMapTest.class + .getDeclaredMethod("dummyB", String.class)).toArray()[0]); + assertNull(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyC"))); + } + + @Test + public void testHappyCaseAlternative4() throws SecurityException, NoSuchMethodException + { + CapabilityMap capMap = + new CapabilityMap(Arrays.asList("A : SPACE_POWER_USER\t", + " A INSTANCE_ETL_SERVER"), "<memory>"); + assertEquals(2, capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")) + .size()); + assertTrue(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")) + .contains(RoleWithHierarchy.SPACE_POWER_USER)); + assertTrue(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")) + .contains(RoleWithHierarchy.INSTANCE_ETL_SERVER)); + } + + @Test + public void testHappyCaseAlternative5() throws SecurityException, NoSuchMethodException + { + CapabilityMap capMap = + new CapabilityMap(Arrays.asList("A : SPACE_POWER_USER,INSTANCE_ETL_SERVER\t"), + "<memory>"); + assertEquals(2, capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")) + .size()); + assertTrue(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")) + .contains(RoleWithHierarchy.SPACE_POWER_USER)); + assertTrue(capMap.tryGetRoles(CapabilityMapTest.class.getDeclaredMethod("dummyA1")) + .contains(RoleWithHierarchy.INSTANCE_ETL_SERVER)); + } + @Test public void testInvalidMapLines() throws SecurityException, NoSuchMethodException {