@Test public void testListFiltering() { when(commonPermissionCheckingUtils.proceed(any(), any(), anyLong())).thenReturn( Lists.newArrayList((ResourceCrnAwareApiModel) () -> RESOURCE_CRN1, () -> RESOURCE_CRN2, () -> RESOURCE_CRN3)); Object result = underTest.checkPermissions(getAnnotation(), null, null, null, 0L); assertTrue(result instanceof List); assertEquals(2, ((List) result).size()); assertTrue(((List) result).stream().allMatch(item -> item instanceof ResourceCrnAwareApiModel)); List<String> filteredResourceCrns = ((List<ResourceCrnAwareApiModel>) result).stream() .map(resourceCrnAwareApiModel -> resourceCrnAwareApiModel.getResourceCrn()) .collect(Collectors.toList()); assertTrue(filteredResourceCrns.contains(RESOURCE_CRN1)); assertFalse(filteredResourceCrns.contains(RESOURCE_CRN3)); }
public Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn, ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime) { AuthorizationResourceAction action = methodAnnotation.action(); if (commonPermissionCheckingUtils.legacyAuthorizationNeeded()) { commonPermissionCheckingUtils.checkPermissionForUser(action, userCrn); return commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); } List<String> allResourceCrns = commonPermissionCheckingUtils.getResourceBasedCrnProvider(action).getResourceCrnsInAccount(); Set<String> filteredResourceCrns = commonPermissionCheckingUtils.getPermissionsForUserOnResources(action, userCrn, allResourceCrns) .entrySet() .stream() .filter(Map.Entry::getValue) .map(Map.Entry::getKey) .collect(Collectors.toSet()); Object result = commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); switch (ListResponseFilteringType.getByClass(result.getClass())) { case SET: return filterSet(filteredResourceCrns, (Set) result); case LIST: return filterList(filteredResourceCrns, (List) result); case FILTERABLE_RESPONSE_MODEL: return filterAuthorizationFilterableResponse(filteredResourceCrns, (AuthorizationFilterableResponseCollection) result); case UNSUPPORTED: default: throw new IllegalStateException("Response of list API should be List, Set or an instance of " + "AuthorizationFilterableResponseCollection interface"); } }
ListPermissionChecker { public Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn, ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime) { AuthorizationResourceAction action = methodAnnotation.action(); if (commonPermissionCheckingUtils.legacyAuthorizationNeeded()) { commonPermissionCheckingUtils.checkPermissionForUser(action, userCrn); return commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); } List<String> allResourceCrns = commonPermissionCheckingUtils.getResourceBasedCrnProvider(action).getResourceCrnsInAccount(); Set<String> filteredResourceCrns = commonPermissionCheckingUtils.getPermissionsForUserOnResources(action, userCrn, allResourceCrns) .entrySet() .stream() .filter(Map.Entry::getValue) .map(Map.Entry::getKey) .collect(Collectors.toSet()); Object result = commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); switch (ListResponseFilteringType.getByClass(result.getClass())) { case SET: return filterSet(filteredResourceCrns, (Set) result); case LIST: return filterList(filteredResourceCrns, (List) result); case FILTERABLE_RESPONSE_MODEL: return filterAuthorizationFilterableResponse(filteredResourceCrns, (AuthorizationFilterableResponseCollection) result); case UNSUPPORTED: default: throw new IllegalStateException("Response of list API should be List, Set or an instance of " + "AuthorizationFilterableResponseCollection interface"); } } }
ListPermissionChecker { public Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn, ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime) { AuthorizationResourceAction action = methodAnnotation.action(); if (commonPermissionCheckingUtils.legacyAuthorizationNeeded()) { commonPermissionCheckingUtils.checkPermissionForUser(action, userCrn); return commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); } List<String> allResourceCrns = commonPermissionCheckingUtils.getResourceBasedCrnProvider(action).getResourceCrnsInAccount(); Set<String> filteredResourceCrns = commonPermissionCheckingUtils.getPermissionsForUserOnResources(action, userCrn, allResourceCrns) .entrySet() .stream() .filter(Map.Entry::getValue) .map(Map.Entry::getKey) .collect(Collectors.toSet()); Object result = commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); switch (ListResponseFilteringType.getByClass(result.getClass())) { case SET: return filterSet(filteredResourceCrns, (Set) result); case LIST: return filterList(filteredResourceCrns, (List) result); case FILTERABLE_RESPONSE_MODEL: return filterAuthorizationFilterableResponse(filteredResourceCrns, (AuthorizationFilterableResponseCollection) result); case UNSUPPORTED: default: throw new IllegalStateException("Response of list API should be List, Set or an instance of " + "AuthorizationFilterableResponseCollection interface"); } }  }
ListPermissionChecker { public Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn, ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime) { AuthorizationResourceAction action = methodAnnotation.action(); if (commonPermissionCheckingUtils.legacyAuthorizationNeeded()) { commonPermissionCheckingUtils.checkPermissionForUser(action, userCrn); return commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); } List<String> allResourceCrns = commonPermissionCheckingUtils.getResourceBasedCrnProvider(action).getResourceCrnsInAccount(); Set<String> filteredResourceCrns = commonPermissionCheckingUtils.getPermissionsForUserOnResources(action, userCrn, allResourceCrns) .entrySet() .stream() .filter(Map.Entry::getValue) .map(Map.Entry::getKey) .collect(Collectors.toSet()); Object result = commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); switch (ListResponseFilteringType.getByClass(result.getClass())) { case SET: return filterSet(filteredResourceCrns, (Set) result); case LIST: return filterList(filteredResourceCrns, (List) result); case FILTERABLE_RESPONSE_MODEL: return filterAuthorizationFilterableResponse(filteredResourceCrns, (AuthorizationFilterableResponseCollection) result); case UNSUPPORTED: default: throw new IllegalStateException("Response of list API should be List, Set or an instance of " + "AuthorizationFilterableResponseCollection interface"); } }  Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn,
            ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime); }
ListPermissionChecker { public Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn, ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime) { AuthorizationResourceAction action = methodAnnotation.action(); if (commonPermissionCheckingUtils.legacyAuthorizationNeeded()) { commonPermissionCheckingUtils.checkPermissionForUser(action, userCrn); return commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); } List<String> allResourceCrns = commonPermissionCheckingUtils.getResourceBasedCrnProvider(action).getResourceCrnsInAccount(); Set<String> filteredResourceCrns = commonPermissionCheckingUtils.getPermissionsForUserOnResources(action, userCrn, allResourceCrns) .entrySet() .stream() .filter(Map.Entry::getValue) .map(Map.Entry::getKey) .collect(Collectors.toSet()); Object result = commonPermissionCheckingUtils.proceed(proceedingJoinPoint, methodSignature, startTime); switch (ListResponseFilteringType.getByClass(result.getClass())) { case SET: return filterSet(filteredResourceCrns, (Set) result); case LIST: return filterList(filteredResourceCrns, (List) result); case FILTERABLE_RESPONSE_MODEL: return filterAuthorizationFilterableResponse(filteredResourceCrns, (AuthorizationFilterableResponseCollection) result); case UNSUPPORTED: default: throw new IllegalStateException("Response of list API should be List, Set or an instance of " + "AuthorizationFilterableResponseCollection interface"); } }  Object checkPermissions(FilterListBasedOnPermissions methodAnnotation, String userCrn,
            ProceedingJoinPoint proceedingJoinPoint, MethodSignature methodSignature, long startTime);  }