@Test public void shouldHasPermissionWhenUserIsSuperUser() { UserACL userACL = mock(UserACL.class); when(userACL.getSuperUser()).thenReturn("john"); SpacesAdministrationService spacesAdministrationService = mock(SpacesAdministrationService.class); when(spacesAdministrationService.getSpacesAdministratorsMemberships()).thenReturn(Arrays.asList(new MembershipEntry("/platform/spacesadmins", "*"))); GroupVisibilityPlugin plugin = new SpaceCreationInvitationGroupVisibilityPlugin(userACL, spacesAdministrationService); 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); }
@Override public boolean hasPermission(Identity userIdentity, Group group) { if (userACL.getSuperUser().equals(userIdentity.getUserId()) || userIdentity.getMemberships() .stream() .anyMatch(userMembership -> userMembership.getGroup().equals(userACL.getAdminGroups()))) { return true; } Collection<MembershipEntry> userMemberships = userIdentity.getMemberships(); if (spacesAdministratorsGroups == null) { spacesAdministratorsGroups = spacesAdministrationService.getSpacesAdministratorsMemberships() .stream() .map(membershipEntry -> membershipEntry.getGroup()) .collect(Collectors.toList()); } return userMemberships.stream() .anyMatch(userMembership -> ((group.getId().startsWith("/spaces/") || group.getId().equals("/spaces")) && (spacesAdministratorsGroups.contains(userMembership.getGroup()) || userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/"))) || ((userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/")) && (userMembership.getMembershipType().equals(ANY) || userMembership.getMembershipType().equals(MANAGER)))); }
SpaceCreationInvitationGroupVisibilityPlugin extends GroupVisibilityPlugin { @Override public boolean hasPermission(Identity userIdentity, Group group) { if (userACL.getSuperUser().equals(userIdentity.getUserId()) || userIdentity.getMemberships() .stream() .anyMatch(userMembership -> userMembership.getGroup().equals(userACL.getAdminGroups()))) { return true; } Collection<MembershipEntry> userMemberships = userIdentity.getMemberships(); if (spacesAdministratorsGroups == null) { spacesAdministratorsGroups = spacesAdministrationService.getSpacesAdministratorsMemberships() .stream() .map(membershipEntry -> membershipEntry.getGroup()) .collect(Collectors.toList()); } return userMemberships.stream() .anyMatch(userMembership -> ((group.getId().startsWith("/spaces/") || group.getId().equals("/spaces")) && (spacesAdministratorsGroups.contains(userMembership.getGroup()) || userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/"))) || ((userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/")) && (userMembership.getMembershipType().equals(ANY) || userMembership.getMembershipType().equals(MANAGER)))); } }
SpaceCreationInvitationGroupVisibilityPlugin extends GroupVisibilityPlugin { @Override public boolean hasPermission(Identity userIdentity, Group group) { if (userACL.getSuperUser().equals(userIdentity.getUserId()) || userIdentity.getMemberships() .stream() .anyMatch(userMembership -> userMembership.getGroup().equals(userACL.getAdminGroups()))) { return true; } Collection<MembershipEntry> userMemberships = userIdentity.getMemberships(); if (spacesAdministratorsGroups == null) { spacesAdministratorsGroups = spacesAdministrationService.getSpacesAdministratorsMemberships() .stream() .map(membershipEntry -> membershipEntry.getGroup()) .collect(Collectors.toList()); } return userMemberships.stream() .anyMatch(userMembership -> ((group.getId().startsWith("/spaces/") || group.getId().equals("/spaces")) && (spacesAdministratorsGroups.contains(userMembership.getGroup()) || userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/"))) || ((userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/")) && (userMembership.getMembershipType().equals(ANY) || userMembership.getMembershipType().equals(MANAGER)))); } SpaceCreationInvitationGroupVisibilityPlugin(UserACL userACL, SpacesAdministrationService spacesAdministrationService); }
SpaceCreationInvitationGroupVisibilityPlugin extends GroupVisibilityPlugin { @Override public boolean hasPermission(Identity userIdentity, Group group) { if (userACL.getSuperUser().equals(userIdentity.getUserId()) || userIdentity.getMemberships() .stream() .anyMatch(userMembership -> userMembership.getGroup().equals(userACL.getAdminGroups()))) { return true; } Collection<MembershipEntry> userMemberships = userIdentity.getMemberships(); if (spacesAdministratorsGroups == null) { spacesAdministratorsGroups = spacesAdministrationService.getSpacesAdministratorsMemberships() .stream() .map(membershipEntry -> membershipEntry.getGroup()) .collect(Collectors.toList()); } return userMemberships.stream() .anyMatch(userMembership -> ((group.getId().startsWith("/spaces/") || group.getId().equals("/spaces")) && (spacesAdministratorsGroups.contains(userMembership.getGroup()) || userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/"))) || ((userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/")) && (userMembership.getMembershipType().equals(ANY) || userMembership.getMembershipType().equals(MANAGER)))); } SpaceCreationInvitationGroupVisibilityPlugin(UserACL userACL, SpacesAdministrationService spacesAdministrationService); @Override boolean hasPermission(Identity userIdentity, Group group); }
SpaceCreationInvitationGroupVisibilityPlugin extends GroupVisibilityPlugin { @Override public boolean hasPermission(Identity userIdentity, Group group) { if (userACL.getSuperUser().equals(userIdentity.getUserId()) || userIdentity.getMemberships() .stream() .anyMatch(userMembership -> userMembership.getGroup().equals(userACL.getAdminGroups()))) { return true; } Collection<MembershipEntry> userMemberships = userIdentity.getMemberships(); if (spacesAdministratorsGroups == null) { spacesAdministratorsGroups = spacesAdministrationService.getSpacesAdministratorsMemberships() .stream() .map(membershipEntry -> membershipEntry.getGroup()) .collect(Collectors.toList()); } return userMemberships.stream() .anyMatch(userMembership -> ((group.getId().startsWith("/spaces/") || group.getId().equals("/spaces")) && (spacesAdministratorsGroups.contains(userMembership.getGroup()) || userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/"))) || ((userMembership.getGroup().equals(group.getId()) || userMembership.getGroup().startsWith(group.getId() + "/")) && (userMembership.getMembershipType().equals(ANY) || userMembership.getMembershipType().equals(MANAGER)))); } SpaceCreationInvitationGroupVisibilityPlugin(UserACL userACL, SpacesAdministrationService spacesAdministrationService); @Override boolean hasPermission(Identity userIdentity, Group group); static final String MANAGER; static final String ANY; }