/** * @since 1.5 * @see org.openmrs.OpenmrsObject#getId() */ @Override public Integer getId() { return getUserId(); }
/** * @see org.openmrs.api.db.UserDAO#getLoginCredential(org.openmrs.User) */ @Override public LoginCredential getLoginCredential(User user) { return (LoginCredential) sessionFactory.getCurrentSession().get(LoginCredential.class, user.getUserId()); }
/** * @param user The recipient/user to set. */ public void setRecipient(User user) { this.recipient = user; setRecipientId(user.getUserId()); }
/** * @see org.openmrs.api.db.UserDAO#changeHashedPassword(User, String, String) */ @Override public void changeHashedPassword(User user, String hashedPassword, String salt) throws DAOException { User authUser = Context.getAuthenticatedUser(); updateUserPassword(hashedPassword, salt, authUser.getUserId(), new Date(), user.getUserId()); }
/** * @see org.openmrs.api.UserService#saveUser(org.openmrs.User, java.lang.String) */ @Override public User saveUser(User user, String password) { // only change the user's password when creating a new user boolean isNewUser = user.getUserId() == null; sessionFactory.getCurrentSession().saveOrUpdate(user); if (isNewUser && password != null) { //update the new user with the password String salt = Security.getRandomToken(); String hashedPassword = Security.encodeString(password + salt); updateUserPassword(hashedPassword, salt, Context.getAuthenticatedUser().getUserId(), new Date(), user .getUserId()); } return user; }
@Override public String getSecretQuestion(User user) throws APIException { if (user.getUserId() != null) { LoginCredential loginCredential = dao.getLoginCredential(user); return loginCredential.getSecretQuestion(); } else { return null; } }
/** * @see org.openmrs.api.UserService#hasDuplicateUsername(org.openmrs.User) */ @Override @Transactional(readOnly = true) public boolean hasDuplicateUsername(User user) throws APIException { return dao.hasDuplicateUsername(user.getUsername(), user.getSystemId(), user.getUserId()); }
/** * @see org.openmrs.api.db.UserDAO#changePassword(org.openmrs.User, java.lang.String) */ @Override public void changePassword(User u, String pw) throws DAOException { User authUser = Context.getAuthenticatedUser(); if (authUser == null) { authUser = u; } log.debug("updating password"); //update the user with the new password String salt = getLoginCredential(u).getSalt(); String newHashedPassword = Security.encodeString(pw + salt); updateUserPassword(newHashedPassword, salt, authUser.getUserId(), new Date(), u.getUserId()); }
/** * Refresh the authenticated user object in this UserContext. This should be used when updating * information in the database about the current user and it needs to be reflecting in the * (cached) {@link #getAuthenticatedUser()} User object. * * @since 1.5 */ public void refreshAuthenticatedUser() { log.debug("Refreshing authenticated user"); if (user != null) { user = Context.getUserService().getUser(user.getUserId()); //update the stored location in the user's session setUserLocation(); } }
/** * @see org.openmrs.api.UserService#changePassword(java.lang.String, java.lang.String) */ @Override public void changePassword(String pw, String pw2) throws DAOException { User u = Context.getAuthenticatedUser(); LoginCredential credentials = getLoginCredential(u); if (!credentials.checkPassword(pw)) { log.error("Passwords don't match"); throw new DAOException("Passwords don't match"); } log.info("updating password for " + u.getUsername()); // update the user with the new password String salt = getLoginCredential(u).getSalt(); String newHashedPassword = Security.encodeString(pw2 + salt); updateUserPassword(newHashedPassword, salt, u.getUserId(), new Date(), u.getUserId()); }
/** * @see UserService#getUser(Integer) */ @Test public void getUser_shouldFetchUserWithGivenUserId() { User user = userService.getUser(501); Assert.assertEquals(501, user.getUserId().intValue()); }
/** * @see UserService#getUserByUuid(String) */ @Test public void getUserByUuid_shouldFindObjectGivenValidUuid() { String uuid = "c1d8f5c2-e131-11de-babe-001e378eb67e"; User user = userService.getUserByUuid(uuid); Assert.assertEquals(501, (int) user.getUserId()); }
/** * @see PersonService#savePersonMergeLog(PersonMergeLog) */ @Test public void savePersonMergeLog_shouldSetCreatorIfNull() throws Exception { PersonMergeLog personMergeLog = getTestPersonMergeLog(); personMergeLog.setCreator(null); PersonMergeLog persisted = Context.getPersonService().savePersonMergeLog(personMergeLog); Assert.assertEquals("creator has not been correctly set", Context.getAuthenticatedUser().getUserId(), persisted .getCreator().getUserId()); }
/** * @see org.openmrs.api.UserService#saveUser(org.openmrs.User) */ @Override @CacheEvict(value = "userSearchLocales", allEntries = true) public User saveUser(User user) throws APIException { if (user.getUserId() == null) { throw new APIException("This method can be called only to update existing users"); } Context.requirePrivilege(PrivilegeConstants.EDIT_USERS); checkPrivileges(user); if (hasDuplicateUsername(user)) { throw new DAOException("Username " + user.getUsername() + " or system id " + user.getSystemId() + " is already in use."); } return dao.saveUser(user, null); }
/** * @see org.openmrs.api.UserService#createUser(org.openmrs.User, java.lang.String) */ @Override public User createUser(User user, String password) throws APIException { if (user.getUserId() != null) { throw new APIException("This method can be used for only creating new users"); } Context.requirePrivilege(PrivilegeConstants.ADD_USERS); checkPrivileges(user); // if a password wasn't supplied, throw an error if (password == null || password.length() < 1) { throw new APIException("User.creating.password.required", (Object[]) null); } if (hasDuplicateUsername(user)) { throw new DAOException("Username " + user.getUsername() + " or system id " + user.getSystemId() + " is already in use."); } // TODO Check required fields for user!! OpenmrsUtil.validatePassword(user.getUsername(), password, user.getSystemId()); return dao.saveUser(user, password); }
/** * @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 UserService#changePassword(User, String, String) */ @Override @Authorized(PrivilegeConstants.EDIT_USER_PASSWORDS) @Logging(ignoredArgumentIndexes = { 1, 2 }) public void changePassword(User user, String oldPassword, String newPassword) throws APIException { if (user.getUserId() == null) { throw new APIException("user.must.exist", (Object[]) null); } if (oldPassword == null) { if (!Context.hasPrivilege(PrivilegeConstants.EDIT_USER_PASSWORDS)) { throw new APIException("null.old.password.privilege.required", (Object[]) null); } } else if (!dao.getLoginCredential(user).checkPassword(oldPassword)) { throw new APIException("old.password.not.correct", (Object[]) null); } updatePassword(user, newPassword); }
/** * @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 ConditionService#unvoidCondition(Condition) */ @Test public void unvoidCondition_shouldUnvoidConditionSuccessfully(){ Condition voidedCondition = conditionService.voidCondition(conditionService.getCondition(4), "Test Reason"); Assert.assertTrue(voidedCondition.getVoided()); Assert.assertNotNull(voidedCondition.getVoidReason()); Assert.assertNotNull(voidedCondition.getDateVoided()); Assert.assertEquals(new Integer(1), voidedCondition.getVoidedBy().getUserId()); Condition unVoidedCondition = conditionService.unvoidCondition(voidedCondition); Assert.assertEquals(ConditionVerificationStatus.CONFIRMED, unVoidedCondition.getVerificationStatus()); Assert.assertEquals(ConditionClinicalStatus.ACTIVE, unVoidedCondition.getClinicalStatus()); Assert.assertEquals("2cb6880e-2cd6-11e4-9138-a6c5e4d20fb7", unVoidedCondition.getUuid()); Assert.assertFalse(unVoidedCondition.getVoided()); Assert.assertNull(unVoidedCondition.getVoidReason()); Assert.assertNull(unVoidedCondition.getDateVoided()); Assert.assertNull(unVoidedCondition.getVoidedBy()); }