NamespaceSecurityHelper { public Set<String> getAuthorizedNamespaces(NamespacePermissionEnum... permissions) { Set<String> namespaces = new HashSet<>(); if (SecurityContextHolder.getContext().getAuthentication() != null) { ApplicationUser applicationUser = getApplicationUser(); if (applicationUser != null) { for (NamespaceAuthorization namespaceAuthorization : applicationUser.getNamespaceAuthorizations()) { if (namespaceAuthorization.getNamespacePermissions().containsAll(Arrays.asList(permissions))) { namespaces.add(namespaceAuthorization.getNamespace()); } } } } return namespaces; }  void checkPermission(Object object, NamespacePermissionEnum[] permissions); void checkPermission(String namespace, NamespacePermissionEnum[] permissions); AccessDeniedException getAccessDeniedException(List<AccessDeniedException> accessDeniedExceptions); Set<String> getAuthorizedNamespaces(NamespacePermissionEnum... permissions);  }
@Test public void getAuthorizedNamespacesWhenUserHasPermissionAssertReturnNamespace() { ApplicationUser applicationUser = new ApplicationUser(getClass()); applicationUser .setNamespaceAuthorizations(new HashSet<>(Arrays.asList(new NamespaceAuthorization("namespace", Arrays.asList(NamespacePermissionEnum.READ))))); SecurityContextHolder.getContext().setAuthentication( new TestingAuthenticationToken(new SecurityUserWrapper("username", "", true, true, true, true, Collections.emptyList(), applicationUser), null)); Set<String> authorizedNamespaces = namespaceSecurityHelper.getAuthorizedNamespaces(NamespacePermissionEnum.READ); assertEquals(1, authorizedNamespaces.size()); assertTrue(authorizedNamespaces.contains("namespace")); }
@Test public void getAuthorizedNamespacesWhenUserHasNoPermissionAssertReturnEmpty() { ApplicationUser applicationUser = new ApplicationUser(getClass()); applicationUser .setNamespaceAuthorizations(new HashSet<>(Arrays.asList(new NamespaceAuthorization("namespace", Arrays.asList(NamespacePermissionEnum.WRITE))))); SecurityContextHolder.getContext().setAuthentication( new TestingAuthenticationToken(new SecurityUserWrapper("username", "", true, true, true, true, Collections.emptyList(), applicationUser), null)); Set<String> authorizedNamespaces = namespaceSecurityHelper.getAuthorizedNamespaces(NamespacePermissionEnum.READ); assertEquals(0, authorizedNamespaces.size()); }
@Test public void getAuthorizedNamespacesWhenNoApplicationUserInContextReturnEmpty() { SecurityContextHolder.getContext().setAuthentication( new TestingAuthenticationToken(new SecurityUserWrapper("username", "", true, true, true, true, Collections.emptyList(), null), null)); Set<String> authorizedNamespaces = namespaceSecurityHelper.getAuthorizedNamespaces(NamespacePermissionEnum.READ); assertEquals(0, authorizedNamespaces.size()); }
@Test public void getAuthorizedNamespacesWhenNoAuthenticationInContextReturnEmpty() { SecurityContextHolder.clearContext(); Set<String> authorizedNamespaces = namespaceSecurityHelper.getAuthorizedNamespaces(NamespacePermissionEnum.READ); assertEquals(0, authorizedNamespaces.size()); }