/** * Checks whether user is Daemon. * However this is not the preferred method for checking to see if the current thread is a daemon thread, * rather use Daemon.isDeamonThread(). * isDaemonThread is preferred for checking to see if you are in that thread or if the current thread is daemon. * * @param user user whom we are checking if daemon * @return true if user is Daemon * @should return true for a daemon user * @should return false if the user is not a daemon */ public static boolean isDaemonUser(User user) { return DAEMON_USER_UUID.equals(user.getUuid()); }
/** * Change user associations for notPreferred to preferred person. * * @param preferred * @param notPreferred * @param mergedData a patient merge audit data object to update * @see PatientServiceImpl#mergePatients(Patient, Patient) */ private void changeUserAssociations(Patient preferred, Person notPreferred, PersonMergeLogData mergedData) { UserService userService = Context.getUserService(); List<User> users = userService.getUsersByPerson(notPreferred, true); for (User user : users) { user.setPerson(preferred); User persisted = userService.saveUser(user); if (mergedData != null) { mergedData.addMovedUser(persisted.getUuid()); } } }
String output = "<update tableName=\"users\"><column name=\"uuid\" value=\"" + user.getUuid() + "\"/><where> user_id = '" + user.getUserId() + "' and system_id = '" + user.getSystemId().replace("'", "\\'") + "'</where></update>";
/** * @param newHashedPassword * @param salt * @param userId * @param date * @param userId2 */ private void updateUserPassword(String newHashedPassword, String salt, Integer changedBy, Date dateChanged, Integer userIdToChange) { User changeForUser = getUser(userIdToChange); if (changeForUser == null) { throw new DAOException("Couldn't find user to set password for userId=" + userIdToChange); } User changedByUser = getUser(changedBy); LoginCredential credentials = getLoginCredential(changeForUser); credentials.setUserId(userIdToChange); credentials.setHashedPassword(newHashedPassword); credentials.setSalt(salt); credentials.setChangedBy(changedByUser); credentials.setDateChanged(dateChanged); credentials.setUuid(changeForUser.getUuid()); sessionFactory.getCurrentSession().merge(credentials); // reset lockout changeForUser.setUserProperty(OpenmrsConstants.USER_PROPERTY_LOCKOUT_TIMESTAMP, ""); changeForUser.setUserProperty(OpenmrsConstants.USER_PROPERTY_LOGIN_ATTEMPTS, "0"); saveUser(changeForUser, null); }
/** * @see org.openmrs.BaseOpenmrsObject#getUuid() */ @Override public String getUuid() { return getUser().getUuid(); }
/** * Overridden here since the unique id is not on UserAndPassword directly * * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getUniqueId(java.lang.Object) */ @Override protected String getUniqueId(UserAndPassword1_8 delegate) { return delegate.getUser().getUuid(); }
/** * @see PatientService#mergePatients(Patient,Patient) */ @Test public void mergePatients_shouldAuditMovedUsers() throws Exception { //retrieve patients Patient preferred = patientService.getPatient(999); Patient notPreferred = patientService.getPatient(7); voidOrders(Collections.singleton(notPreferred)); User user = Context.getUserService().getUser(501); user.setPerson(notPreferred); Context.getUserService().saveUser(user); //merge the two patients and retrieve the audit object PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred); Assert.assertTrue("user association change not audited", isValueInList(Context.getUserService().getUser(501).getUuid(), audit.getPersonMergeLogData().getMovedUsers())); }
@Test public void convertToRepresentation_shouldConvertValuesOfMaps() throws Exception { Map<Object, Object> toConvert = new LinkedHashMap<Object, Object>(); Date date = new Date(); toConvert.put("date", date); Map<String, Object> toConvert2ndLevel = new HashMap<String, Object>(); Concept concept = conceptService.getConcept(3); toConvert2ndLevel.put("concept", concept); toConvert.put("map", toConvert2ndLevel); toConvert.put("string", "a string"); User user = userService.getUser(1); toConvert.put(1, user); SimpleObject converted = (SimpleObject) ConversionUtil.convertToRepresentation(toConvert, Representation.REF); assertThat(converted, (Matcher) hasEntry(is("date"), is(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(date)))); assertThat(converted, (Matcher) hasEntry(is("string"), is("a string"))); assertThat(converted, (Matcher) hasEntry(is("1"), is(hasEntry(is("uuid"), is(user.getUuid()))))); assertThat(converted, (Matcher) hasEntry(is("map"), is(hasEntry(is("concept"), is(hasEntry(is("uuid"), is(concept.getUuid()))))))); } }
/** * @see SessionController1_8#get(WebRequest) * @verifies return the session id if the user is authenticated */ @Test public void get_shouldReturnTheSessionIdAndUserIfTheUserIsAuthenticated() throws Exception { Assert.assertTrue(Context.isAuthenticated()); Object ret = controller.get(request); Object userProp = PropertyUtils.getProperty(ret, "user"); Assert.assertEquals(SESSION_ID, PropertyUtils.getProperty(ret, "sessionId")); Assert.assertEquals(true, PropertyUtils.getProperty(ret, "authenticated")); Assert.assertEquals(Context.getAuthenticatedUser().getUuid(), PropertyUtils.getProperty(userProp, "uuid")); Object personProp = PropertyUtils.getProperty(userProp, "person"); Assert.assertEquals(Context.getAuthenticatedUser().getPerson().getUuid(), PropertyUtils.getProperty(personProp, "uuid")); }
/** * @see UserController#findUsers(String,WebRequest,HttpServletResponse) * @throws Exception * @verifies find matching users */ @Test public void findUsers_shouldFindMatchingUsers() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI()); req.addParameter("q", "but"); SimpleObject result = deserialize(handle(req)); Assert.assertNotNull(result); List<Object> hits = (List<Object>) result.get("results"); Assert.assertEquals(1, hits.size()); Util.log("Found " + hits.size() + " user(s)", result); Assert.assertEquals(service.getUserByUuid(getUuid()).getUuid(), PropertyUtils.getProperty(hits.get(0), "uuid")); }
/** * @see UserController#findUsers(String,WebRequest,HttpServletResponse) * @throws Exception * @verifies find matching users */ @Test public void findUsers_shouldFindMatchingUsers() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI()); req.addParameter("q", "but"); SimpleObject result = deserialize(handle(req)); Assert.assertNotNull(result); List<Object> hits = (List<Object>) result.get("results"); assertEquals(1, hits.size()); Util.log("Found " + hits.size() + " user(s)", result); assertEquals(service.getUserByUuid(getUuid()).getUuid(), PropertyUtils.getProperty(hits.get(0), "uuid")); }
@Test public void shouldVoidACondition() throws Exception { Condition condition = conditionService.getConditionByUuid(getUuid()); Assert.assertFalse(condition.isVoided()); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); req.addParameter("reason", "unit test"); handle(req); condition = conditionService.getConditionByUuid(getUuid()); Assert.assertEquals(Context.getAuthenticatedUser().getUuid(), condition.getVoidedBy().getUuid()); Assert.assertTrue(condition.isVoided()); Assert.assertEquals("unit test", condition.getVoidReason()); Assert.assertNotNull(condition.getDateVoided()); }
@Test public void shouldVoidDiagnosis() throws Exception { Diagnosis diagnosis = diagnosisService.getDiagnosisByUuid(getUuid()); Assert.assertFalse(diagnosis.isVoided()); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); req.addParameter("reason", "test"); handle(req); diagnosis = diagnosisService.getDiagnosisByUuid(getUuid()); Assert.assertTrue(diagnosis.isVoided()); Assert.assertEquals("test", diagnosis.getVoidReason()); Assert.assertNotNull(diagnosis.getDateVoided()); Assert.assertNotNull(diagnosis.getVoidedBy()); Assert.assertEquals(Context.getAuthenticatedUser().getUuid(), diagnosis.getVoidedBy().getUuid()); }
/** * @see UserController#retireUser(User,String,WebRequest) * @throws Exception * @verifies void a patient */ @Test public void retireUser_shouldRetireAUser() throws Exception { User user = service.getUserByUuid(getUuid()); Assert.assertFalse(user.isRetired()); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + user.getUuid()); req.addParameter("!purge", ""); req.addParameter("reason", "unit test"); handle(req); User retiredUser = service.getUserByUuid(getUuid()); Assert.assertTrue(retiredUser.isRetired()); Assert.assertEquals("unit test", retiredUser.getRetireReason()); }
/** * @see UserController#retireUser(User,String,WebRequest) * @throws Exception * @verifies void a patient */ @Test public void retireUser_shouldRetireAUser() throws Exception { User user = service.getUserByUuid(getUuid()); Assert.assertFalse(user.isRetired()); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + user.getUuid()); req.addParameter("!purge", ""); req.addParameter("reason", "unit test"); handle(req); User retiredUser = service.getUserByUuid(getUuid()); assertTrue(retiredUser.isRetired()); assertEquals("unit test", retiredUser.getRetireReason()); }
@Test public void updateUser_shouldUpdateTheUserPassword() throws Exception { User user = service.getUserByUuid(RestTestConstants1_8.USER_UUID); assertNotNull(user); assertNotEquals(user, Context.getAuthenticatedUser()); final String username = user.getUsername(); final String newPassword = "SomeOtherPassword123"; ContextAuthenticationException exception = null; try { Context.authenticate(username, newPassword); } catch (ContextAuthenticationException e) { exception = e; } assertNotNull(exception); assertEquals("Invalid username and/or password: " + username, exception.getMessage()); handle(newPostRequest("password" + "/" + user.getUuid(), "{\"newPassword\":\"" + newPassword + "\"}")); Context.logout(); Context.authenticate(username, newPassword); assertEquals(user, Context.getAuthenticatedUser()); }
@Test public void updateUser_shouldUpdateTheUserPassword() throws Exception { User user = service.getUserByUuid(getUuid()); assertNotNull(user); assertNotEquals(user, Context.getAuthenticatedUser()); final String username = user.getUsername(); final String newPassword = "SomeOtherPassword123"; ContextAuthenticationException exception = null; try { Context.authenticate(username, newPassword); } catch (ContextAuthenticationException e) { exception = e; } assertNotNull(exception); assertEquals("Invalid username and/or password: " + username, exception.getMessage()); handle(newPostRequest(getURI() + "/" + user.getUuid(), "{\"password\":\"" + newPassword + "\"}")); Context.logout(); Context.authenticate(username, newPassword); assertEquals(user, Context.getAuthenticatedUser()); }