Skip to content

Commit

Permalink
feat: Add option to exclude ALL auth from check
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mackessy committed Dec 18, 2024
1 parent 72a1c4e commit ffdf3be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
* Annotation that takes in any number of {@link Authorities}. This allows us to check if a {@link
* org.hisp.dhis.user.User} has any of the {@link Authorities} passed in.
*
* <p>{@link Authorities#ALL} is automatically added to the check, as having this Authority allows
* access to all methods by default. No need to pass {@link Authorities#ALL} in the arguments. See
* {@link AuthorityInterceptor}.
* <p>{@link Authorities#ALL} is automatically added to the check by default, as having this
* Authority allows access to all methods by default. No need to pass {@link Authorities#ALL} in the
* arguments. See {@link AuthorityInterceptor}. <br>
* {@link Authorities#ALL} will only be excluded from the check if explicitly requested, using the
* optional param `excludeAllAuth=true`.
*
* <p>Can be used at Class or Method level. Usage at the method level will always take precedence
* (matching how Spring works). Class level usage only applies if there is no usage at the method
Expand All @@ -50,4 +52,6 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresAuthority {
Authorities[] anyOf();

boolean excludeAllAuth() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
* the passed-in {@link org.hisp.dhis.security.Authorities}. The exception message includes the
* required {@link org.hisp.dhis.security.Authorities} for the endpoint.
*
* <p>{@link Authorities#ALL} is automatically added to the check, as having this Authority allows
* access to all methods by default.
* <p>{@link Authorities#ALL} is only excluded if explicitly requested. The default includes {@link
* Authorities#ALL} in the check, as this is how the system operates.
*/
@Component
public class AuthorityInterceptor implements HandlerInterceptor {
Expand Down Expand Up @@ -88,7 +88,7 @@ public boolean preHandle(
return checkForRequiredAuthority(requiresMethodAuthority);
}

// heck if RequiresAuthority is at method level
// heck if RequiresAuthority is at class level
if (handlerMethod.getBeanType().isAnnotationPresent(RequiresAuthority.class)) {
RequiresAuthority requiresClassAuthority =
handlerMethod.getBeanType().getAnnotation(RequiresAuthority.class);
Expand All @@ -98,8 +98,11 @@ public boolean preHandle(
}

private boolean checkForRequiredAuthority(RequiresAuthority requiresAuthority) {
// include 'ALL' authority in required authorities
List<Authorities> requiredAuthorities = new ArrayList<>(List.of(Authorities.ALL));
List<Authorities> requiredAuthorities = new ArrayList<>();

// include 'ALL' authority in required authorities if not excluded
if (!requiresAuthority.excludeAllAuth()) requiredAuthorities.add(Authorities.ALL);

requiredAuthorities.addAll(List.of(requiresAuthority.anyOf()));

// get user authorities
Expand Down

0 comments on commit ffdf3be

Please sign in to comment.