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

DataBiosphere / consent / #5119

24 May 2024 05:31PM UTC coverage: 76.36% (+0.08%) from 76.282%
#5119

push

web-flow
[DCJ-49][DCJ-50][risk=no] New APIs for DAA Enforced DAR Create/Update (#2330)

Co-authored-by: fboulnois <fboulnois@users.noreply.github.com>

88 of 93 new or added lines in 6 files covered. (94.62%)

1 existing line in 1 file now uncovered.

9739 of 12754 relevant lines covered (76.36%)

0.76 hits per line

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

95.33
/src/main/java/org/broadinstitute/consent/http/models/User.java
1
package org.broadinstitute.consent.http.models;
2

3
import com.fasterxml.jackson.annotation.JsonProperty;
4
import com.google.gson.Gson;
5
import com.google.gson.JsonObject;
6
import java.beans.Transient;
7
import java.util.ArrayList;
8
import java.util.Arrays;
9
import java.util.Date;
10
import java.util.EnumSet;
11
import java.util.List;
12
import java.util.Objects;
13
import java.util.stream.Collectors;
14
import net.gcardone.junidecode.Junidecode;
15
import org.apache.commons.collections4.CollectionUtils;
16
import org.apache.commons.lang3.StringUtils;
17
import org.apache.commons.lang3.builder.EqualsBuilder;
18
import org.broadinstitute.consent.http.enumeration.UserRoles;
19
import org.broadinstitute.consent.http.util.gson.GsonUtil;
20

21
public class User {
22

23
  public static final String QUERY_FIELDS_WITH_U_PREFIX =
24
      " u.user_id as u_user_id, " +
25
          " u.email as u_email, " +
26
          " u.display_name as u_display_name, " +
27
          " u.create_date as u_create_date, " +
28
          " u.email_preference as u_email_preference, " +
29
          " u.institution_id as u_institution_id," +
30
          " u.era_commons_id as u_era_commons_id ";
31

32
  @JsonProperty
33
  private Integer userId;
34

35
  @JsonProperty
36
  private String email;
37

38
  @JsonProperty
39
  private String displayName;
40

41
  @JsonProperty
42
  private Date createDate;
43

44
  @JsonProperty
45
  private List<UserRole> roles;
46

47
  @JsonProperty
48
  private List<UserProperty> properties;
49

50
  @JsonProperty
51
  private Boolean emailPreference;
52

53
  @JsonProperty
54
  private Integer institutionId;
55

56
  @JsonProperty
57
  private String eraCommonsId;
58

59
  private Institution institution;
60

61
  private List<LibraryCard> libraryCards;
62

63
  public User() {
1✔
64
  }
1✔
65

66
  public Integer getUserId() {
67
    return userId;
1✔
68
  }
69

70
  public void setUserId(Integer userId) {
71
    this.userId = userId;
1✔
72
  }
1✔
73

74
  public User(Integer userId, String email, String displayName, Date createDate) {
1✔
75
    this.userId = userId;
1✔
76
    this.email = email;
1✔
77
    this.displayName = displayName;
1✔
78
    this.createDate = createDate;
1✔
79
  }
1✔
80

81
  public User(Integer userId, String email, String displayName, Date createDate,
82
      List<UserRole> roles) {
1✔
83
    this.userId = userId;
1✔
84
    this.email = email;
1✔
85
    this.displayName = displayName;
1✔
86
    this.createDate = createDate;
1✔
87
    this.roles = roles;
1✔
88
  }
1✔
89

90
  /**
91
   * Convenience method for backwards compatibility support for older clients. This method is
92
   * intended to reconstruct a User object from a supplied JSON string for primary fields and roles.
93
   * Other associated objects are not intended to be parsed using this method since it comes from
94
   * user-supplied data and may conflict with what the system knows about those associations.
95
   *
96
   * @param json A json string that may or may not be correctly structured as a DACUser
97
   */
98
  public User(String json) {
1✔
99
    Gson gson = GsonUtil.getInstance();
1✔
100
    JsonObject userJsonObject = gson.fromJson(json, JsonObject.class);
1✔
101
    // There are no cases where we want to pull the create date/update date from user-provided data.
102
    // Nor do we need to retrieve the full institution object from user-provided data.
103
    JsonObject filteredUserJsonObject = filterFields(
1✔
104
        userJsonObject,
105
        Arrays.asList("createDate", "institution", "libraryCards"));
1✔
106
    User u = gson.fromJson(filteredUserJsonObject.toString(), User.class);
1✔
107
    setUserId(u);
1✔
108
    setEmail(u);
1✔
109
    setDisplayName(u);
1✔
110
    setEmailPreference(u);
1✔
111
    setRoles(u);
1✔
112
    setInstitutionId(u);
1✔
113
  }
1✔
114

115
  /**
116
   * Private method to filter out fields that we do not want to parse from json objects.
117
   *
118
   * @param obj    The json object
119
   * @param fields The fields to remove
120
   * @return Filtered Clone of the object.
121
   */
122
  private JsonObject filterFields(JsonObject obj, List<String> fields) {
123
    JsonObject copy = obj.deepCopy();
1✔
124
    fields.forEach(f -> {
1✔
125
      if (copy.has(f)) {
1✔
126
        copy.remove(f);
1✔
127
      }
128
    });
1✔
129
    return copy;
1✔
130
  }
131

132
  private void setUserId(User u) {
133
    if (Objects.nonNull(u.getUserId())) {
1✔
134
      this.setUserId(u.getUserId());
1✔
135
    }
136
  }
1✔
137

138
  private void setEmail(User u) {
139
    if (!StringUtils.isEmpty(u.getEmail()) && u.getEmail() != null) {
1✔
140
      this.setEmail(Junidecode.unidecode(u.getEmail()));
1✔
141
    }
142
  }
1✔
143

144
  private void setDisplayName(User u) {
145
    if (!StringUtils.isEmpty(u.getDisplayName())) {
1✔
146
      this.setDisplayName(u.getDisplayName());
1✔
147
    }
148
  }
1✔
149

150
  private void setEmailPreference(User u) {
151
    if (Objects.nonNull(u.getEmailPreference())) {
1✔
152
      this.setEmailPreference(u.getEmailPreference());
1✔
153
    }
154
  }
1✔
155

156
  private void setRoles(User u) {
157
    if (CollectionUtils.isNotEmpty(u.getRoles())) {
1✔
158
      this.setRoles(u.getRoles());
1✔
159
    }
160
  }
1✔
161

162
  private void setInstitutionId(User u) {
163
    if (Objects.nonNull(u.getInstitutionId())) {
1✔
164
      this.setInstitutionId(u.getInstitutionId());
×
165
    }
166
  }
1✔
167

168
  public String getEmail() {
169
    return email;
1✔
170
  }
171

172
  public void setEmail(String email) {
173
    this.email = email;
1✔
174
  }
1✔
175

176
  public String getDisplayName() {
177
    return displayName;
1✔
178
  }
179

180
  public void setDisplayName(String displayName) {
181
    this.displayName = displayName;
1✔
182
  }
1✔
183

184
  public List<UserRole> getRoles() {
185
    return roles;
1✔
186
  }
187

188
  public void setRoles(List<UserRole> roles) {
189
    this.roles = roles;
1✔
190
  }
1✔
191

192
  public void setAdminRole() {
193
    this.roles = List.of(UserRoles.Admin());
1✔
194
  }
1✔
195

196
  public void setChairpersonRole() {
197
    this.roles = List.of(UserRoles.Chairperson());
1✔
198
  }
1✔
199

200
  public void setChairpersonRoleWithDAC(int dacId) {
201
    UserRole chairpersonRole = UserRoles.Chairperson();
1✔
202
    chairpersonRole.setDacId(dacId);
1✔
203
    this.roles = List.of(chairpersonRole);
1✔
204
  }
1✔
205

206
  public void setITDirectorRole() {
207
    this.roles = List.of(UserRoles.ITDirector());
1✔
208
  }
1✔
209

210
  public void setMemberRole() {
211
    this.roles = List.of(UserRoles.Member());
1✔
212
  }
1✔
213

214
  public void setResearcherRole() {
215
    this.roles = List.of(UserRoles.Researcher());
1✔
216
  }
1✔
217

218
  public void setSigningOfficialRole() {
219
    this.roles = List.of(UserRoles.SigningOfficial());
1✔
220
  }
1✔
221

222
  public List<UserProperty> getProperties() {
223
    return properties;
1✔
224
  }
225

226
  public void setProperties(List<UserProperty> properties) {
227
    this.properties = properties;
1✔
228
  }
1✔
229

230
  public Date getCreateDate() {
231
    return createDate;
1✔
232
  }
233

234
  public void setCreateDate(Date createDate) {
235
    this.createDate = createDate;
1✔
236
  }
1✔
237

238
  public Boolean getEmailPreference() {
239
    return emailPreference;
1✔
240
  }
241

242
  public void setEmailPreference(Boolean emailPreference) {
243
    this.emailPreference = emailPreference;
1✔
244
  }
1✔
245

246
  public Integer getInstitutionId() {
247
    return institutionId;
1✔
248
  }
249

250
  public void setInstitutionId(Integer institutionId) {
251
    this.institutionId = institutionId;
1✔
252
  }
1✔
253

254
  public String getEraCommonsId() {
255
    return eraCommonsId;
1✔
256
  }
257

258
  public void setEraCommonsId(String eraCommonsId) {
259
    this.eraCommonsId = eraCommonsId;
1✔
260
  }
1✔
261

262
  public void setInstitution(Institution institution) {
263
    this.institution = institution;
1✔
264
  }
1✔
265

266
  public void setInstitution(User user) {
267
    if (Objects.nonNull(user.getInstitution())) {
×
268
      this.institution = user.institution;
×
269
    }
270
  }
×
271

272
  public Institution getInstitution() {
273
    return institution;
1✔
274
  }
275

276
  public void setLibraryCards(List<LibraryCard> cards) {
277
    this.libraryCards = cards;
1✔
278
  }
1✔
279

280
  public List<LibraryCard> getLibraryCards() {
281
    return this.libraryCards;
1✔
282
  }
283

284
  public void addRole(UserRole userRole) {
285
    if (Objects.isNull(this.getRoles())) {
1✔
286
      this.setRoles(new ArrayList<>());
1✔
287
    }
288

289
    if (!this.getRoles().contains(userRole)) {
1✔
290
      this.getRoles().add(userRole);
1✔
291
    }
292
  }
1✔
293

294
  public void addProperty(UserProperty userProp) {
295
    if (Objects.isNull(this.getProperties())) {
1✔
296
      this.setProperties(new ArrayList<>());
1✔
297
    }
298
    if (!this.getProperties().contains(userProp)) {
1✔
299
      this.getProperties().add(userProp);
1✔
300
    }
301
  }
1✔
302

303
  public void addLibraryCard(LibraryCard card) {
304
    if (Objects.isNull(this.getLibraryCards())) {
1✔
305
      this.setLibraryCards(new ArrayList<>());
1✔
306
    }
307
    if (!this.getLibraryCards().contains(card)) {
1✔
308
      this.getLibraryCards().add(card);
1✔
309
    }
310
  }
1✔
311

312
  @Override
313
  public int hashCode() {
314
    return this.getUserId();
1✔
315
  }
316

317
  @Override
318
  public boolean equals(Object obj) {
319
    if (this == obj) {
1✔
320
      return true;
1✔
321
    }
322
    if (obj == null) {
1✔
323
      return false;
×
324
    }
325
    if (getClass() != obj.getClass()) {
1✔
326
      return false;
×
327
    }
328

329
    User other = (User) obj;
1✔
330
    return new EqualsBuilder().append(getUserId(), other.getUserId()).isEquals();
1✔
331
  }
332

333
  @Override
334
  public String toString() {
NEW
335
    return GsonUtil.gsonBuilderWithAdapters().create().toJson(this);
×
336
  }
337

338
  public boolean hasUserRole(UserRoles role) {
339
    if (Objects.isNull(this.getRoles())) {
1✔
340
      return false;
1✔
341
    } else {
342
      return this.getRoles().stream().anyMatch((r) -> r.getRoleId().equals(role.getRoleId()));
1✔
343
    }
344
  }
345

346
  @Transient
347
  public List<Integer> getUserRoleIdsFromUser() {
348
    if (Objects.isNull(this.getRoles())) {
1✔
349
      return List.of();
1✔
350
    }
351
    return this.getRoles()
1✔
352
        .stream()
1✔
353
        .map(UserRole::getRoleId)
1✔
354
        .collect(Collectors.toList());
1✔
355
  }
356

357
  @Transient
358
  public boolean doesUserHaveAnyRoleInSet(EnumSet<UserRoles> userRoles) {
359
    List<Integer> queriedRoleIds = userRoles.stream().map(UserRoles::getRoleId)
1✔
360
        .collect(Collectors.toList());
1✔
361
    return getUserRoleIdsFromUser().stream().anyMatch(queriedRoleIds::contains);
1✔
362
  }
363

364
  @Transient
365
  public Boolean checkIfUserHasRole(String roleName, Integer dacId) {
366
    UserRoles role = UserRoles.getUserRoleFromName(roleName);
1✔
367
    List<UserRole> roles = getRoles();
1✔
368
    List<UserRole> targetRoles = roles.stream()
1✔
369
        .filter((r) -> {
1✔
370
          return r.getName().equals(role.getRoleName())
1✔
371
              && r.getRoleId().equals(role.getRoleId())
1✔
372
              && Objects.equals(r.getDacId(), dacId);
1✔
373
        })
374
        .collect(Collectors.toList());
1✔
375
    return !targetRoles.isEmpty();
1✔
376

377
  }
378

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

© 2026 Coveralls, Inc