-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
User.java
693 lines (585 loc) · 15.4 KB
/
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.openmrs.api.context.Context;
import org.openmrs.util.LocaleUtility;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.util.RoleConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Defines a User Account in the system. This account belongs to a {@link Person} in the system,
* although that person may have other user accounts. Users have login credentials
* (username/password) and can have special user properties. User properties are just simple
* key-value pairs for either quick info or display specific info that needs to be persisted (like
* locale preferences, search options, etc)
*/
public class User extends BaseOpenmrsObject implements java.io.Serializable, Attributable<User>, Auditable, Retireable {
public static final long serialVersionUID = 2L ;
private static final Logger log = LoggerFactory.getLogger(User.class);
// Fields
private Integer userId;
private Person person;
private String systemId;
private String username;
private String email;
private Set<Role> roles;
private Map<String, String> userProperties;
private List<Locale> proficientLocales = null;
private String parsedProficientLocalesProperty = "";
private User creator;
private Date dateCreated;
private User changedBy;
private Date dateChanged;
private boolean retired;
private User retiredBy;
private Date dateRetired;
private String retireReason;
// Constructors
/** default constructor */
public User() {
}
/** constructor with id */
public User(Integer userId) {
this.userId = userId;
}
/** constructor with person object */
public User(Person person) {
this.person = person;
}
/**
* Return true if this user has all privileges
*
* @return true/false if this user is defined as a super user
*/
public boolean isSuperUser() {
return containsRole(RoleConstants.SUPERUSER);
}
/**
* This method shouldn't be used directly. Use org.openmrs.api.context.Context#hasPrivilege so that
* anonymous/authenticated/proxy privileges are all included Return true if this user has the
* specified privilege
*
* @param privilege
* @return true/false depending on whether user has specified privilege
*/
public boolean hasPrivilege(String privilege) {
// All authenticated users have the "" (empty) privilege
if (StringUtils.isEmpty(privilege)) {
return true;
}
if (isSuperUser()) {
return true;
}
Set<Role> tmproles = getAllRoles();
// loop over the roles and check each for the privilege
for (Role tmprole : tmproles) {
if (tmprole.hasPrivilege(privilege)) {
return true;
}
}
return false;
}
/**
* Check if this user has the given String role
*
* @param r String name of a role to check
* @return Returns true if this user has the specified role, false otherwise
*/
public boolean hasRole(String r) {
return hasRole(r, false);
}
/**
* Checks if this user has the given String role
*
* @param r String name of a role to check
* @param ignoreSuperUser If this is false, then this method will always return true for a
* superuser.
* @return Returns true if the user has the given role, or if ignoreSuperUser is false and the user
* is a superUser
*/
public boolean hasRole(String r, boolean ignoreSuperUser) {
if (!ignoreSuperUser && isSuperUser()) {
return true;
}
if (roles == null) {
return false;
}
Set<Role> tmproles = getAllRoles();
log.debug("User # {} has roles: {}", userId, tmproles);
return containsRole(r);
}
/**
* Checks if the user has a given role. Role name comparisons are not case sensitive.
*
* @param roleName the name of the role to check
* @return true if the user has the given role, else false
* <strong>Should</strong> return true if the user has the given role
* <strong>Should</strong> return false if the user does not have the given role
* <strong>Should</strong> be case insensitive
*/
public boolean containsRole(String roleName) {
for (Role role : getAllRoles()) {
if (role.getRole().equalsIgnoreCase(roleName)) {
return true;
}
}
return false;
}
/**
* Get <i>all</i> privileges this user has. This delves into all of the roles that a person has,
* appending unique privileges
*
* @return Collection of complete Privileges this user has
*/
public Collection<Privilege> getPrivileges() {
Set<Privilege> privileges = new HashSet<>();
Set<Role> tmproles = getAllRoles();
Role role;
for (Role tmprole : tmproles) {
role = tmprole;
Collection<Privilege> privs = role.getPrivileges();
if (privs != null) {
privileges.addAll(privs);
}
}
return privileges;
}
// Property accessors
/**
* Returns all roles attributed to this user by expanding the role list to include the parents of
* the assigned roles
*
* @return all roles (inherited from parents and given) for this user
*/
public Set<Role> getAllRoles() {
// the user's immediate roles
Set<Role> baseRoles = new HashSet<>();
// the user's complete list of roles including
// the parent roles of their immediate roles
Set<Role> totalRoles = new HashSet<>();
if (getRoles() != null) {
baseRoles.addAll(getRoles());
totalRoles.addAll(getRoles());
}
log.debug("User's base roles: {}", baseRoles);
try {
for (Role r : baseRoles) {
totalRoles.addAll(r.getAllParentRoles());
}
}
catch (ClassCastException e) {
log.error("Error converting roles for user: " + this);
log.error("baseRoles.class: " + baseRoles.getClass().getName());
log.error("baseRoles: " + baseRoles.toString());
for (Role baseRole : baseRoles) {
log.error("baseRole: '" + baseRole + "'");
}
}
return totalRoles;
}
/**
* @return Returns the roles.
*/
public Set<Role> getRoles() {
return roles;
}
/**
* @param roles The roles to set.
*/
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
/**
* Add the given Role to the list of roles for this User
*
* @param role
* @return Returns this user with the given role attached
*/
public User addRole(Role role) {
if (roles == null) {
roles = new HashSet<>();
}
if (!roles.contains(role) && role != null) {
roles.add(role);
}
return this;
}
/**
* Remove the given Role from the list of roles for this User
*
* @param role
* @return this user with the given role removed
*/
public User removeRole(Role role) {
if (roles != null) {
roles.remove(role);
}
return this;
}
/**
* @see org.openmrs.Attributable#findPossibleValues(java.lang.String)
*/
@Override
@Deprecated
public List<User> findPossibleValues(String searchText) {
try {
return Context.getUserService().getUsersByName(searchText, "", false);
}
catch (Exception e) {
return Collections.emptyList();
}
}
/**
* @see org.openmrs.Attributable#getPossibleValues()
*/
@Override
@Deprecated
public List<User> getPossibleValues() {
try {
return Context.getUserService().getAllUsers();
}
catch (Exception e) {
return Collections.emptyList();
}
}
/**
* @see org.openmrs.Attributable#hydrate(java.lang.String)
*/
@Override
public User hydrate(String userId) {
try {
return Context.getUserService().getUser(Integer.valueOf(userId));
}
catch (Exception e) {
return new User();
}
}
/**
* @see org.openmrs.Attributable#serialize()
*/
@Override
public String serialize() {
if (getUserId() != null) {
return "" + getUserId();
} else {
return "";
}
}
/**
* @see org.openmrs.Attributable#getDisplayString()
*/
@Override
public String getDisplayString() {
String returnString = "";
if (getPersonName() != null) {
returnString += getPersonName().getFullName() + " ";
}
returnString += "(" + getUsername() + ")";
return returnString;
}
/**
* @return Returns the systemId.
*/
public String getSystemId() {
return systemId;
}
/**
* @param systemId The systemId to set.
*/
public void setSystemId(String systemId) {
this.systemId = systemId;
}
/**
* @return Returns the userId.
*/
public Integer getUserId() {
return userId;
}
/**
* @param userId The userId to set.
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
* @return the person
* @since 1.6
*/
public Person getPerson() {
return person;
}
/**
* @return the person, creating a new object if person is null
*/
private Person getPersonMaybeCreate() {
if (person == null) {
person = new Person();
}
return person;
}
/**
* @param person the person to set
* @since 1.6
*/
public void setPerson(Person person) {
this.person = person;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @since 2.2
* @return Returns the email.
*/
public String getEmail() {
return email;
}
/**
* @since 2.2
* @param email The email to set.
*/
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return StringUtils.isNotBlank(username) ? username : systemId;
}
/**
* @return Returns the userProperties.
*/
public Map<String, String> getUserProperties() {
if (userProperties == null) {
userProperties = new HashMap<>();
}
return userProperties;
}
/**
* @param userProperties A Map<String,String> of the properties to set.
*/
public void setUserProperties(Map<String, String> userProperties) {
this.userProperties = userProperties;
}
/**
* Convenience method. Adds the given property to the user's properties
*/
public void setUserProperty(String prop, String value) {
getUserProperties().put(prop, value);
}
/**
* Convenience method. Removes the given property from the user's properties
*/
public void removeUserProperty(String prop) {
if (getUserProperties() != null && userProperties.containsKey(prop)) {
userProperties.remove(prop);
}
}
/**
* Get prop property from this user's properties. If prop is not found in properties, return empty
* string
*
* @param prop
* @return property value
*/
public String getUserProperty(String prop) {
if (getUserProperties() != null && userProperties.containsKey(prop)) {
return userProperties.get(prop);
}
return "";
}
/**
* Get prop property from this user's properties. If prop is not found in properties, return
* <code>defaultValue</code>
*
* @param prop
* @param defaultValue
* @return property value
* @see #getUserProperty(java.lang.String)
*/
public String getUserProperty(String prop, String defaultValue) {
if (getUserProperties() != null && userProperties.containsKey(prop)) {
return userProperties.get(prop);
}
return defaultValue;
}
/**
* @see Person#addName(PersonName)
*/
public void addName(PersonName name) {
getPersonMaybeCreate().addName(name);
}
/**
* @see Person#getPersonName()
*/
public PersonName getPersonName() {
return getPerson() == null ? null : getPerson().getPersonName();
}
/**
* Get givenName on the Person this user account belongs to
*
* @see Person#getGivenName()
*/
public String getGivenName() {
return getPerson() == null ? null : getPerson().getGivenName();
}
/**
* Get familyName on the Person this user account belongs to
*
* @see Person#getFamilyName()
*/
public String getFamilyName() {
return getPerson() == null ? null : getPerson().getFamilyName();
}
/**
* @see org.openmrs.Person#getNames()
*/
public Set<PersonName> getNames() {
return person.getNames();
}
/**
* Returns a list of Locales for which the User is considered proficient.
*
* @return List of the User's proficient locales
*/
public List<Locale> getProficientLocales() {
String proficientLocalesProperty = getUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES);
if ((proficientLocales == null)
|| (!OpenmrsUtil.nullSafeEquals(parsedProficientLocalesProperty, proficientLocalesProperty))) {
parsedProficientLocalesProperty = proficientLocalesProperty;
proficientLocales = new ArrayList<>();
if (proficientLocalesProperty != null) {
String[] proficientLocalesArray = proficientLocalesProperty.split(",");
for (String proficientLocaleSpec : proficientLocalesArray) {
if (proficientLocaleSpec.length() > 0) {
Locale proficientLocale = LocaleUtility.fromSpecification(proficientLocaleSpec);
if (!proficientLocales.contains(proficientLocale)) {
proficientLocales.add(proficientLocale);
if (StringUtils.isNotEmpty(proficientLocale.getCountry())) {
// add the language also
Locale languageOnlyLocale = LocaleUtility.fromSpecification(proficientLocale.getLanguage());
if (!proficientLocales.contains(languageOnlyLocale)) {
proficientLocales.add(LocaleUtility.fromSpecification(proficientLocale.getLanguage()));
}
}
}
}
}
}
}
// return a copy so that the list isn't changed by other processes
return new ArrayList<>(proficientLocales);
}
/**
* @since 1.5
* @see org.openmrs.OpenmrsObject#getId()
*/
@Override
public Integer getId() {
return getUserId();
}
/**
* @since 1.5
* @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
*/
@Override
public void setId(Integer id) {
setUserId(id);
}
@Override
public User getCreator() {
return creator;
}
@Override
public void setCreator(User creator) {
this.creator = creator;
}
@Override
public Date getDateCreated() {
return dateCreated;
}
@Override
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
@Override
public User getChangedBy() {
return changedBy;
}
@Override
public void setChangedBy(User changedBy) {
this.changedBy = changedBy;
}
@Override
public Date getDateChanged() {
return dateChanged;
}
@Override
public void setDateChanged(Date dateChanged) {
this.dateChanged = dateChanged;
}
@Override
public Boolean isRetired() {
return retired;
}
@Override
public Boolean getRetired() {
return retired;
}
@Override
public void setRetired(Boolean retired) {
this.retired = retired;
}
@Override
public User getRetiredBy() {
return retiredBy;
}
@Override
public void setRetiredBy(User retiredBy) {
this.retiredBy = retiredBy;
}
@Override
public Date getDateRetired() {
return dateRetired;
}
@Override
public void setDateRetired(Date dateRetired) {
this.dateRetired = dateRetired;
}
@Override
public String getRetireReason() {
return retireReason;
}
@Override
public void setRetireReason(String retireReason) {
this.retireReason = retireReason;
}
}