org.openmrs.api.context
Class UserContext

java.lang.Object
  extended by org.openmrs.api.context.UserContext

public class UserContext
extends java.lang.Object

Represents an OpenMRS User Context which stores the current user information. Only one User may be authenticated within a UserContext at any given time. The UserContext should not be accessed directly, but rather used through the Context. This class should be kept light-weight. There is one instance of this class per user that is logged into the system.

See Also:
Context

Constructor Summary
UserContext()
          Default public constructor
 
Method Summary
 void addProxyPrivilege(java.lang.String privilege)
          Gives the given privilege to all calls to hasPrivilege.
 User authenticate(java.lang.String username, java.lang.String password, ContextDAO contextDAO)
          Authenticate the user to this UserContext.
 User becomeUser(java.lang.String systemId)
          Change current authentication to become another user.
 java.util.Set<Role> getAllRoles()
          Gets all the roles for the (un)authenticated user.
 java.util.Set<Role> getAllRoles(User user)
          Gets all the roles for a user.
 User getAuthenticatedUser()
           
 java.util.Locale getLocale()
           
 boolean hasPrivilege(java.lang.String privilege)
          Tests whether or not currently authenticated user has a particular privilege
 boolean isAuthenticated()
           
 void logout()
          logs out the "active" (authenticated) user within this UserContext
 void refreshAuthenticatedUser()
          Refresh the authenticated user object in this UserContext.
 void removeProxyPrivilege(java.lang.String privilege)
          Will remove one instance of privilege from the privileges that are currently proxied
 void setLocale(java.util.Locale locale)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UserContext

public UserContext()
Default public constructor

Method Detail

authenticate

public User authenticate(java.lang.String username,
                         java.lang.String password,
                         ContextDAO contextDAO)
                  throws ContextAuthenticationException
Authenticate the user to this UserContext.

Parameters:
username - String login name
password - String login password
contextDAO - ContextDAO implementation to use for authentication
Returns:
User that has been authenticated
Throws:
ContextAuthenticationException
See Also:
Context.authenticate(String,String)

refreshAuthenticatedUser

public void refreshAuthenticatedUser()
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) getAuthenticatedUser() User object.

Since:
1.5

becomeUser

public User becomeUser(java.lang.String systemId)
                throws ContextAuthenticationException
Change current authentication to become another user. (You can only do this if you're already authenticated as a superuser.)

Parameters:
systemId -
Returns:
The new user that this context has been set to. (null means no change was made)
Throws:
ContextAuthenticationException

getAuthenticatedUser

public User getAuthenticatedUser()
Returns:
"active" user who has been authenticated, otherwise null

isAuthenticated

public boolean isAuthenticated()
Returns:
true if user has been authenticated in this UserContext

logout

public void logout()
logs out the "active" (authenticated) user within this UserContext

See Also:
authenticate(java.lang.String, java.lang.String, org.openmrs.api.db.ContextDAO)

addProxyPrivilege

public void addProxyPrivilege(java.lang.String privilege)
Gives the given privilege to all calls to hasPrivilege. This method was visualized as being used as follows (try/finally is important):
 try {
   Context.addProxyPrivilege("AAA");
   Context.get*Service().methodRequiringAAAPrivilege();
 }
 finally {
   Context.removeProxyPrivilege("AAA");
 }
 

Parameters:
privilege - to give to users

removeProxyPrivilege

public void removeProxyPrivilege(java.lang.String privilege)
Will remove one instance of privilege from the privileges that are currently proxied

Parameters:
privilege - Privilege to remove in string form

setLocale

public void setLocale(java.util.Locale locale)
Parameters:
locale - new locale for this context

getLocale

public java.util.Locale getLocale()
Returns:
current locale for this context

getAllRoles

public java.util.Set<Role> getAllRoles()
                                throws java.lang.Exception
Gets all the roles for the (un)authenticated user. Anonymous and Authenticated roles are appended if necessary

Returns:
all expanded roles for a user
Throws:
java.lang.Exception

getAllRoles

public java.util.Set<Role> getAllRoles(User user)
                                throws java.lang.Exception
Gets all the roles for a user. Anonymous and Authenticated roles are appended if necessary

Parameters:
user -
Returns:
all expanded roles for a user
Throws:
java.lang.Exception
Expected behavior:
not fail with null user, add anonymous role to all users, add authenticated role to all authenticated users, return same roles as user getAllRoles method

hasPrivilege

public boolean hasPrivilege(java.lang.String privilege)
Tests whether or not currently authenticated user has a particular privilege

Parameters:
privilege -
Returns:
true if authenticated user has given privilege
Expected behavior:
authorize if authenticated user has specified privilege, authorize if authenticated role has specified privilege, authorize if proxied user has specified privilege, authorize if anonymous user has specified privilege, not authorize if authenticated user does not have specified privilege, not authorize if authenticated role does not have specified privilege, not authorize if proxied user does not have specified privilege, not authorize if anonymous user does not have specified privilege

OpenMRS-1.7.x

Generated Apr 27 2012 10:06 PM. NOTE - these libraries are in active development and subject to change