use of org.openmrs.api.UserService in project openmrs-core by openmrs.
the class PersonVoidHandler method handle.
/**
* Sets all personVoid* attributes to the given parameters.
*
* @see org.openmrs.api.handler.RequiredDataHandler#handle(org.openmrs.OpenmrsObject,
* org.openmrs.User, java.util.Date, java.lang.String)
* @should set the personVoided bit
* @should set the personVoidReason
* @should set personVoidedBy
* @should not set personVoidedBy if non null
* @should set personDateVoided
* @should not set personDateVoided if non null
* @should not set the personVoidReason if already personVoided
* @should retire users
*/
@Override
public void handle(Person person, User voidingUser, Date voidedDate, String voidReason) {
// skip over all work if the object is already voided
if (!person.getPersonVoided()) {
if (person.getPersonId() != null) {
// Skip if person is not persisted
UserService us = Context.getUserService();
for (User user : us.getUsersByPerson(person, false)) {
us.retireUser(user, voidReason);
}
}
person.setPersonVoided(true);
person.setPersonVoidReason(voidReason);
if (person.getPersonVoidedBy() == null) {
person.setPersonVoidedBy(voidingUser);
}
if (person.getPersonDateVoided() == null) {
person.setPersonDateVoided(voidedDate);
}
}
}
use of org.openmrs.api.UserService in project openmrs-core by openmrs.
the class MigrationHelper method importUsers.
/**
* Takes XML like: <something> <user date_changed="2001-03-06 08:46:53.0"
* date_created="2001-03-06 08:46:53.0" username="hamish@mit.edu" first_name="Hamish"
* last_name="Fraser" user_id="2001"/> </something> Returns the number of users added
*/
public static int importUsers(Document document) throws ParseException {
int ret = 0;
Random rand = new Random();
UserService us = Context.getUserService();
List<Node> toAdd = new ArrayList<>();
findNodesNamed(document, "user", toAdd);
for (Node node : toAdd) {
Element e = (Element) node;
String username = e.getAttribute("username");
if (username == null || username.length() == 0) {
throw new IllegalArgumentException("each <user /> element must define a user_name attribute");
}
if (us.getUserByUsername(username) != null) {
continue;
}
User user = new User();
user.setPerson(new Person());
PersonName pn = new PersonName(e.getAttribute("first_name"), "", e.getAttribute("last_name"));
user.addName(pn);
user.setUsername(username);
user.setDateCreated(parseDate(e.getAttribute("date_created")));
user.setDateChanged(parseDate(e.getAttribute("date_changed")));
// Generate a temporary password: 8-12 random characters
String pass;
{
int length = rand.nextInt(4) + 8;
char[] password = new char[length];
for (int x = 0; x < length; x++) {
int randDecimalAsciiVal = rand.nextInt(93) + 33;
password[x] = (char) randDecimalAsciiVal;
}
pass = new String(password);
}
us.createUser(user, pass);
++ret;
}
return ret;
}
use of org.openmrs.api.UserService in project openmrs-core by openmrs.
the class MigrationHelper method importRelationships.
/**
* Takes a list of Strings of the format RELATIONSHIP:<user last name>,<user first
* name>,<relationship type name>,<patient identifier type name>,<identifier> so if user hfraser
* if the cardiologist of the patient with patient_id 8039 in PIH's old emr, then:
* RELATIONSHIP:hfraser,Cardiologist,HIV-EMRV1,8039 (the "RELATIONSHIP:" is not actually
* necessary. Anything before and including the first : will be dropped If autoCreateUsers is
* true, and no user exists with the given username, one will be created. If autoAddRole is
* true, then whenever a user is auto-created, if a role exists with the same name as
* relationshipType.name, then the user will be added to that role
*/
public static int importRelationships(Collection<String> relationships, boolean autoCreateUsers, boolean autoAddRole) {
PatientService ps = Context.getPatientService();
UserService us = Context.getUserService();
PersonService personService = Context.getPersonService();
List<Relationship> relsToAdd = new ArrayList<>();
Random rand = new Random();
for (String s : relationships) {
if (s.contains(":")) {
s = s.substring(s.indexOf(":") + 1);
}
String[] ss = s.split(",");
if (ss.length < 5) {
throw new IllegalArgumentException("The line '" + s + "' is in the wrong format");
}
String userLastName = ss[0];
String userFirstName = ss[1];
String username = (userFirstName + userLastName).replaceAll(" ", "");
String relationshipType = ss[2];
String identifierType = ss[3];
String identifier = ss[4];
User user = null;
{
// first try looking for non-voided users
List<User> users = us.getUsersByName(userFirstName, userLastName, false);
if (users.size() == 1) {
user = users.get(0);
} else if (users.size() > 1) {
throw new IllegalArgumentException("Found " + users.size() + " users named '" + userLastName + ", " + userFirstName + "'");
}
}
if (user == null) {
// next try looking for voided users
List<User> users = us.getUsersByName(userFirstName, userLastName, false);
if (users.size() == 1) {
user = users.get(0);
} else if (users.size() > 1) {
throw new IllegalArgumentException("Found " + users.size() + " voided users named '" + userLastName + ", " + userFirstName + "'");
}
}
if (user == null && autoCreateUsers) {
user = new User();
user.setPerson(new Person());
PersonName pn = new PersonName(userFirstName, "", userLastName);
user.addName(pn);
user.setUsername(username);
// Generate a temporary password: 8-12 random characters
String pass;
{
int length = rand.nextInt(4) + 8;
char[] password = new char[length];
for (int x = 0; x < length; x++) {
int randDecimalAsciiVal = rand.nextInt(93) + 33;
password[x] = (char) randDecimalAsciiVal;
}
pass = new String(password);
}
if (autoAddRole) {
Role role = us.getRole(relationshipType);
if (role != null) {
user.addRole(role);
}
}
us.createUser(user, pass);
}
if (user == null) {
throw new IllegalArgumentException("Can't find user '" + userLastName + ", " + userFirstName + "'");
}
Person person = personService.getPerson(user.getUserId());
RelationshipType relationship = personService.getRelationshipTypeByName(relationshipType);
PatientIdentifierType pit = ps.getPatientIdentifierTypeByName(identifierType);
List<PatientIdentifier> found = ps.getPatientIdentifiers(identifier, Collections.singletonList(pit), null, null, null);
if (found.size() != 1) {
throw new IllegalArgumentException("Found " + found.size() + " patients with identifier '" + identifier + "' of type " + identifierType);
}
Person relative = personService.getPerson(found.get(0).getPatient().getPatientId());
Relationship rel = new Relationship();
rel.setPersonA(person);
rel.setRelationshipType(relationship);
rel.setPersonB(relative);
relsToAdd.add(rel);
}
int addedSoFar = 0;
for (Relationship rel : relsToAdd) {
personService.saveRelationship(rel);
++addedSoFar;
}
return addedSoFar;
}
use of org.openmrs.api.UserService in project openmrs-core by openmrs.
the class ContextDAOTest method authenticate_shouldThrowAContextAuthenticationExceptionIfUsernameIsAnEmptyString.
@Test(expected = ContextAuthenticationException.class)
public void authenticate_shouldThrowAContextAuthenticationExceptionIfUsernameIsAnEmptyString() {
// update a user with a username that is an empty string for this test
UserService us = Context.getUserService();
User u = us.getUser(1);
u.setUsername("");
u.getPerson().setGender("M");
us.saveUser(u);
dao.authenticate("", "password");
}
use of org.openmrs.api.UserService in project openmrs-module-mirebalais by PIH.
the class MirebalaisHospitalActivator method removeOldPrivileges.
/* private void migratePaperRecordLocation(PaperRecordProperties paperRecordProperties) {
Context.getAdministrationService().executeSQL("update patient_identifier set location_id = (select location_id from location where uuid='"+
Locations.MIREBALAIS_HOSPITAL.uuid() + "')" +
"where identifier_type = (select patient_identifier_type_id from patient_identifier_type where uuid = '" +
paperRecordProperties.getPaperRecordIdentifierType().getUuid() + "')" +
"and location_id = (select location_id from location where uuid='" +
Locations.MIREBALAIS_CDI_PARENT.uuid() + "')", false);
Context.getAdministrationService().executeSQL("update paperrecord_paper_record set record_location = (select location_id from location where uuid='" +
Locations.MIREBALAIS_HOSPITAL.uuid() + "')" +
"where record_location = (select location_id from location where uuid='" +
Locations.MIREBALAIS_CDI_PARENT.uuid() + "')", false);
}*/
private void removeOldPrivileges() {
UserService userService = Context.getUserService();
Privilege privilege = userService.getPrivilege("App: appointmentschedulingui.scheduleAdmin");
if (privilege != null) {
userService.purgePrivilege(privilege);
}
}
Aggregations