@FormParam("redirectUri") String redirectUri,
@FormParam("asHealthRecord") Boolean asHR
) {
String status = "unknown";
String hrid = null;
User user = null;
if (!newPassword.equals(newPassword2)) {
//passwords do not match
status = "nomatch";
} else if (!ServiceUtils.isPassword(newPassword)) {
//new password invalid
status = "invalid";
} else {
PersistenceService persistenceSvc = PersistenceService.getInstance();
if (asHR == null) asHR = false;
try {
persistenceSvc.beginTx();
EntityManager em = persistenceSvc.getEntityManager();
if (asHR) {
user = em.find(HealthRecord.class, userId).getUser();
} else {
user = em.find(User.class, userId);
}
if (user == null) throw new Exception("Unable to locate user entity");
if (user.isPatient()) {
if (hrid == null) {
if (asHR) {
hrid = userId.toString();
} else {
hrid = user.getPrimaryHealthRecord().getHealthRecordId().toString();
//hrid = user.getHealthRecords().get(0).getHealthRecordId().toString();
}
}
// patients will pass in their own redirect URL using own HRID generally
if (redirectUri == null || redirectUri.isEmpty()) {
redirectUri = "." + AppConfig.PATH_PATIENT_ROOT + "/" + hrid + "/account";
}
}
if (user.testPassword(currentPassword) == false) {
throw new Exception("Incorrect password");
} else if (user.getUsername().equalsIgnoreCase(newPassword)) {
//error if setting password with username
throw new Exception("Password matches username");
}
user.setPassword(newPassword);
user.setRequiresReset(Boolean.FALSE);
em.merge(user);
persistenceSvc.commitTx();
status = "updated";
} catch (Exception e) {
logger.error("Password Update Failed - " + e.getMessage());
status = "failed";
} finally {
persistenceSvc.close();
}
}
//mirror getFirstLogin() in Main.java here
if ("updated".equals(status) && redirectUri.contains("passwordchange")) {
//password has been updated on first login/forced change
HttpSession session = req.getSession(); //create session if none exists
if (session != null)
session.setAttribute("needsreset", "false");
if (req.isUserInRole(UserConfig.ROLE_PATIENT) || req.isUserInRole(UserConfig.ROLE_CARETAKER)) {
if (hrid != null) {
session.setAttribute("healthRecordId", hrid);
if (req.isUserInRole(UserConfig.ROLE_PATIENT)) {
UserPreferences pref = user.getPrimaryHealthRecord().getPreferences();
boolean showCN;
if (pref != null) {
showCN = (pref.getShowCarenotebook() == true) ? true : false;
} else {
showCN = false;