UserInfoResponse.java

  1. package no.nav.data.common.security.dto;

  2. import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;

  7. import java.util.ArrayList;
  8. import java.util.List;

  9. @Data
  10. @Builder
  11. @AllArgsConstructor
  12. @NoArgsConstructor
  13. @JsonPropertyOrder({"loggedIn", "ident", "name", "email", "groups"})
  14. public class UserInfoResponse {

  15.     private boolean loggedIn;
  16.     private String ident;
  17.     private String name;
  18.     private String email;
  19.     private List<String> groups = new ArrayList<>();

  20.     public static UserInfoResponse noUser(boolean securityEnabled) {
  21.         var responseBuilder = UserInfoResponse.builder().loggedIn(false)
  22.                 .appRole(AppRole.READ);
  23.         if (!securityEnabled) {
  24.             responseBuilder
  25.                     .appRole(AppRole.WRITE)
  26.                     .appRole(AppRole.ADMIN)
  27.                     .name("Anon")
  28.                     .ident("x000000");
  29.         }
  30.         return responseBuilder.build();
  31.     }

  32.     @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
  33.     public static class UserInfoResponseBuilder {

  34.         private List<String> groups = new ArrayList<>();

  35.         public UserInfoResponseBuilder appRole(AppRole appRole) {
  36.             this.groups.add(appRole.name());
  37.             return this;
  38.         }
  39.     }
  40. }