@Transactional public class UserServiceImpl extends BaseOpenmrsService implements UserService
UserService
,
Context
Modifier and Type | Field and Description |
---|---|
protected UserDAO |
dao |
ADMIN_PASSWORD_LOCKED_PROPERTY
Constructor and Description |
---|
UserServiceImpl() |
Modifier and Type | Method and Description |
---|---|
void |
changeHashedPassword(User user,
String hashedPassword,
String salt)
Changes the current user's password directly.
|
void |
changePassword(String oldPassword,
String newPassword)
Changes the current user's password.
|
void |
changePassword(User user,
String newPassword)
Changes password of
User passed in |
void |
changePassword(User user,
String oldPassword,
String newPassword)
Change user password.
|
void |
changePasswordUsingActivationKey(String activationKey,
String newPassword)
Change user password given the activation key
|
void |
changePasswordUsingSecretAnswer(String secretAnswer,
String pw)
Change user password given the answer to the secret question
|
void |
changeQuestionAnswer(String pw,
String q,
String a)
Changes the current user's secret question and answer.
|
void |
changeQuestionAnswer(User u,
String question,
String answer)
Changes the passed user's secret question and answer.
|
User |
createUser(User user,
String password)
Create user with given password.
|
String |
generateSystemId()
Generates system ids based on the following algorithm scheme: user_id-check digit
|
List<Privilege> |
getAllPrivileges()
Returns all privileges currently possible for any User
|
List<Role> |
getAllRoles()
Returns all roles currently possible for any User
|
List<User> |
getAllUsers()
Returns all users in the system
|
Integer |
getCountOfUsers(String name,
List<Role> roles,
boolean includeRetired)
Return the number of users with a matching name or system id and have at least one of the
given roles assigned to them
|
Locale |
getDefaultLocaleForUser(User user) |
String |
getLastLoginTime(User user)
Retrieves the last login time of the user in Unix Timestamp
|
Privilege |
getPrivilege(String p)
Returns Privilege in the system with given String privilege
|
Privilege |
getPrivilegeByUuid(String uuid)
Get Privilege by its UUID
|
Role |
getRole(String r)
Returns role object with given string role
|
Role |
getRoleByUuid(String uuid)
Get Role by its UUID
|
String |
getSecretQuestion(User user)
Returns secret question for the given user.
|
User |
getUser(Integer userId)
Get user by internal user identifier.
|
User |
getUserByActivationKey(String activationKey)
Gets a user with the specified activation key
|
User |
getUserByUsername(String username)
Get user by username (user's login identifier)
|
User |
getUserByUsernameOrEmail(String usernameOrEmail)
Gets a user by username or email
|
User |
getUserByUuid(String uuid)
Get user by the given uuid.
|
List<User> |
getUsers(String nameSearch,
List<Role> roles,
boolean includeVoided)
Return a list of users sorted by personName (see
PersonByNameComparator ) if any part
of the search matches first/last/system id and the user has one at least one of the given
roles assigned to them |
List<User> |
getUsers(String name,
List<Role> roles,
boolean includeRetired,
Integer start,
Integer length)
Return a batch of users of a specific size sorted by personName (see
PersonByNameComparator ) if any part of the search matches first/last/system id and
the user has one at least one of the given roles assigned to them. |
List<User> |
getUsersByName(String givenName,
String familyName,
boolean includeVoided)
Search for a list of users by exact first name and last name.
|
List<User> |
getUsersByPerson(Person person,
boolean includeRetired)
Get all user accounts that belong to a given person.
|
List<User> |
getUsersByRole(Role role)
Get users by role granted
|
boolean |
hasDuplicateUsername(User user)
true/false if username or systemId is already in db in username or system_id columns
|
boolean |
isSecretAnswer(User u,
String answer)
Compares
answer against the user 's secret answer. |
void |
purgePrivilege(Privilege privilege)
Completely remove a privilege from the database
|
void |
purgeRole(Role role)
Complete remove a role from the database
|
void |
purgeUser(User user)
Completely remove a location from the database (not reversible).
|
void |
purgeUser(User user,
boolean cascade)
Completely remove a user from the database (not reversible).
|
User |
removeUserProperty(User user,
String key)
Removes the property denoted by
key from the user 's properties. |
User |
retireUser(User user,
String reason)
Deactivate a user account so that it can no longer log in.
|
Privilege |
savePrivilege(Privilege privilege)
Save the given privilege in the database
|
Role |
saveRole(Role role)
Save the given role in the database
|
User |
saveUser(User user)
Updates a given
user in the database. |
User |
saveUserProperties(Map<String,String> properties)
Replaces all user properties with the given map of properties for the current user
|
User |
saveUserProperty(String key,
String value)
Saves the current key/value as a user property for the current user.
|
User |
setUserActivationKey(User user)
Sets a user's activation key
|
void |
setUserDAO(UserDAO dao) |
User |
setUserProperty(User user,
String key,
String value)
Adds the
key /value pair to the given user . |
User |
unretireUser(User user)
Clears retired flag for a user.
|
User |
unvoidUser(User user) |
User |
voidUser(User user,
String reason) |
onShutdown, onStartup
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
onShutdown, onStartup
protected UserDAO dao
public void setUserDAO(UserDAO dao)
public User createUser(User user, String password) throws APIException
UserService
createUser
in interface UserService
user
- the user to createpassword
- the password for created userAPIException
UserService.createUser(org.openmrs.User, java.lang.String)
@Transactional(readOnly=true) public User getUser(Integer userId) throws APIException
UserService
getUser
in interface UserService
userId
- internal identifierAPIException
- Should fetch user with given userIdUserService.getUser(java.lang.Integer)
@Transactional(readOnly=true) public User getUserByUsername(String username) throws APIException
UserService
getUserByUsername
in interface UserService
username
- User's identifier used for authenticationAPIException
UserService.getUserByUsername(java.lang.String)
@Transactional(readOnly=true) public boolean hasDuplicateUsername(User user) throws APIException
UserService
hasDuplicateUsername
in interface UserService
user
- User to compareAPIException
- Should verify that username and system id is uniqueUserService.hasDuplicateUsername(org.openmrs.User)
@Transactional(readOnly=true) public List<User> getUsersByRole(Role role) throws APIException
UserService
getUsersByRole
in interface UserService
role
- Role that the Users must have to be returnedAPIException
- Should fetch users assigned given role
Should not fetch user that does not belong to given roleUserService.getUsersByRole(org.openmrs.Role)
@CacheEvict(value="userSearchLocales", allEntries=true) public User saveUser(User user) throws APIException
UserService
user
in the database.saveUser
in interface UserService
APIException
UserService.saveUser(org.openmrs.User)
public User voidUser(User user, String reason) throws APIException
APIException
public User retireUser(User user, String reason) throws APIException
UserService
retireUser
in interface UserService
APIException
- Should retire user and set attributesUserService.retireUser(org.openmrs.User, java.lang.String)
public User unvoidUser(User user) throws APIException
APIException
public User unretireUser(User user) throws APIException
UserService
unretireUser
in interface UserService
APIException
- Should unretire and unmark all attributesUserService.unretireUser(org.openmrs.User)
@Transactional(readOnly=true) public List<User> getAllUsers() throws APIException
UserService
getAllUsers
in interface UserService
APIException
- Should fetch all users in the system
Should not contains any duplicate usersUserService.getAllUsers()
@Transactional(readOnly=true) public List<Privilege> getAllPrivileges() throws APIException
UserService
getAllPrivileges
in interface UserService
APIException
- Should return all privileges in the systemUserService.getAllPrivileges()
@Transactional(readOnly=true) public Privilege getPrivilege(String p) throws APIException
UserService
getPrivilege
in interface UserService
APIException
- Should fetch privilege for given nameUserService.getPrivilege(java.lang.String)
public void purgePrivilege(Privilege privilege) throws APIException
UserService
purgePrivilege
in interface UserService
privilege
- Privilege to deleteAPIException
- Should delete given privilege from the database
Should throw error when privilege is core privilegeUserService.purgePrivilege(org.openmrs.Privilege)
public Privilege savePrivilege(Privilege privilege) throws APIException
UserService
savePrivilege
in interface UserService
privilege
- Privilege to updateAPIException
- Should save given privilege to the databaseUserService.savePrivilege(org.openmrs.Privilege)
@Transactional(readOnly=true) public List<Role> getAllRoles() throws APIException
UserService
getAllRoles
in interface UserService
APIException
- Should return all roles in the systemUserService.getAllRoles()
@Transactional(readOnly=true) public Role getRole(String r) throws APIException
UserService
getRole
in interface UserService
APIException
- Should fetch role for given role nameUserService.getRole(java.lang.String)
public void purgeRole(Role role) throws APIException
UserService
purgeRole
in interface UserService
role
- Role to delete from the databaseAPIException
- Should throw error when role is a core role
Should return if role is null
Should delete given role from databaseUserService.purgeRole(org.openmrs.Role)
public Role saveRole(Role role) throws APIException
UserService
saveRole
in interface UserService
role
- Role to updateAPIException
- Should throw error if role inherits from itself
Should save given role to the databaseUserService.saveRole(org.openmrs.Role)
public void changePassword(String oldPassword, String newPassword) throws APIException
UserService
changePassword
in interface UserService
oldPassword
- current passwordnewPassword
- new passwordAPIException
- Should match on correctly hashed sha1 stored password
Should match on incorrectly hashed sha1 stored password
Should match on sha512 hashed password
Should be able to update password multiple times
Should respect locking via runtime propertiesUserService.changePassword(java.lang.String, java.lang.String)
public void changeHashedPassword(User user, String hashedPassword, String salt) throws APIException
UserService
changeHashedPassword
in interface UserService
user
- the user whose password you want to changehashedPassword
- - the already hashed password to storesalt
- - the salt which should be used with this hashed passwordAPIException
UserService.changeHashedPassword(User, String, String)
public void changeQuestionAnswer(User u, String question, String answer) throws APIException
UserService
changeQuestionAnswer
in interface UserService
u
- User to changeAPIException
UserService.changeQuestionAnswer(User, String, String)
public void changeQuestionAnswer(String pw, String q, String a)
UserService
changeQuestionAnswer
in interface UserService
pw
- user's passwordq
- questiona
- answerUserService.changeQuestionAnswer(java.lang.String, java.lang.String,
java.lang.String)
@Transactional(readOnly=true) public boolean isSecretAnswer(User u, String answer)
UserService
answer
against the user
's secret answer.isSecretAnswer
in interface UserService
u
- userUserService.isSecretAnswer(org.openmrs.User, java.lang.String)
@Transactional(readOnly=true) public List<User> getUsersByName(String givenName, String familyName, boolean includeVoided) throws APIException
UserService
getUsersByName
in interface UserService
APIException
UserService.getUsersByName(java.lang.String, java.lang.String, boolean)
@Transactional(readOnly=true) public List<User> getUsersByPerson(Person person, boolean includeRetired) throws APIException
UserService
getUsersByPerson
in interface UserService
APIException
- Should fetch all accounts for a person when include retired is true
Should not fetch retired accounts when include retired is falseUserService.getUsersByPerson(org.openmrs.Person, boolean)
@Transactional(readOnly=true) public List<User> getUsers(String nameSearch, List<Role> roles, boolean includeVoided) throws APIException
UserService
PersonByNameComparator
) if any part
of the search matches first/last/system id and the user has one at least one of the given
roles
assigned to themgetUsers
in interface UserService
nameSearch
- string to compare to the beginning of user's given/middle/family/family2
namesroles
- all the Roles the user must containincludeVoided
- true/false whether to include voided usersAPIException
UserService.getUsers(java.lang.String, java.util.List, boolean)
public User setUserProperty(User user, String key, String value)
UserService
key
/value
pair to the given user
.
Implementations of this method should handle privileges
setUserProperty
in interface UserService
UserService.setUserProperty(User, String, String)
public User removeUserProperty(User user, String key)
UserService
key
from the user
's properties.
Implementations of this method should handle privilegesremoveUserProperty
in interface UserService
UserService.removeUserProperty(org.openmrs.User, java.lang.String)
@Transactional(readOnly=true) public String generateSystemId()
generateSystemId
in interface UserService
UserService.generateSystemId()
public void purgeUser(User user) throws APIException
UserService
purgeUser
in interface UserService
user
- the User to remove from the database.
Should delete given userAPIException
UserService.purgeUser(org.openmrs.User)
public void purgeUser(User user, boolean cascade) throws APIException
UserService
Users
should be
voided and not deleted altogether (since many foreign key constraints
depend on users, deleting a user would require deleting all traces, and any historical trail
would be lost). This method only clears user roles and attempts to delete the user record. If
the user has been included in any other parts of the database (through a foreign key), the
attempt to delete the user will violate foreign key constraints and fail.purgeUser
in interface UserService
cascade
- true
to delete associated content
Should throw APIException if cascade is true
Should delete given user when cascade equals false
Should not delete user roles for given user when cascade equals falseAPIException
UserService.purgeUser(org.openmrs.User, boolean)
@Transactional(readOnly=true) public Privilege getPrivilegeByUuid(String uuid) throws APIException
UserService
getPrivilegeByUuid
in interface UserService
APIException
UserService.getPrivilegeByUuid(java.lang.String)
@Transactional(readOnly=true) public Role getRoleByUuid(String uuid) throws APIException
UserService
getRoleByUuid
in interface UserService
APIException
UserService.getRoleByUuid(java.lang.String)
@Transactional(readOnly=true) public User getUserByUuid(String uuid) throws APIException
UserService
getUserByUuid
in interface UserService
APIException
- Should fetch user with given uuid
Should find object given valid uuid
Should return null if no object found with given uuidUserService.getUserByUuid(java.lang.String)
@Transactional(readOnly=true) public Integer getCountOfUsers(String name, List<Role> roles, boolean includeRetired)
UserService
getCountOfUsers
in interface UserService
name
- patient nameroles
- all the Roles the user must containincludeRetired
- Specifies whether voided users should be includedUserService.getCountOfUsers(String, List, boolean)
@Transactional(readOnly=true) public List<User> getUsers(String name, List<Role> roles, boolean includeRetired, Integer start, Integer length) throws APIException
UserService
PersonByNameComparator
) if any part of the search matches first/last/system id and
the user has one at least one of the given roles
assigned to them. If start and
length are not specified, then all matches are returned, If name is empty or null, then all
all users will be returned taking into consideration the values of start and length
arguments.getUsers
in interface UserService
name
- string to compare to the beginning of user's given/middle/family/family2 namesroles
- all the Roles the user must containincludeRetired
- true/false whether to include voided usersstart
- beginning index for the batchlength
- number of users to return in the batchAPIException
UserService.getUsers(String, List, boolean, Integer, Integer)
public User saveUserProperty(String key, String value)
UserService
saveUserProperty
in interface UserService
key
- the authenticated user's propertyvalue
- value of the propertypublic User saveUserProperties(Map<String,String> properties)
UserService
saveUserProperties
in interface UserService
properties
- the authenticated user's properties@Authorized(value="Edit User Passwords") @Logging(ignoredArgumentIndexes={1,2}) public void changePassword(User user, String oldPassword, String newPassword) throws APIException
UserService
changePassword
in interface UserService
user
- the user to update passwordoldPassword
- the user password to updatenewPassword
- the new user passwordAPIException
- for not existing user and if old password is weakUserService.changePassword(User, String, String)
public void changePassword(User user, String newPassword)
UserService
User
passed inchangePassword
in interface UserService
user
- user whose password is to be changednewPassword
- new password to setpublic void changePasswordUsingSecretAnswer(String secretAnswer, String pw) throws APIException
UserService
changePasswordUsingSecretAnswer
in interface UserService
secretAnswer
- the answer to secret questionpw
- the new password
Should update password if secret is correct
Should not update password if secret is not correctAPIException
public String getSecretQuestion(User user) throws APIException
UserService
getSecretQuestion
in interface UserService
APIException
@Transactional(readOnly=true) public User getUserByUsernameOrEmail(String usernameOrEmail)
UserService
getUserByUsernameOrEmail
in interface UserService
usernameOrEmail
- User's email address or usernameUserService.getUserByUsernameOrEmail(java.lang.String)
@Transactional(readOnly=true) public User getUserByActivationKey(String activationKey)
UserService
getUserByActivationKey
in interface UserService
activationKey
- User's activation key for password resetUserService.getUserByActivationKey(java.lang.String)
public User setUserActivationKey(User user) throws MessageException
UserService
setUserActivationKey
in interface UserService
user
- The user for which the activation key will be setAPIException
MessageException
UserService.setUserActivationKey(org.openmrs.User)
public Locale getDefaultLocaleForUser(User user)
getDefaultLocaleForUser
in interface UserService
user
- the User whose Locale to retrieveUserService.getDefaultLocaleForUser(User)
public void changePasswordUsingActivationKey(String activationKey, String newPassword)
UserService
changePasswordUsingActivationKey
in interface UserService
activationKey
- the activation for password resetnewPassword
- the new passwordUserService.changePasswordUsingActivationKey(String, String);
public String getLastLoginTime(User user)
UserService
getLastLoginTime
in interface UserService
user
- the subject userUserService.getLastLoginTime(User)
Copyright © 2024 OpenMRS Inc.. All rights reserved.