Search in sources :

Example 1 with InvalidActivationKeyException

use of org.openmrs.api.InvalidActivationKeyException in project openmrs-module-pihcore by PIH.

the class ResetPasswordPageController method post.

public String post(@RequestParam(value = "activationKey") String activationKey, @RequestParam(value = "newPassword") String newPassword, @RequestParam(value = "confirmPassword") String confirmPassword, @SpringBean("userService") UserService userService, HttpServletRequest request, PageModel model, UiUtils ui) {
    model.addAttribute("activationKey", activationKey);
    model.addAttribute("newPassword", newPassword);
    model.addAttribute("confirmPassword", confirmPassword);
    try {
        Context.addProxyPrivilege(GET_USERS);
        User user = userService.getUserByActivationKey(activationKey);
        if (user == null) {
            throw new InvalidActivationKeyException("activation.key.not.correct");
        }
        Context.setLocale(userService.getDefaultLocaleForUser(user));
        if (StringUtils.isBlank(newPassword) || StringUtils.isBlank(confirmPassword)) {
            throw new ValidationException(ui.message("emr.account.changePassword.newAndConfirmPassword.required"));
        } else if (!newPassword.equals(confirmPassword)) {
            throw new ValidationException(ui.message("emr.account.changePassword.newAndConfirmPassword.DoesNotMatch"));
        }
        userService.changePasswordUsingActivationKey(activationKey, newPassword);
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("emr.account.changePassword.success"));
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
    } catch (Exception e) {
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, ui.message("emr.account.changePassword.fail", new Object[] { e.getMessage() }, Context.getLocale()));
        log.warn("An error occurred while trying to reset password", e);
    } finally {
        Context.removeProxyPrivilege(GET_USERS);
    }
    return "redirect:index.htm";
}
Also used : User(org.openmrs.User) InvalidActivationKeyException(org.openmrs.api.InvalidActivationKeyException) ValidationException(org.openmrs.api.ValidationException) ValidationException(org.openmrs.api.ValidationException) InvalidActivationKeyException(org.openmrs.api.InvalidActivationKeyException)

Aggregations

User (org.openmrs.User)1 InvalidActivationKeyException (org.openmrs.api.InvalidActivationKeyException)1 ValidationException (org.openmrs.api.ValidationException)1