/** * 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()); } } }
@Test public void getSearchLocales_shouldExcludeNotAllowedLocales() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_US, en_GB, pl, es")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "es_CL, en_US, pl"); Context.getUserService().saveUser(user); //when List<Locale> searchLocales = adminService.getSearchLocales(); //then assertTrue("en_US", searchLocales.contains(new Locale("en", "US"))); assertTrue("pl", searchLocales.contains(new Locale("pl"))); assertTrue("es", searchLocales.contains(new Locale("es"))); assertFalse("es_CL", searchLocales.contains(new Locale("es", "CL"))); }
@Test public void getSearchLocales_shouldIncludeUsersProficientLocales() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, en_US, pl")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "en_GB, en_US"); Context.getUserService().saveUser(user); //when List<Locale> searchLocales = adminService.getSearchLocales(); //then assertTrue("en_GB", searchLocales.contains(new Locale("en", "GB"))); assertTrue("en_US", searchLocales.contains(new Locale("en", "US"))); assertFalse("pl", searchLocales.contains(new Locale("pl"))); }
@Test public void getSearchLocales_shouldIncludeCurrentlySelectedFullLocaleAndLangugage() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, ""); Context.getUserService().saveUser(user); Context.setLocale(new Locale("en", "GB")); //when List<Locale> searchLocales = adminService.getSearchLocales(); //then assertEquals(Context.getLocale(), searchLocales.get(0)); assertEquals(new Locale(Context.getLocale().getLanguage()), searchLocales.get(1)); }
/** * @see org.openmrs.api.UserService#setUserProperty(User, String, String) */ @Override public User setUserProperty(User user, String key, String value) { if (user != null) { if (!Context.hasPrivilege(PrivilegeConstants.EDIT_USERS) && !user.equals(Context.getAuthenticatedUser())) { throw new APIException("you.are.not.authorized.change.properties", new Object[] { user.getUserId() }); } user.setUserProperty(key, value); try { Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS); Context.getUserService().saveUser(user); } finally { Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS); } } return user; }
/** * @see org.openmrs.api.UserService#removeUserProperty(org.openmrs.User, java.lang.String) */ @Override public User removeUserProperty(User user, String key) { if (user != null) { // if the current user isn't allowed to edit users and // the user being edited is not the current user, throw an // exception if (!Context.hasPrivilege(PrivilegeConstants.EDIT_USERS) && !user.equals(Context.getAuthenticatedUser())) { throw new APIException("you.are.not.authorized.change.properties", new Object[] { user.getUserId() }); } user.removeUserProperty(key); try { Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS); Context.getUserService().saveUser(user); } finally { Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS); } } return user; }
@Test public void saveUser_shouldUpdateUsersUsername() { User u = userService.getUserByUsername(ADMIN_USERNAME); assertNotNull("There needs to be a user with username 'admin' in the database", u); u.setUsername("admin2"); userService.saveUser(u); User u2 = userService.getUserByUsername("admin2"); assertEquals("The fetched user should equal the user we tried to update", u, u2); }
@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"); }
@Test public void getSearchLocales_shouldCacheResultsForAnUser() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, en_US, pl")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "en_GB, en_US"); Context.getUserService().saveUser(user); //when adminService.getSearchLocales(); List<Locale> cachedSearchLocales = getCachedSearchLocalesForCurrentUser(); //then assertThat(cachedSearchLocales, hasItem(Locale.ENGLISH)); assertThat(cachedSearchLocales, hasItem(new Locale("en", "US"))); assertThat(cachedSearchLocales, not(hasItem(new Locale("pl")))); }
@Test public void saveUser_shouldGrantNewRolesInRolesListToUser() { // add in some basic properties executeDataSet(XML_FILENAME); User u = userService.getUserByUsername(ADMIN_USERNAME); Role role1 = new Role(); role1.setDescription("testing1"); role1.setRole("test1"); Privilege p1 = userService.getAllPrivileges().get(0); Set<Privilege> privileges1 = new HashSet<>(); privileges1.add(p1); role1.setPrivileges(privileges1); Role role2 = new Role(); role2.setDescription("testing2"); role2.setRole("test2"); Privilege p2 = userService.getAllPrivileges().get(0); Set<Privilege> privileges2 = new HashSet<>(); privileges2.add(p2); role2.setPrivileges(privileges2); userService.saveUser(u.addRole(role1)); userService.saveUser(u.addRole(role2)); // so the contents are fetched from the db Context.evictFromSession(u); userService.getUser(u.getUserId()).hasRole("test1"); userService.getUser(u.getUserId()).hasRole("test2"); }
/** * This test makes sure that the AuditableInterceptor is registered on the session * * @see AuditableInterceptor#onFlushDirty(Object,Serializable,Object[],Object[],String[],Type[]) */ @Test public void onFlushDirty_shouldBeCalledWhenSavingAnAuditable() { User u = Context.getUserService().getUser(1); u.setUsername("asdf"); Date beforeDate = u.getDateChanged(); Context.getUserService().saveUser(u); Date afterDate = u.getDateChanged(); Assert.assertNotSame(beforeDate, afterDate); }
@Test public void saveGlobalProperty_shouldEvictCachedResults() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, en_US, pl")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "en_GB, en_US"); Context.getUserService().saveUser(user); //sanity check that cache has been populated adminService.getSearchLocales(); List<Locale> cachedSearchLocales = getCachedSearchLocalesForCurrentUser(); assertThat(cachedSearchLocales, hasItem(new Locale("en", "US"))); //evict cache adminService.saveGlobalProperty(new GlobalProperty("test", "TEST")); assertThat(getCacheForCurrentUser(), nullValue()); }
/** * @see LocationUtility#getUserDefaultLocation() */ @Test public void getUserDefaultLocation_shouldReturnTheUserSpecifiedLocationIfAnyIsSet() { //sanity check Assert.assertNull(LocationUtility.getUserDefaultLocation()); User user = Context.getAuthenticatedUser(); Map<String, String> properties = user.getUserProperties(); properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION, "2"); user.setUserProperties(properties); Context.getUserService().saveUser(user); Context.refreshAuthenticatedUser(); Assert.assertEquals("Xanadu", LocationUtility.getUserDefaultLocation().getName()); } }
/** * @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())); }
private Map<String, Object> save(MRSUser mrsUser) { org.openmrs.User openMRSUser = mrsUserToOpenMRSUser(mrsUser); final String password = new Password(PASSWORD_LENGTH).create(); Map<String, Object> userMap = new HashMap<String, Object>(); final org.openmrs.User savedUser = userService.saveUser(openMRSUser, password); userMap.put(USER_KEY, openMrsToMrsUser(savedUser)); userMap.put(PASSWORD_KEY, password); return userMap; }
/** * @see Context#refreshAuthenticatedUser() */ @Test public void refreshAuthenticatedUser_shouldGetFreshValuesFromTheDatabase() { User evictedUser = Context.getAuthenticatedUser(); Context.evictFromSession(evictedUser); User fetchedUser = Context.getUserService().getUser(evictedUser.getUserId()); fetchedUser.getPersonName().setGivenName("new username"); Context.getUserService().saveUser(fetchedUser); // sanity check to make sure the cached object wasn't updated already Assert.assertNotSame(Context.getAuthenticatedUser().getGivenName(), fetchedUser.getGivenName()); Context.refreshAuthenticatedUser(); Assert.assertEquals("new username", Context.getAuthenticatedUser().getGivenName()); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#save(java.lang.Object) */ @Override public UserAndPassword1_8 save(UserAndPassword1_8 user) { User openmrsUser = new User(); String password = user.getPassword(); openmrsUser = Context.getUserService().saveUser(user.getUser(), password); Context.refreshAuthenticatedUser(); if (openmrsUser.getId() != null && StringUtils.isNotBlank(password)) { Context.getUserService().changePassword(openmrsUser, password); } return new UserAndPassword1_8(openmrsUser); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#save(java.lang.Object) */ @Override public UserAndPassword1_8 save(UserAndPassword1_8 user) { User openmrsUser = new User(); if (user.getUser().getUserId() == null) { openmrsUser = Context.getUserService().createUser(user.getUser(), user.getPassword()); } else { openmrsUser = Context.getUserService().saveUser(user.getUser()); Context.refreshAuthenticatedUser(); } return new UserAndPassword1_8(openmrsUser); } }
user.getPersonName().setGivenName(null); user.getPersonName().setMiddleName("middleName"); Context.getUserService().saveUser(user, null); Assert.assertEquals(1, service.getUsersAsPersonStubs("Clinician").size());