FreeResourcesLimitServicePermissionsFilter extends CheMethodInvokerFilter { @Override protected void filter(GenericResourceMethod genericMethodResource, Object[] arguments) throws ApiException { switch (genericMethodResource.getMethod().getName()) { case STORE_FREE_RESOURCES_LIMIT_METHOD: case GET_FREE_RESOURCES_LIMITS_METHOD: case GET_FREE_RESOURCES_LIMIT_METHOD: case REMOVE_FREE_RESOURCES_LIMIT_METHOD: final Subject currentSubject = EnvironmentContext.getCurrent().getSubject(); if (currentSubject.hasPermission( SystemDomain.DOMAIN_ID, null, SystemDomain.MANAGE_SYSTEM_ACTION)) { return; } default: throw new ForbiddenException("The user does not have permission to perform this operation"); } }    }
@Test public void shouldTestThatAllPublicMethodsAreCoveredByPermissionsFilter() throws Exception { final List<String> collect = Stream.of(FreeResourcesLimitService.class.getDeclaredMethods()) .filter(method -> Modifier.isPublic(method.getModifiers())) .map(Method::getName) .collect(Collectors.toList()); assertEquals(collect.size(), 4); assertTrue( collect.contains( FreeResourcesLimitServicePermissionsFilter.STORE_FREE_RESOURCES_LIMIT_METHOD)); assertTrue( collect.contains( FreeResourcesLimitServicePermissionsFilter.GET_FREE_RESOURCES_LIMITS_METHOD)); assertTrue( collect.contains( FreeResourcesLimitServicePermissionsFilter.GET_FREE_RESOURCES_LIMIT_METHOD)); assertTrue( collect.contains( FreeResourcesLimitServicePermissionsFilter.REMOVE_FREE_RESOURCES_LIMIT_METHOD)); }
@Test( expectedExceptions = ForbiddenException.class, expectedExceptionsMessageRegExp = "The user does not have permission to perform this operation") public void shouldThrowForbiddenExceptionWhenRequestedUnknownMethod() throws Exception { final GenericResourceMethod mock = mock(GenericResourceMethod.class); Method unknownMethod = FreeResourcesLimitService.class.getMethod("getServiceDescriptor"); when(mock.getMethod()).thenReturn(unknownMethod); filter.filter(mock, new Object[] {}); }