• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DataBiosphere / consent / #5659

14 Apr 2025 03:25PM UTC coverage: 79.072% (-0.07%) from 79.137%
#5659

push

web-flow
DT-1496: Prevent update self from updating institution id (#2477)

13 of 24 new or added lines in 2 files covered. (54.17%)

10292 of 13016 relevant lines covered (79.07%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.04
/src/main/java/org/broadinstitute/consent/http/models/UserUpdateFields.java
1
package org.broadinstitute.consent.http.models;
2

3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
import java.util.Objects;
7
import java.util.stream.Collectors;
8
import org.broadinstitute.consent.http.enumeration.UserFields;
9
import org.broadinstitute.consent.http.enumeration.UserRoles;
10

11
/**
12
 * This class represents the limited amount of information that is available for update from an
13
 * admin-only interface.
14
 */
15
public class UserUpdateFields {
16

17
  // We can only update non-DAC-related roles so always filter those out for addition or removal
18
  protected static final List<Integer> IGNORE_ROLE_IDS = List.of(UserRoles.CHAIRPERSON.getRoleId(),
1✔
19
      UserRoles.MEMBER.getRoleId());
1✔
20
  private static final List<Integer> VALID_ROLE_IDS = Arrays.stream(UserRoles.values())
1✔
21
      .map(UserRoles::getRoleId).toList();
1✔
22
  private String displayName;
23
  private Integer institutionId;
24
  private Boolean emailPreference;
25
  private List<Integer> userRoleIds;
26
  private String eraCommonsId;
27
  private Integer selectedSigningOfficialId;
28
  private String suggestedInstitution;
29
  private String suggestedSigningOfficial;
30
  private Boolean daaAcceptance;
31

32
  public UserUpdateFields() {
1✔
33
  }
1✔
34

35
  public String getDisplayName() {
36
    return displayName;
1✔
37
  }
38

39
  public void setDisplayName(String displayName) {
40
    this.displayName = displayName;
1✔
41
  }
1✔
42

43
  public Integer getInstitutionId() {
44
    return institutionId;
1✔
45
  }
46

47
  public void setInstitutionId(Integer institutionId) {
48
    this.institutionId = institutionId;
1✔
49
  }
1✔
50

51
  public Boolean getEmailPreference() {
52
    return emailPreference;
1✔
53
  }
54

55
  public void setEmailPreference(Boolean emailPreference) {
56
    this.emailPreference = emailPreference;
1✔
57
  }
1✔
58

59
  public List<Integer> getUserRoleIds() {
60
    return userRoleIds;
1✔
61
  }
62

63
  public void setUserRoleIds(List<Integer> userRoleIds) {
64
    this.userRoleIds = userRoleIds;
1✔
65
  }
1✔
66

67
  public String getEraCommonsId() {
68
    return eraCommonsId;
1✔
69
  }
70

71
  public void setEraCommonsId(String eraCommonsId) {
72
    this.eraCommonsId = eraCommonsId;
1✔
73
  }
1✔
74

75
  public Integer getSelectedSigningOfficialId() {
76
    return selectedSigningOfficialId;
1✔
77
  }
78

79
  public void setSelectedSigningOfficialId(Integer selectedSigningOfficialId) {
80
    this.selectedSigningOfficialId = selectedSigningOfficialId;
1✔
81
  }
1✔
82

83
  public String getSuggestedInstitution() {
84
    return suggestedInstitution;
1✔
85
  }
86

87
  public void setSuggestedInstitution(String suggestedInstitution) {
88
    this.suggestedInstitution = suggestedInstitution;
1✔
89
  }
1✔
90

91
  public String getSuggestedSigningOfficial() {
92
    return suggestedSigningOfficial;
1✔
93
  }
94

95
  public void setSuggestedSigningOfficial(String suggestedSigningOfficial) {
96
    this.suggestedSigningOfficial = suggestedSigningOfficial;
1✔
97
  }
1✔
98

99
  public Boolean getDaaAcceptance() {
100
    return daaAcceptance;
1✔
101
  }
102

103
  public void setDaaAcceptance(Boolean daaAcceptance) {
104
    this.daaAcceptance = daaAcceptance;
1✔
105
  }
1✔
106

107
  public List<UserProperty> buildUserProperties(Integer userId) {
108
    List<UserProperty> userProps = new ArrayList<>();
1✔
109
    if (Objects.nonNull(this.getSelectedSigningOfficialId())) {
1✔
110
      UserProperty prop = new UserProperty();
1✔
111
      prop.setUserId(userId);
1✔
112
      prop.setPropertyKey(UserFields.SELECTED_SIGNING_OFFICIAL_ID.getValue());
1✔
113
      prop.setPropertyValue(this.getSelectedSigningOfficialId().toString());
1✔
114
      userProps.add(prop);
1✔
115
    }
116
    if (Objects.nonNull(this.getSuggestedSigningOfficial())) {
1✔
117
      UserProperty prop = new UserProperty();
1✔
118
      prop.setUserId(userId);
1✔
119
      prop.setPropertyKey(UserFields.SUGGESTED_SIGNING_OFFICIAL.getValue());
1✔
120
      prop.setPropertyValue(this.getSuggestedSigningOfficial());
1✔
121
      userProps.add(prop);
1✔
122
    }
123
    if (Objects.nonNull(this.getSuggestedInstitution())) {
1✔
124
      UserProperty prop = new UserProperty();
1✔
125
      prop.setUserId(userId);
1✔
126
      prop.setPropertyKey(UserFields.SUGGESTED_INSTITUTION.getValue());
1✔
127
      prop.setPropertyValue(this.getSuggestedInstitution());
1✔
128
      userProps.add(prop);
1✔
129
    }
130
    if (Objects.nonNull(this.getDaaAcceptance())) {
1✔
131
      UserProperty prop = new UserProperty();
1✔
132
      prop.setUserId(userId);
1✔
133
      prop.setPropertyKey(UserFields.DAA_ACCEPTANCE.getValue());
1✔
134
      prop.setPropertyValue(this.getDaaAcceptance().toString());
1✔
135
      userProps.add(prop);
1✔
136
    }
137
    return userProps;
1✔
138
  }
139

140
  /**
141
   * Takes a list of current user roles and compares with roles that are being requested to be added
142
   * to the user. The result is a list of user roles that should be added to the user based on
143
   * allowable conditions.
144
   *
145
   * @param currentUserRoleIds List of current user role ids.
146
   * @return List of role ids that need to be added to the user.
147
   */
148
  public List<Integer> getRoleIdsToAdd(List<Integer> currentUserRoleIds) {
149
    return this.getUserRoleIds().stream()
1✔
150
        .filter(
1✔
151
            id -> {
152
              return !currentUserRoleIds.contains(id) && // Don't add any that already exist
1✔
153
                  !IGNORE_ROLE_IDS.contains(id) &&    // Never add ignorable roles
1✔
154
                  VALID_ROLE_IDS.contains(id);        // Only add roles we know about
1✔
155
            })
156
        .collect(Collectors.toList());
1✔
157
  }
158

159
  /**
160
   * Takes a list of current user roles and compares with roles that are being requested to be
161
   * removed from the user. The result is a list of user roles that should be removed from the user
162
   * based on allowable conditions.
163
   *
164
   * @param currentUserRoleIds List of current user role ids.
165
   * @return List of role ids that need to be removed from the user.
166
   */
167
  public List<Integer> getRoleIdsToRemove(List<Integer> currentUserRoleIds) {
168
    return currentUserRoleIds.stream()
1✔
169
        .filter(
1✔
170
            id -> {
171
              return !getUserRoleIds().contains(id) &&
1✔
172
                  // Remove roles that are NOT in the new role id list
173
                  !Objects.equals(id, UserRoles.RESEARCHER.getRoleId()) &&
1✔
174
                  // Never remove the researcher role
175
                  !IGNORE_ROLE_IDS.contains(id) &&
1✔
176
                  // Never remove ignorable roles
177
                  VALID_ROLE_IDS.contains(
1✔
178
                      id);                            // Only remove roles we know about
179
            })
180
        .toList();
1✔
181
  }
182

183
  @Override
184
  public boolean equals(Object o) {
185
    if (o == null || getClass() != o.getClass()) {
1✔
NEW
186
      return false;
×
187
    }
188

189
    UserUpdateFields that = (UserUpdateFields) o;
1✔
190
    return Objects.equals(displayName, that.displayName) && Objects.equals(
1✔
191
        institutionId, that.institutionId) && Objects.equals(emailPreference,
1✔
192
        that.emailPreference) && Objects.equals(userRoleIds, that.userRoleIds)
1✔
193
        && Objects.equals(eraCommonsId, that.eraCommonsId) && Objects.equals(
1✔
194
        selectedSigningOfficialId, that.selectedSigningOfficialId) && Objects.equals(
1✔
195
        suggestedInstitution, that.suggestedInstitution) && Objects.equals(
1✔
196
        suggestedSigningOfficial, that.suggestedSigningOfficial) && Objects.equals(
1✔
197
        daaAcceptance, that.daaAcceptance);
198
  }
199

200
  @Override
201
  public int hashCode() {
NEW
202
    int result = Objects.hashCode(displayName);
×
NEW
203
    result = 31 * result + Objects.hashCode(institutionId);
×
NEW
204
    result = 31 * result + Objects.hashCode(emailPreference);
×
NEW
205
    result = 31 * result + Objects.hashCode(userRoleIds);
×
NEW
206
    result = 31 * result + Objects.hashCode(eraCommonsId);
×
NEW
207
    result = 31 * result + Objects.hashCode(selectedSigningOfficialId);
×
NEW
208
    result = 31 * result + Objects.hashCode(suggestedInstitution);
×
NEW
209
    result = 31 * result + Objects.hashCode(suggestedSigningOfficial);
×
NEW
210
    result = 31 * result + Objects.hashCode(daaAcceptance);
×
NEW
211
    return result;
×
212
  }
213

214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc