PermissionsGroupVisibilityPlugin extends GroupVisibilityPlugin { public boolean hasPermission(Identity userIdentity, Group group) { Collection<MembershipEntry> userMemberships = userIdentity.getMemberships(); return userACL.getSuperUser().equals(userIdentity.getUserId()) || userMemberships.stream() .anyMatch(userMembership -> userMembership.getGroup().equals(userACL.getAdminGroups()) || ((userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/")) && (group.getId().equals("/spaces") || group.getId().startsWith("/spaces/") || userMembership.getMembershipType().equals("*") || userMembership.getMembershipType().equals("manager")))); } PermissionsGroupVisibilityPlugin(UserACL userACL); boolean hasPermission(Identity userIdentity, Group group);  }
@Test public void shouldHasPermissionWhenUserIsSuperUser() { UserACL userACL = mock(UserACL.class); when(userACL.getSuperUser()).thenReturn("john"); GroupVisibilityPlugin plugin = new PermissionsGroupVisibilityPlugin(userACL); Identity userIdentity = new Identity("john", Arrays.asList(new MembershipEntry("/platform/users", "manager"))); Group groupPlatform = new GroupImpl(); groupPlatform.setId("/platform"); Group groupPlatformUsers = new GroupImpl(); groupPlatformUsers.setId("/platform/users"); boolean hasPermissionOnPlatform = plugin.hasPermission(userIdentity, groupPlatform); boolean hasPermissionOnPlatformUsers = plugin.hasPermission(userIdentity, groupPlatformUsers); assertTrue(hasPermissionOnPlatform); assertTrue(hasPermissionOnPlatformUsers); }
@Test public void shouldHasPermissionWhenUserIsPlatformAdministrator() { UserACL userACL = mock(UserACL.class); when(userACL.getSuperUser()).thenReturn("root"); when(userACL.getAdminGroups()).thenReturn("/platform/administrators"); GroupVisibilityPlugin plugin = new PermissionsGroupVisibilityPlugin(userACL); Identity userIdentity = new Identity("john", Arrays.asList(new MembershipEntry("/platform/administrators", "manager"))); Group groupPlatform = new GroupImpl(); groupPlatform.setId("/platform"); Group groupPlatformUsers = new GroupImpl(); groupPlatformUsers.setId("/platform/users"); boolean hasPermissionOnPlatform = plugin.hasPermission(userIdentity, groupPlatform); boolean hasPermissionOnPlatformUsers = plugin.hasPermission(userIdentity, groupPlatformUsers); assertTrue(hasPermissionOnPlatform); assertTrue(hasPermissionOnPlatformUsers); }
@Test public void shouldHasPermissionWhenUserIsInGivenGroup() { UserACL userACL = mock(UserACL.class); when(userACL.getSuperUser()).thenReturn("root"); when(userACL.getAdminGroups()).thenReturn("/platform/administrators"); GroupVisibilityPlugin plugin = new PermissionsGroupVisibilityPlugin(userACL); Identity userIdentity = new Identity("john", Arrays.asList(new MembershipEntry("/platform/developers", "manager"), new MembershipEntry("/platform/testers", "member"), new MembershipEntry("/spaces/marketing", "member"), new MembershipEntry("/spaces/sales", "manager"), new MembershipEntry("/organization/rh", "*"))); Group groupPlatform = new GroupImpl(); groupPlatform.setId("/platform"); Group groupPlatformDevelopers = new GroupImpl(); groupPlatformDevelopers.setId("/platform/developers"); Group groupPlatformTesters = new GroupImpl(); groupPlatformTesters.setId("/platform/testers"); Group groupSpaces = new GroupImpl(); groupSpaces.setId("/spaces"); Group groupSpacesMarketing = new GroupImpl(); groupSpacesMarketing.setId("/spaces/marketing"); Group groupSpacesSales = new GroupImpl(); groupSpacesSales.setId("/spaces/sales"); Group groupSpacesEngineering = new GroupImpl(); groupSpacesEngineering.setId("/spaces/engineering"); Group groupOrganization = new GroupImpl(); groupOrganization.setId("/organization"); Group groupOrganizationRh = new GroupImpl(); groupOrganizationRh.setId("/organization/rh"); boolean hasPermissionOnPlatform = plugin.hasPermission(userIdentity, groupPlatform); boolean hasPermissionOnPlatformDevelopers = plugin.hasPermission(userIdentity, groupPlatformDevelopers); boolean hasPermissionOnPlatformTesters = plugin.hasPermission(userIdentity, groupPlatformTesters); boolean hasPermissionOnSpaces = plugin.hasPermission(userIdentity, groupSpaces); boolean hasPermissionOnSpacesMarketing = plugin.hasPermission(userIdentity, groupSpacesMarketing); boolean hasPermissionOnSpacesSales = plugin.hasPermission(userIdentity, groupSpacesSales); boolean hasPermissionOnSpacesEngineering = plugin.hasPermission(userIdentity, groupSpacesEngineering); boolean hasPermissionOnOrganization = plugin.hasPermission(userIdentity, groupOrganization); boolean hasPermissionOnOrganizationRh = plugin.hasPermission(userIdentity, groupOrganizationRh); assertTrue(hasPermissionOnPlatform); assertTrue(hasPermissionOnPlatformDevelopers); assertFalse(hasPermissionOnPlatformTesters); assertTrue(hasPermissionOnSpaces); assertTrue(hasPermissionOnSpacesMarketing); assertTrue(hasPermissionOnSpacesSales); assertFalse(hasPermissionOnSpacesEngineering); assertTrue(hasPermissionOnOrganization); assertTrue(hasPermissionOnOrganizationRh); }
@Test public void shouldHasPermissionWhenUserIsOnlyMemberOfSpaces() { UserACL userACL = mock(UserACL.class); when(userACL.getSuperUser()).thenReturn("root"); when(userACL.getAdminGroups()).thenReturn("/platform/administrators"); GroupVisibilityPlugin plugin = new PermissionsGroupVisibilityPlugin(userACL); Identity userIdentity = new Identity("john", Arrays.asList(new MembershipEntry("/spaces/marketing", "member"), new MembershipEntry("/spaces/sales", "redactor"))); Group groupSpaces = new GroupImpl(); groupSpaces.setId("/spaces"); Group groupSpacesMarketing = new GroupImpl(); groupSpacesMarketing.setId("/spaces/marketing"); Group groupSpacesSales = new GroupImpl(); groupSpacesSales.setId("/spaces/sales"); Group groupSpacesEngineering = new GroupImpl(); groupSpacesEngineering.setId("/spaces/engineering"); boolean hasPermissionOnSpaces = plugin.hasPermission(userIdentity, groupSpaces); boolean hasPermissionOnSpacesMarketing = plugin.hasPermission(userIdentity, groupSpacesMarketing); boolean hasPermissionOnSpacesSales = plugin.hasPermission(userIdentity, groupSpacesSales); boolean hasPermissionOnSpacesEngineering = plugin.hasPermission(userIdentity, groupSpacesEngineering); assertTrue(hasPermissionOnSpaces); assertTrue(hasPermissionOnSpacesMarketing); assertTrue(hasPermissionOnSpacesSales); assertFalse(hasPermissionOnSpacesEngineering); }