Skip to content

Commit

Permalink
[DUOS-3021][risk=no] Handle null roles better (#2279)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong authored Apr 4, 2024
1 parent 898700f commit 0f28492
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import java.util.Objects;
import org.broadinstitute.consent.http.enumeration.UserRoles;
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.models.UserRole;

public class InstitutionUtil {
public class InstitutionUtil implements ConsentLogger {

private final GsonBuilder gson;

Expand All @@ -28,8 +29,12 @@ public Gson getGsonBuilder(Boolean isAdmin) {

public Boolean checkIfAdmin(User user) {
List<UserRole> roles = user.getRoles();
if (roles == null || roles.isEmpty()) {
logWarn("User has no roles: " + user.getEmail());
return false;
}
return roles.stream()
.anyMatch((userRole) -> userRole.getRoleId() == UserRoles.ADMIN.getRoleId());
.anyMatch((userRole) -> Objects.equals(userRole.getRoleId(), UserRoles.ADMIN.getRoleId()));
}

private ExclusionStrategy getSerializationExclusionStrategy(Boolean isAdmin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ void setUp() {
}

@Test
void testCheckIfAdmin() {
Boolean adminResult = util.checkIfAdmin(adminUser);
Boolean researcherResult = util.checkIfAdmin(researcherUser);
assertTrue(adminResult);
assertFalse(researcherResult);
void testCheckIfAdminAdmin() {
assertTrue(util.checkIfAdmin(adminUser));
assertFalse(util.checkIfAdmin(researcherUser));
assertFalse(util.checkIfAdmin(new User()));
}

@Test
Expand Down

0 comments on commit 0f28492

Please sign in to comment.