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

DataBiosphere / consent / #5339

30 Aug 2024 07:11PM UTC coverage: 78.196% (+0.1%) from 78.081%
#5339

push

web-flow
DCJ-638: Remove old approved users API (#2389)

7 of 7 new or added lines in 3 files covered. (100.0%)

3 existing lines in 2 files now uncovered.

9916 of 12681 relevant lines covered (78.2%)

0.78 hits per line

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

94.7
/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.List;
11
import java.util.Objects;
12
import java.util.stream.Collectors;
13
import net.gcardone.junidecode.Junidecode;
14
import org.apache.commons.collections4.CollectionUtils;
15
import org.apache.commons.lang3.StringUtils;
16
import org.apache.commons.lang3.builder.EqualsBuilder;
17
import org.broadinstitute.consent.http.enumeration.UserRoles;
18
import org.broadinstitute.consent.http.util.gson.GsonUtil;
19

20
public class User {
21

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

31
  @JsonProperty
32
  private Integer userId;
33

34
  @JsonProperty
35
  private String email;
36

37
  @JsonProperty
38
  private String displayName;
39

40
  @JsonProperty
41
  private Date createDate;
42

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

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

49
  @JsonProperty
50
  private Boolean emailPreference;
51

52
  @JsonProperty
53
  private Integer institutionId;
54

55
  @JsonProperty
56
  private String eraCommonsId;
57

58
  private Institution institution;
59

60
  private List<LibraryCard> libraryCards;
61

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

213
  public void setMemberRoleWithDAC(int dacId) {
214
    UserRole memberRole = UserRoles.Member();
1✔
215
    memberRole.setDacId(dacId);
1✔
216
    this.roles = List.of(memberRole);
1✔
217
  }
1✔
218

219
  public void setResearcherRole() {
220
    this.roles = List.of(UserRoles.Researcher());
1✔
221
  }
1✔
222

223
  public void setSigningOfficialRole() {
224
    this.roles = List.of(UserRoles.SigningOfficial());
1✔
225
  }
1✔
226

227
  public List<UserProperty> getProperties() {
228
    return properties;
1✔
229
  }
230

231
  public void setProperties(List<UserProperty> properties) {
232
    this.properties = properties;
1✔
233
  }
1✔
234

235
  public Date getCreateDate() {
236
    return createDate;
1✔
237
  }
238

239
  public void setCreateDate(Date createDate) {
240
    this.createDate = createDate;
1✔
241
  }
1✔
242

243
  public Boolean getEmailPreference() {
244
    return emailPreference;
1✔
245
  }
246

247
  public void setEmailPreference(Boolean emailPreference) {
248
    this.emailPreference = emailPreference;
1✔
249
  }
1✔
250

251
  public Integer getInstitutionId() {
252
    return institutionId;
1✔
253
  }
254

255
  public void setInstitutionId(Integer institutionId) {
256
    this.institutionId = institutionId;
1✔
257
  }
1✔
258

259
  public String getEraCommonsId() {
260
    return eraCommonsId;
1✔
261
  }
262

263
  public void setEraCommonsId(String eraCommonsId) {
264
    this.eraCommonsId = eraCommonsId;
1✔
265
  }
1✔
266

267
  public void setInstitution(Institution institution) {
268
    this.institution = institution;
1✔
269
  }
1✔
270

271
  public void setInstitution(User user) {
272
    if (Objects.nonNull(user.getInstitution())) {
×
273
      this.institution = user.institution;
×
274
    }
275
  }
×
276

277
  public Institution getInstitution() {
278
    return institution;
1✔
279
  }
280

281
  public void setLibraryCards(List<LibraryCard> cards) {
282
    this.libraryCards = cards;
1✔
283
  }
1✔
284

285
  public List<LibraryCard> getLibraryCards() {
286
    return this.libraryCards;
1✔
287
  }
288

289
  public void addRole(UserRole userRole) {
290
    if (Objects.isNull(this.getRoles())) {
1✔
291
      this.setRoles(new ArrayList<>());
1✔
292
    }
293

294
    if (!this.getRoles().contains(userRole)) {
1✔
295
      this.getRoles().add(userRole);
1✔
296
    }
297
  }
1✔
298

299
  public void addProperty(UserProperty userProp) {
300
    if (Objects.isNull(this.getProperties())) {
1✔
301
      this.setProperties(new ArrayList<>());
1✔
302
    }
303
    if (!this.getProperties().contains(userProp)) {
1✔
304
      this.getProperties().add(userProp);
1✔
305
    }
306
  }
1✔
307

308
  public void addLibraryCard(LibraryCard card) {
309
    if (Objects.isNull(this.getLibraryCards())) {
1✔
310
      this.setLibraryCards(new ArrayList<>());
1✔
311
    }
312
    if (!this.getLibraryCards().contains(card)) {
1✔
313
      this.getLibraryCards().add(card);
1✔
314
    }
315
  }
1✔
316

317
  @Override
318
  public int hashCode() {
319
    return this.getUserId();
1✔
320
  }
321

322
  @Override
323
  public boolean equals(Object obj) {
324
    if (this == obj) {
1✔
325
      return true;
1✔
326
    }
327
    if (obj == null) {
1✔
328
      return false;
×
329
    }
330
    if (getClass() != obj.getClass()) {
1✔
331
      return false;
×
332
    }
333

334
    User other = (User) obj;
1✔
335
    return new EqualsBuilder().append(getUserId(), other.getUserId()).isEquals();
1✔
336
  }
337

338
  @Override
339
  public String toString() {
340
    return GsonUtil.gsonBuilderWithAdapters().create().toJson(this);
×
341
  }
342

343
  public boolean hasUserRole(UserRoles role) {
344
    if (Objects.isNull(this.getRoles())) {
1✔
345
      return false;
1✔
346
    } else {
347
      return this.getRoles().stream().anyMatch((r) -> r.getRoleId().equals(role.getRoleId()));
1✔
348
    }
349
  }
350

351
  @Transient
352
  public List<Integer> getUserRoleIdsFromUser() {
353
    if (Objects.isNull(this.getRoles())) {
1✔
UNCOV
354
      return List.of();
×
355
    }
356
    return this.getRoles()
1✔
357
        .stream()
1✔
358
        .map(UserRole::getRoleId)
1✔
359
        .collect(Collectors.toList());
1✔
360
  }
361

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

375
  }
376

377
}
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