Get Help from Others
Q&A: Ask OpenMRS
Discussion: OpenMRS Talk
Real-Time: IRC Chat | Slack
A User object represents someone who can log into the system. When a Person needs to access the system they are given a username and password – a row in the users table. User has a column with a foreign key to the associated person id. This means that one Person can have multiple User accounts for multiple logins.
Users are given permissions through Roles. Each Role has a set of Privileges assigned to it. Those atomic privileges are used in the code to restrict access to different parts of the API.
To check whether the current user has permissions, use Context.hasPrivilege().
A User object represents a Person who can log into the system.
Properties on a User:
// Creates user with given password. public User createUser(User user, String password); // Gets user by given userId. public User getUser(Integer userId); // Gets user by given uuid. public User getUserByUuid(String uuid); // Gets user by given username. public User getUserByUsername(String username); // Verifies that the username and system id are unique. public boolean hasDuplicateUsername(User user); // Updates a given user in the database. public User saveUser(User user); // Deactivates a user account so that it can no longer log in. public User retireUser(User user, String reason); // Save the given privilege in the database. public Privilege savePrivilege(Privilege privilege); // Gets role for given role name. public Role getRole(String r); // Gets privilege for given name. public Privilege getPrivilege(String p); // Gets list of users by first and last name. public List<User> getUsersByName(String givenName, String familyName, boolean includeRetired);
User user = Context.getUserService().getUser(Integer.valueOf(userId)); user.setUsername("Johnny"); Context.getUserService().saveUser(user);
The User class source code can be seen here.
The UserService class source code can be seen here.
A module demonstrating how to use this class can be seen here.
Roles are hierarchical. A role inherits privileges from its parent. A user can have multiple roles, and a role can have multiple privileges.
Properties on a Role:
The Role class source code can be seen here.
Privilege is a property of a Role.
Properties on a Privilege:
The Privilege class source code can be seen here.
A set of key value pairs. Used to store user specific data. Something like:
User user2 = Context.getUserService().getUser(Integer.valueOf(userId)); user2.getUserProperty(String) //get the value defined by the given string/key, returns a String user2.getUserProperties() //get all properties for that user in key-value pairs, returns a Map<String, String> user2.setUserProperty(String, String) //overwrites given user property key with the given value, and adds it to the user's properties
Here is a class diagram for User and related classes.
Future updates to this visualization can be made on draw.io here, or using the XML file here.