@Test public void test_or_permission_ok() throws Throwable { Mockito.when(subjectUnderTest.hasRole("CODE")).thenReturn(true); Mockito.when(subjectUnderTest.hasRole("EAT")).thenReturn(false); underTest = new RequiresRolesInterceptor(); MethodInvocation methodInvocation = Mockito.mock(MethodInvocation.class); when(methodInvocation.getMethod()) .thenReturn(RequiresRolesInterceptorTest.class.getMethod("securedOrMethod")); underTest.invoke(methodInvocation); }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Optional<RequiresRoles> annotation = findAnnotation(invocation); if (annotation.isPresent()) { RequiresRoles rrAnnotation = annotation.get(); String[] roles = rrAnnotation.value(); if (roles.length == 1) { checkRole(roles[0]); } else { boolean isAllowed = hasRoles(roles, rrAnnotation.logical()); if (!isAllowed) { if (Logical.OR.equals(rrAnnotation.logical())) { throw new AuthorizationException( "User does not have any of the roles to access method " + invocation.getMethod() .toString()); } else { throw new AuthorizationException("Subject doesn't have roles " + Arrays.toString(roles)); } } } } return invocation.proceed(); }
RequiresRolesInterceptor extends AbstractInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Optional<RequiresRoles> annotation = findAnnotation(invocation); if (annotation.isPresent()) { RequiresRoles rrAnnotation = annotation.get(); String[] roles = rrAnnotation.value(); if (roles.length == 1) { checkRole(roles[0]); } else { boolean isAllowed = hasRoles(roles, rrAnnotation.logical()); if (!isAllowed) { if (Logical.OR.equals(rrAnnotation.logical())) { throw new AuthorizationException( "User does not have any of the roles to access method " + invocation.getMethod() .toString()); } else { throw new AuthorizationException("Subject doesn't have roles " + Arrays.toString(roles)); } } } } return invocation.proceed(); } }
RequiresRolesInterceptor extends AbstractInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Optional<RequiresRoles> annotation = findAnnotation(invocation); if (annotation.isPresent()) { RequiresRoles rrAnnotation = annotation.get(); String[] roles = rrAnnotation.value(); if (roles.length == 1) { checkRole(roles[0]); } else { boolean isAllowed = hasRoles(roles, rrAnnotation.logical()); if (!isAllowed) { if (Logical.OR.equals(rrAnnotation.logical())) { throw new AuthorizationException( "User does not have any of the roles to access method " + invocation.getMethod() .toString()); } else { throw new AuthorizationException("Subject doesn't have roles " + Arrays.toString(roles)); } } } } return invocation.proceed(); }  }
RequiresRolesInterceptor extends AbstractInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Optional<RequiresRoles> annotation = findAnnotation(invocation); if (annotation.isPresent()) { RequiresRoles rrAnnotation = annotation.get(); String[] roles = rrAnnotation.value(); if (roles.length == 1) { checkRole(roles[0]); } else { boolean isAllowed = hasRoles(roles, rrAnnotation.logical()); if (!isAllowed) { if (Logical.OR.equals(rrAnnotation.logical())) { throw new AuthorizationException( "User does not have any of the roles to access method " + invocation.getMethod() .toString()); } else { throw new AuthorizationException("Subject doesn't have roles " + Arrays.toString(roles)); } } } } return invocation.proceed(); }  @Override Object invoke(MethodInvocation invocation); }
RequiresRolesInterceptor extends AbstractInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Optional<RequiresRoles> annotation = findAnnotation(invocation); if (annotation.isPresent()) { RequiresRoles rrAnnotation = annotation.get(); String[] roles = rrAnnotation.value(); if (roles.length == 1) { checkRole(roles[0]); } else { boolean isAllowed = hasRoles(roles, rrAnnotation.logical()); if (!isAllowed) { if (Logical.OR.equals(rrAnnotation.logical())) { throw new AuthorizationException( "User does not have any of the roles to access method " + invocation.getMethod() .toString()); } else { throw new AuthorizationException("Subject doesn't have roles " + Arrays.toString(roles)); } } } } return invocation.proceed(); }  @Override Object invoke(MethodInvocation invocation);  }