/
UserContext.java
executable file
·484 lines (420 loc) · 14.4 KB
/
UserContext.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
/**
* 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.api.context;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.openmrs.Location;
import org.openmrs.PrivilegeListener;
import org.openmrs.Role;
import org.openmrs.User;
import org.openmrs.UserSessionListener;
import org.openmrs.UserSessionListener.Event;
import org.openmrs.UserSessionListener.Status;
import org.openmrs.api.APIAuthenticationException;
import org.openmrs.util.LocaleUtility;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.RoleConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents an OpenMRS <code>User Context</code> which stores the current user information. Only
* one <code>User</code> may be authenticated within a UserContext at any given time. The
* UserContext should not be accessed directly, but rather used through the <code>Context</code>.
* This class should be kept light-weight. There is one instance of this class per user that is
* logged into the system.
*
* @see org.openmrs.api.context.Context
*/
public class UserContext implements Serializable {
private static final long serialVersionUID = -806631231941890648L;
/**
* Logger - shared by entire class
*/
private static final Logger log = LoggerFactory.getLogger(UserContext.class);
/**
* User object containing details about the authenticated user
*/
private User user = null;
/**
* User's permission proxies
*/
private List<String> proxies = new ArrayList<>();
/**
* User's locale
*/
private Locale locale = null;
/**
* Cached Role given to all authenticated users
*/
private Role authenticatedRole = null;
/**
* Cache Role given to all users
*/
private Role anonymousRole = null;
/**
* User's defined location
*/
private Integer locationId;
/**
* The authentication scheme for this user
*/
private AuthenticationScheme authenticationScheme;
/**
* Creates a user context based on the provided auth. scheme.
*
* @param authenticationScheme The auth. scheme that applies for this user context.
*
* @since 2.3.0
*/
public UserContext(AuthenticationScheme authenticationScheme) {
this.authenticationScheme = authenticationScheme;
}
/**
* Authenticate user with the provided credentials. The authentication scheme must be Spring wired, see {@link Context#getAuthenticationScheme()}.
*
* @param credentials The credentials to use to authenticate
* @return The authenticated client information
* @throws ContextAuthenticationException
*
* @since 2.3.0
*/
public Authenticated authenticate(Credentials credentials)
throws ContextAuthenticationException {
log.debug("Authenticating client '" + credentials.getClientName() + "' with sheme: " + credentials.getAuthenticationScheme());
Authenticated authenticated = null;
try {
authenticated = authenticationScheme.authenticate(credentials);
this.user = authenticated.getUser();
notifyUserSessionListener(this.user, Event.LOGIN, Status.SUCCESS);
}
catch(ContextAuthenticationException e) {
User loggingInUser = new User();
loggingInUser.setUsername(credentials.getClientName());
notifyUserSessionListener(loggingInUser, Event.LOGIN, Status.FAIL);
throw e;
}
setUserLocation();
log.debug("Authenticated as: " + this.user);
return authenticated;
}
/**
* Refresh the authenticated user object in this UserContext. This should be used when updating
* information in the database about the current user and it needs to be reflecting in the
* (cached) {@link #getAuthenticatedUser()} User object.
*
* @since 1.5
*/
public void refreshAuthenticatedUser() {
log.debug("Refreshing authenticated user");
if (user != null) {
user = Context.getUserService().getUser(user.getUserId());
//update the stored location in the user's session
setUserLocation();
}
}
/**
* Change current authentication to become another user. (You can only do this if you're already
* authenticated as a superuser.)
*
* @param systemId
* @return The new user that this context has been set to. (null means no change was made)
* @throws ContextAuthenticationException
*/
public User becomeUser(String systemId) throws ContextAuthenticationException {
if (!Context.getAuthenticatedUser().isSuperUser()) {
throw new APIAuthenticationException("You must be a superuser to assume another user's identity");
}
log.debug("Turning the authenticated user into user with systemId: {}", systemId);
User userToBecome = Context.getUserService().getUserByUsername(systemId);
if (userToBecome == null) {
throw new ContextAuthenticationException("User not found with systemId: " + systemId);
}
// hydrate the user object
if (userToBecome.getAllRoles() != null) {
userToBecome.getAllRoles().size();
}
if (userToBecome.getUserProperties() != null) {
userToBecome.getUserProperties().size();
}
if (userToBecome.getPrivileges() != null) {
userToBecome.getPrivileges().size();
}
this.user = userToBecome;
//update the user's location
setUserLocation();
log.debug("Becoming user: {}", user);
return userToBecome;
}
/**
* @return "active" user who has been authenticated, otherwise <code>null</code>
*/
public User getAuthenticatedUser() {
return user;
}
/**
* @return true if user has been authenticated in this UserContext
*/
public boolean isAuthenticated() {
return user != null;
}
/**
* logs out the "active" (authenticated) user within this UserContext
*
* @see #authenticate
*/
public void logout() {
log.debug("setting user to null on logout");
notifyUserSessionListener(user, Event.LOGOUT, Status.SUCCESS);
user = null;
}
/**
* Gives the given privilege to all calls to hasPrivilege. This method was visualized as being
* used as follows (try/finally is important):
*
* <pre>
* try {
* Context.addProxyPrivilege("AAA");
* Context.get*Service().methodRequiringAAAPrivilege();
* }
* finally {
* Context.removeProxyPrivilege("AAA");
* }
* </pre>
*
* @param privilege to give to users
*/
public void addProxyPrivilege(String privilege) {
log.debug("Adding proxy privilege: {}", privilege);
proxies.add(privilege);
}
/**
* Will remove one instance of privilege from the privileges that are currently proxied
*
* @param privilege Privilege to remove in string form
*/
public void removeProxyPrivilege(String privilege) {
log.debug("Removing proxy privilege: {}", privilege);
proxies.remove(privilege);
}
/**
* @param locale new locale for this context
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
/**
* @return current locale for this context
*/
public Locale getLocale() {
if (locale == null) {
// don't cache default locale - allows recognition of changed default at login page
return LocaleUtility.getDefaultLocale();
}
return locale;
}
/**
* Gets all the roles for the (un)authenticated user. Anonymous and Authenticated roles are
* appended if necessary
*
* @return all expanded roles for a user
* @throws Exception
*/
public Set<Role> getAllRoles() throws Exception {
return getAllRoles(getAuthenticatedUser());
}
/**
* Gets all the roles for a user. Anonymous and Authenticated roles are appended if necessary
*
* @param user
* @return all expanded roles for a user
* <strong>Should</strong> not fail with null user
* <strong>Should</strong> add anonymous role to all users
* <strong>Should</strong> add authenticated role to all authenticated users
* <strong>Should</strong> return same roles as user getAllRoles method
*/
public Set<Role> getAllRoles(User user) throws Exception {
Set<Role> roles = new HashSet<>();
// add the Anonymous Role
roles.add(getAnonymousRole());
// add the Authenticated role
if (user != null && getAuthenticatedUser() != null && getAuthenticatedUser().equals(user)) {
roles.addAll(user.getAllRoles());
roles.add(getAuthenticatedRole());
}
return roles;
}
/**
* Tests whether or not currently authenticated user has a particular privilege
*
* @param privilege
* @return true if authenticated user has given privilege
* <strong>Should</strong> authorize if authenticated user has specified privilege
* <strong>Should</strong> authorize if authenticated role has specified privilege
* <strong>Should</strong> authorize if proxied user has specified privilege
* <strong>Should</strong> authorize if anonymous user has specified privilege
* <strong>Should</strong> not authorize if authenticated user does not have specified privilege
* <strong>Should</strong> not authorize if authenticated role does not have specified privilege
* <strong>Should</strong> not authorize if proxied user does not have specified privilege
* <strong>Should</strong> not authorize if anonymous user does not have specified privilege
*/
public boolean hasPrivilege(String privilege) {
// if a user has logged in, check their privileges
if (isAuthenticated()
&& (getAuthenticatedUser().hasPrivilege(privilege) || getAuthenticatedRole().hasPrivilege(privilege))) {
// check user's privileges
notifyPrivilegeListeners(getAuthenticatedUser(), privilege, true);
return true;
}
log.debug("Checking '{}' against proxies: {}", privilege, proxies);
// check proxied privileges
for (String s : proxies) {
if (s.equals(privilege)) {
notifyPrivilegeListeners(getAuthenticatedUser(), privilege, true);
return true;
}
}
if (getAnonymousRole().hasPrivilege(privilege)) {
notifyPrivilegeListeners(getAuthenticatedUser(), privilege, true);
return true;
}
// default return value
notifyPrivilegeListeners(getAuthenticatedUser(), privilege, false);
return false;
}
/**
* Convenience method to get the Role in the system designed to be given to all users
*
* @return Role
* <strong>Should</strong> fail if database doesn't contain anonymous role
*/
private Role getAnonymousRole() {
if (anonymousRole != null) {
return anonymousRole;
}
anonymousRole = Context.getUserService().getRole(RoleConstants.ANONYMOUS);
if (anonymousRole == null) {
throw new RuntimeException("Database out of sync with code: " + RoleConstants.ANONYMOUS + " role does not exist");
}
return anonymousRole;
}
/**
* Convenience method to get the Role in the system designed to be given to all users that have
* authenticated in some manner
*
* @return Role
* <strong>Should</strong> fail if database doesn't contain authenticated role
*/
private Role getAuthenticatedRole() {
if (authenticatedRole != null) {
return authenticatedRole;
}
authenticatedRole = Context.getUserService().getRole(RoleConstants.AUTHENTICATED);
if (authenticatedRole == null) {
throw new RuntimeException("Database out of sync with code: " + RoleConstants.AUTHENTICATED
+ " role does not exist");
}
return authenticatedRole;
}
/**
* @return locationId for this user context if any is set
* @since 1.10
*/
public Integer getLocationId() {
return locationId;
}
/**
* @param locationId locationId to set
* @since 1.10
*/
public void setLocationId(Integer locationId) {
this.locationId = locationId;
}
/**
* @return current location for this user context if any is set
* @since 1.9
*/
public Location getLocation() {
if (locationId == null) {
return null;
}
return Context.getLocationService().getLocation(locationId);
}
/**
* @param location the location to set to
* @since 1.9
*/
public void setLocation(Location location) {
if (location != null) {
this.locationId = location.getLocationId();
}
}
/**
* Convenience method that sets the default location of the currently authenticated user using
* the value of the user's default location property
*/
private void setUserLocation() {
if (this.user != null) {
String locationId = this.user.getUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION);
if (StringUtils.isNotBlank(locationId)) {
//only go ahead if it has actually changed OR if wasn't set before
if (this.locationId == null || this.locationId != Integer.parseInt(locationId)) {
try {
this.locationId = Context.getLocationService().getLocation(Integer.valueOf(locationId))
.getLocationId();
}
catch (NumberFormatException e) {
//Drop the stored value since we have no match for the set id
if (this.locationId != null) {
this.locationId = null;
}
log.warn("The value of the default Location property of the user with id:" + this.user.getUserId()
+ " should be an integer", e);
}
}
} else {
if (this.locationId != null) {
this.locationId = null;
}
}
}
}
/**
* Notifies privilege listener beans about any privilege check.
* <p>
* It is called by {@link UserContext#hasPrivilege(java.lang.String)}.
*
* @see PrivilegeListener
* @param user the authenticated user or <code>null</code> if not authenticated
* @param privilege the checked privilege
* @param hasPrivilege <code>true</code> if the authenticated user has the required privilege or
* if it is a proxy privilege
* @since 1.8.4, 1.9.1, 1.10
*/
private void notifyPrivilegeListeners(User user, String privilege, boolean hasPrivilege) {
for (PrivilegeListener privilegeListener : Context.getRegisteredComponents(PrivilegeListener.class)) {
try {
privilegeListener.privilegeChecked(user, privilege, hasPrivilege);
}
catch (Exception e) {
log.error("Privilege listener has failed", e);
}
}
}
private void notifyUserSessionListener(User user, Event event, Status status) {
for(UserSessionListener userSessionListener : Context.getRegisteredComponents(UserSessionListener.class)) {
userSessionListener.loggedInOrOut(user, event, status);
}
}
}