Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: webapp/src/main/webapp/WEB-INF/messages.properties
- ===================================================================
- --- webapp/src/main/webapp/WEB-INF/messages.properties (revision 26253)
- +++ webapp/src/main/webapp/WEB-INF/messages.properties (revision )
- @@ -339,6 +339,9 @@
- error.options.secretAnswer.match=Answers do not match
- error.options.secretAnswer.empty=Answer should not be empty
- error.options.secretQuestion.empty=Question should not be empty
- +error.options.notificationAddress.empty=Notifications-Email address should not be empty
- +error.options.notificationAddress.invalid=Notifications-Email address should be a valid one
- +
- error.username.weak=Invalid username. Must be at least 6 characters
- error.username.invalid=Invalid username. Username must be alphanumeric and cannot start with a number
- error.retired.requireMetadata=Who retired this and why?
- Index: web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java
- ===================================================================
- --- web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision 17337)
- +++ web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision )
- @@ -1,20 +1,26 @@
- package org.openmrs.web.controller;
- -import static org.junit.Assert.assertEquals;
- -import static org.junit.Assert.assertNull;
- -
- import javax.servlet.http.HttpServletResponse;
- +import org.databene.commons.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import org.openmrs.User;
- import org.openmrs.api.context.Context;
- import org.openmrs.api.db.LoginCredential;
- import org.openmrs.api.db.UserDAO;
- +import org.openmrs.test.Verifies;
- +import org.openmrs.util.OpenmrsConstants;
- +import org.openmrs.web.controller.encounter.LocationFormController;
- import org.openmrs.web.test.BaseWebContextSensitiveTest;
- import org.springframework.mock.web.MockHttpServletRequest;
- import org.springframework.mock.web.MockHttpServletResponse;
- +import org.springframework.validation.BeanPropertyBindingResult;
- +import org.springframework.validation.BindingResult;
- +import org.springframework.web.servlet.ModelAndView;
- +import static org.junit.Assert.*;
- +
- public class OptionsFormControllerTest extends BaseWebContextSensitiveTest {
- private User user;
- @@ -31,57 +37,108 @@
- userDao = (UserDAO) applicationContext.getBean("userDAO");
- }
- - @Test
- - public void shouldChangeSecretQuestionAndAnswer() throws Exception {
- - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- - request.setParameter("secretQuestionPassword", "test");
- - request.setParameter("secretQuestionNew", "test_question");
- -
- - String answer = "test_answer";
- - request.setParameter("secretAnswerNew", answer);
- - request.setParameter("secretAnswerConfirm", answer);
- -
- - HttpServletResponse response = new MockHttpServletResponse();
- - controller.handleRequest(request, response);
- -
- - LoginCredential loginCredential = userDao.getLoginCredential(user);
- - assertEquals(answer, loginCredential.getSecretAnswer());
- - }
- -
- - @Test
- - public void shouldRejectEmptySecretAnswer() throws Exception {
- - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- - request.setParameter("secretQuestionPassword", "test");
- - request.setParameter("secretQuestionNew", "test_question");
- -
- - String emptyAnswer = "";
- - request.setParameter("secretAnswerNew", emptyAnswer);
- - request.setParameter("secretAnswerConfirm", emptyAnswer);
- -
- - HttpServletResponse response = new MockHttpServletResponse();
- - controller.handleRequest(request, response);
- -
- - LoginCredential loginCredential = userDao.getLoginCredential(user);
- - assertNull(loginCredential.getSecretAnswer());
- - }
- -
- - @Test
- - public void shouldRejectEmptySecretQuestion() throws Exception {
- - LoginCredential loginCredential = userDao.getLoginCredential(user);
- - String originalQuestion = loginCredential.getSecretQuestion();
- -
- - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- - request.setParameter("secretQuestionPassword", "test");
- - request.setParameter("secretQuestionNew", "");
- -
- - String emptyAnswer = "test_answer";
- - request.setParameter("secretAnswerNew", emptyAnswer);
- - request.setParameter("secretAnswerConfirm", emptyAnswer);
- -
- - HttpServletResponse response = new MockHttpServletResponse();
- - controller.handleRequest(request, response);
- -
- - loginCredential = userDao.getLoginCredential(user);
- - assertEquals(originalQuestion, loginCredential.getSecretQuestion());
- - }
- + @Test
- + public void shouldChangeSecretQuestionAndAnswer() throws Exception {
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("secretQuestionPassword", "test");
- + request.setParameter("secretQuestionNew", "test_question");
- +
- + String answer = "test_answer";
- + request.setParameter("secretAnswerNew", answer);
- + request.setParameter("secretAnswerConfirm", answer);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + controller.handleRequest(request, response);
- +
- + LoginCredential loginCredential = userDao.getLoginCredential(user);
- + assertEquals(answer, loginCredential.getSecretAnswer());
- + }
- +
- + @Test
- + public void shouldRejectEmptySecretAnswer() throws Exception {
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("secretQuestionPassword", "test");
- + request.setParameter("secretQuestionNew", "test_question");
- +
- + String emptyAnswer = "";
- + request.setParameter("secretAnswerNew", emptyAnswer);
- + request.setParameter("secretAnswerConfirm", emptyAnswer);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + controller.handleRequest(request, response);
- +
- + LoginCredential loginCredential = userDao.getLoginCredential(user);
- + assertNull(loginCredential.getSecretAnswer());
- + }
- +
- + @Test
- + public void shouldRejectEmptySecretQuestion() throws Exception {
- + LoginCredential loginCredential = userDao.getLoginCredential(user);
- + String originalQuestion = loginCredential.getSecretQuestion();
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("secretQuestionPassword", "test");
- + request.setParameter("secretQuestionNew", "");
- +
- + String emptyAnswer = "test_answer";
- + request.setParameter("secretAnswerNew", emptyAnswer);
- + request.setParameter("secretAnswerConfirm", emptyAnswer);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + controller.handleRequest(request, response);
- +
- + loginCredential = userDao.getLoginCredential(user);
- + assertEquals(originalQuestion, loginCredential.getSecretQuestion());
- + }
- +
- + @Test
- + public void shouldRejectEmptyNotificationAddress() throws Exception {
- + String emptyAddress = "";
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("notification", "internal");
- + request.setParameter("notificationAddress", emptyAddress);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + ModelAndView modelAndView = controller.handleRequest(request, response);
- + assertEquals("", request.getParameter("notificationAddress"));
- +
- + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
- + "org.springframework.validation.BindingResult.location");
- + org.junit.Assert.assertTrue(bindingResult.hasErrors());
- -}
- + }
- +
- + @Test
- + public void shouldRejectInvalidNotificationAddress() throws Exception {
- + String incorrectAddress = "gayan@gmail";
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("notification", "internal");
- + request.setParameter("notificationAddress", incorrectAddress);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + ModelAndView modelAndView = controller.handleRequest(request, response);
- + assertNotNull(modelAndView.getModel().get("notificationAddress"));
- +
- + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
- + "org.springframework.validation.BindingResult.location");
- + org.junit.Assert.assertTrue(bindingResult.hasErrors());
- + }
- +
- + @Test
- + public void shouldAcceptValidNotificationAddress() throws Exception {
- + String correctAddress = "gayan@gmail.com";
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("notification", "internal");
- + request.setParameter("notificationAddress", correctAddress);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + ModelAndView modelAndView = controller.handleRequest(request, response);
- + assertNotNull(modelAndView.getModel().get("notificationAddress"));
- +
- + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
- + "org.springframework.validation.BindingResult.location");
- + org.junit.Assert.assertTrue(!bindingResult.hasErrors());
- + }
- +}
- Index: web/src/main/java/org/openmrs/web/controller/OptionsFormController.java
- ===================================================================
- --- web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision 23745)
- +++ web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision )
- @@ -17,6 +17,8 @@
- import java.util.HashMap;
- import java.util.Locale;
- import java.util.Map;
- +import java.util.regex.Matcher;
- +import java.util.regex.Pattern;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- @@ -47,279 +49,295 @@
- /**
- * This is the controller for the "My Profile" page. This lets logged in users set personal
- * preferences, update their own information, etc.
- - *
- + *
- * @see OptionsForm
- */
- public class OptionsFormController extends SimpleFormController {
- -
- - /** Logger for this class and subclasses */
- - protected final Log log = LogFactory.getLog(getClass());
- -
- - /**
- - * @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
- - * javax.servlet.http.HttpServletResponse, java.lang.Object,
- - * org.springframework.validation.BindException)
- - */
- - protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object object,
- - BindException errors) throws Exception {
- - OptionsForm opts = (OptionsForm) object;
- -
- - if (opts.getUsername().length() > 0) {
- - if (opts.getUsername().length() < 3) {
- - errors.rejectValue("username", "error.username.weak");
- - }
- - if (opts.getUsername().charAt(0) < 'A' || opts.getUsername().charAt(0) > 'z') {
- - errors.rejectValue("username", "error.username.invalid");
- - }
- -
- - }
- - if (opts.getUsername().length() > 0)
- -
- - if (!opts.getOldPassword().equals("")) {
- - if (opts.getNewPassword().equals(""))
- - errors.rejectValue("newPassword", "error.password.weak");
- - else if (!opts.getNewPassword().equals(opts.getConfirmPassword())) {
- - errors.rejectValue("newPassword", "error.password.match");
- - errors.rejectValue("confirmPassword", "error.password.match");
- - }
- - }
- -
- - if (!opts.getSecretQuestionPassword().equals("")) {
- - if (!opts.getSecretAnswerConfirm().equals(opts.getSecretAnswerNew())) {
- - errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.match");
- - errors.rejectValue("secretAnswerConfirm", "error.options.secretAnswer.match");
- - }
- - if (opts.getSecretAnswerNew().isEmpty()) {
- - errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.empty");
- - }
- - if (opts.getSecretQuestionNew().isEmpty()) {
- - errors.rejectValue("secretQuestionNew", "error.options.secretQuestion.empty");
- - }
- - }
- -
- - return super.processFormSubmission(request, response, object, errors);
- - }
- -
- - /**
- - * The onSubmit function receives the form/command object that was modified by the input form
- - * and saves it to the db
- - *
- - * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
- - * javax.servlet.http.HttpServletResponse, java.lang.Object,
- - * org.springframework.validation.BindException)
- - */
- - protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
- - BindException errors) throws Exception {
- -
- - HttpSession httpSession = request.getSession();
- -
- - String view = getFormView();
- -
- - if (!errors.hasErrors()) {
- - User loginUser = Context.getAuthenticatedUser();
- - UserService us = Context.getUserService();
- - User user = null;
- - try {
- - Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- - user = us.getUser(loginUser.getUserId());
- - }
- - finally {
- - Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- - }
- -
- - OptionsForm opts = (OptionsForm) obj;
- -
- - Map<String, String> properties = user.getUserProperties();
- -
- - properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION, opts.getDefaultLocation());
- -
- - Locale locale = WebUtil.normalizeLocale(opts.getDefaultLocale());
- - if (locale != null)
- - properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE, locale.toString());
- -
- - properties.put(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, WebUtil.sanitizeLocales(opts
- - .getProficientLocales()));
- - properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED, opts.getShowRetiredMessage().toString());
- - properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE, opts.getVerbose().toString());
- - properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION, opts.getNotification() == null ? "" : opts
- - .getNotification().toString());
- - properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS, opts.getNotificationAddress() == null ? ""
- - : opts.getNotificationAddress().toString());
- -
- - if (!opts.getOldPassword().equals("")) {
- - try {
- - String password = opts.getNewPassword();
- -
- - // check password strength
- - if (password.length() > 0) {
- - try {
- - OpenmrsUtil.validatePassword(user.getUsername(), password, String.valueOf(user.getUserId()));
- - }
- - catch (PasswordException e) {
- - errors.reject(e.getMessage());
- - }
- - if (password.equals(opts.getOldPassword()) && !errors.hasErrors())
- - errors.reject("error.password.different");
- - }
- -
- - if (!errors.hasErrors()) {
- - us.changePassword(opts.getOldPassword(), password);
- - opts.setSecretQuestionPassword(password);
- - new UserProperties(user.getUserProperties()).setSupposedToChangePassword(false);
- - }
- - }
- - catch (APIException e) {
- - errors.rejectValue("oldPassword", "error.password.match");
- - }
- - } else {
- - // if they left the old password blank but filled in new
- - // password
- - if (!opts.getNewPassword().equals("")) {
- - errors.rejectValue("oldPassword", "error.password.incorrect");
- - }
- - }
- -
- - if (!opts.getSecretQuestionPassword().equals("")) {
- - if (!errors.hasErrors()) {
- - try {
- - user.setSecretQuestion(opts.getSecretQuestionNew());
- - us.changeQuestionAnswer(opts.getSecretQuestionPassword(), opts.getSecretQuestionNew(), opts
- - .getSecretAnswerNew());
- - }
- - catch (APIException e) {
- - errors.rejectValue("secretQuestionPassword", "error.password.match");
- - }
- - }
- - } else if (!opts.getSecretAnswerNew().equals("")) {
- - // if they left the old password blank but filled in new
- - // password
- - errors.rejectValue("secretQuestionPassword", "error.password.incorrect");
- - }
- -
- - if (opts.getUsername().length() > 0 && !errors.hasErrors()) {
- - try {
- - Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- - if (us.hasDuplicateUsername(user)) {
- - errors.rejectValue("username", "error.username.taken");
- - }
- - }
- - finally {
- - Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- - }
- - }
- -
- +
- + /** Logger for this class and subclasses */
- + protected final Log log = LogFactory.getLog(getClass());
- +
- + /**
- + * @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
- + * javax.servlet.http.HttpServletResponse, java.lang.Object,
- + * org.springframework.validation.BindException)
- + */
- + protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object object,
- + BindException errors) throws Exception {
- + OptionsForm opts = (OptionsForm) object;
- +
- + if (opts.getUsername().length() > 0) {
- + if (opts.getUsername().length() < 3) {
- + errors.rejectValue("username", "error.username.weak");
- + }
- + if (opts.getUsername().charAt(0) < 'A' || opts.getUsername().charAt(0) > 'z') {
- + errors.rejectValue("username", "error.username.invalid");
- + }
- +
- + }
- + if (opts.getUsername().length() > 0)
- +
- + if (!opts.getOldPassword().equals("")) {
- + if (opts.getNewPassword().equals(""))
- + errors.rejectValue("newPassword", "error.password.weak");
- + else if (!opts.getNewPassword().equals(opts.getConfirmPassword())) {
- + errors.rejectValue("newPassword", "error.password.match");
- + errors.rejectValue("confirmPassword", "error.password.match");
- + }
- + }
- +
- + if (!opts.getSecretQuestionPassword().equals("")) {
- + if (!opts.getSecretAnswerConfirm().equals(opts.getSecretAnswerNew())) {
- + errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.match");
- + errors.rejectValue("secretAnswerConfirm", "error.options.secretAnswer.match");
- + }
- + if (opts.getSecretAnswerNew().isEmpty()) {
- + errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.empty");
- + }
- + if (opts.getSecretQuestionNew().isEmpty()) {
- + errors.rejectValue("secretQuestionNew", "error.options.secretQuestion.empty");
- + }
- + }
- +
- + return super.processFormSubmission(request, response, object, errors);
- + }
- +
- + /**
- + * The onSubmit function receives the form/command object that was modified by the input form
- + * and saves it to the db
- + *
- + * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
- + * javax.servlet.http.HttpServletResponse, java.lang.Object,
- + * org.springframework.validation.BindException)
- + */
- + protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
- + BindException errors) throws Exception {
- +
- + HttpSession httpSession = request.getSession();
- +
- + String view = getFormView();
- +
- + if (!errors.hasErrors()) {
- + User loginUser = Context.getAuthenticatedUser();
- + UserService us = Context.getUserService();
- + User user = null;
- + try {
- + Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- + user = us.getUser(loginUser.getUserId());
- + }
- + finally {
- + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- + }
- +
- + OptionsForm opts = (OptionsForm) obj;
- +
- + Map<String, String> properties = user.getUserProperties();
- +
- + properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION, opts.getDefaultLocation());
- +
- + Locale locale = WebUtil.normalizeLocale(opts.getDefaultLocale());
- + if (locale != null)
- + properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE, locale.toString());
- +
- + properties.put(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, WebUtil.sanitizeLocales(opts
- + .getProficientLocales()));
- + properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED, opts.getShowRetiredMessage().toString());
- + properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE, opts.getVerbose().toString());
- + properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION, opts.getNotification() == null ? "" : opts
- + .getNotification().toString());
- + properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS, opts.getNotificationAddress() == null ? ""
- + : opts.getNotificationAddress().toString());
- +
- + if (!opts.getOldPassword().equals("")) {
- + try {
- + String password = opts.getNewPassword();
- +
- + // check password strength
- + if (password.length() > 0) {
- + try {
- + OpenmrsUtil.validatePassword(user.getUsername(), password, String.valueOf(user.getUserId()));
- + }
- + catch (PasswordException e) {
- + errors.reject(e.getMessage());
- + }
- + if (password.equals(opts.getOldPassword()) && !errors.hasErrors())
- + errors.reject("error.password.different");
- + }
- +
- + if (!errors.hasErrors()) {
- + us.changePassword(opts.getOldPassword(), password);
- + opts.setSecretQuestionPassword(password);
- + new UserProperties(user.getUserProperties()).setSupposedToChangePassword(false);
- + }
- + }
- + catch (APIException e) {
- + errors.rejectValue("oldPassword", "error.password.match");
- + }
- + } else {
- + // if they left the old password blank but filled in new
- + // password
- + if (!opts.getNewPassword().equals("")) {
- + errors.rejectValue("oldPassword", "error.password.incorrect");
- + }
- + }
- +
- + if (!opts.getSecretQuestionPassword().equals("")) {
- + if (!errors.hasErrors()) {
- + try {
- + user.setSecretQuestion(opts.getSecretQuestionNew());
- + us.changeQuestionAnswer(opts.getSecretQuestionPassword(), opts.getSecretQuestionNew(), opts
- + .getSecretAnswerNew());
- + }
- + catch (APIException e) {
- + errors.rejectValue("secretQuestionPassword", "error.password.match");
- + }
- + }
- + } else if (!opts.getSecretAnswerNew().equals("")) {
- + // if they left the old password blank but filled in new
- + // password
- + errors.rejectValue("secretQuestionPassword", "error.password.incorrect");
- + }
- +
- + if (opts.getUsername().length() > 0 && !errors.hasErrors()) {
- + try {
- + Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- + if (us.hasDuplicateUsername(user)) {
- + errors.rejectValue("username", "error.username.taken");
- + }
- + }
- + finally {
- + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- + }
- + }
- +
- + String notifyType = opts.getNotification();
- + if (notifyType != null) {
- + if (notifyType.equals("internal") || notifyType.equals("internalProtected") || notifyType.equals("email")) {
- + if (opts.getNotificationAddress().isEmpty()) {
- + errors.reject("error.options.notificationAddress.empty");
- + } else {
- + String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
- + Pattern pattern = Pattern.compile(EMAIL_PATTERN);
- + Matcher matcher = pattern.matcher(opts.getNotificationAddress());
- + if (matcher.matches() == false) {
- + errors.reject("error.options.notificationAddress.invalid");
- + }
- + }
- + }
- + }
- +
- - if (!errors.hasErrors()) {
- -
- - user.setUsername(opts.getUsername());
- - user.setUserProperties(properties);
- -
- - // new name
- - PersonName newPersonName = opts.getPersonName();
- -
- - // existing name
- - PersonName existingPersonName = user.getPersonName();
- -
- - // if two are not equal then make the new one the preferred,
- - // make the old one voided
- - if (!existingPersonName.equalsContent(newPersonName)) {
- - existingPersonName.setPreferred(false);
- - existingPersonName.setVoided(true);
- - existingPersonName.setVoidedBy(user);
- - existingPersonName.setDateVoided(new Date());
- - existingPersonName.setVoidReason("Changed name on own options form");
- -
- - newPersonName.setPreferred(true);
- - user.addName(newPersonName);
- - }
- -
- - try {
- - Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS);
- - Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- - us.saveUser(user, null);
- - //trigger updating of the javascript file cache
- - PseudoStaticContentController.invalidateCachedResources(properties);
- - // update login user object so that the new name is visible
- - // in the webapp
- - Context.refreshAuthenticatedUser();
- - }
- - finally {
- - Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS);
- - Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- - }
- -
- - httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "options.saved");
- - } else {
- - return super.processFormSubmission(request, response, opts, errors);
- - }
- -
- - view = getSuccessView();
- - }
- - return new ModelAndView(new RedirectView(view));
- - }
- -
- - /**
- - * This is called prior to displaying a form for the first time. It tells Spring the
- - * form/command object to load into the request
- - *
- - * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
- - */
- - protected Object formBackingObject(HttpServletRequest request) throws ServletException {
- -
- - OptionsForm opts = new OptionsForm();
- -
- - if (Context.isAuthenticated()) {
- - User user = Context.getAuthenticatedUser();
- -
- - Map<String, String> props = user.getUserProperties();
- - opts.setDefaultLocation(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION));
- - opts.setDefaultLocale(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE));
- - opts.setProficientLocales(props.get(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES));
- - opts.setShowRetiredMessage(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED)));
- - opts.setVerbose(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE)));
- - opts.setUsername(user.getUsername());
- - opts.setSecretQuestionNew(user.getSecretQuestion());
- - // Get a copy of the current person name and clear the id so that
- - // they are separate objects
- - PersonName personName = PersonName.newInstance(user.getPersonName());
- - personName.setPersonNameId(null);
- - opts.setPersonName(personName);
- - opts.setNotification(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION));
- - opts.setNotificationAddress(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS));
- - }
- -
- - return opts;
- - }
- -
- - /**
- - * Called prior to form display. Allows for data to be put in the request to be used in the view
- - *
- - * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
- - */
- - protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
- -
- - HttpSession httpSession = request.getSession();
- -
- - Map<String, Object> map = new HashMap<String, Object>();
- -
- - if (Context.isAuthenticated()) {
- -
- - LocationService ls = Context.getLocationService();
- -
- - // set location options
- - map.put("locations", ls.getAllLocations());
- -
- - // set language/locale options
- - map.put("languages", Context.getAdministrationService().getPresentationLocales());
- -
- - String resetPassword = (String) httpSession.getAttribute("resetPassword");
- - if (resetPassword == null)
- - resetPassword = "";
- - else
- - httpSession.removeAttribute("resetPassword");
- - map.put("resetPassword", resetPassword);
- -
- - }
- -
- - return map;
- - }
- + if (!errors.hasErrors()) {
- +
- + user.setUsername(opts.getUsername());
- + user.setUserProperties(properties);
- +
- + // new name
- + PersonName newPersonName = opts.getPersonName();
- +
- + // existing name
- + PersonName existingPersonName = user.getPersonName();
- +
- + // if two are not equal then make the new one the preferred,
- + // make the old one voided
- + if (!existingPersonName.equalsContent(newPersonName)) {
- + existingPersonName.setPreferred(false);
- + existingPersonName.setVoided(true);
- + existingPersonName.setVoidedBy(user);
- + existingPersonName.setDateVoided(new Date());
- + existingPersonName.setVoidReason("Changed name on own options form");
- +
- + newPersonName.setPreferred(true);
- + user.addName(newPersonName);
- + }
- +
- + try {
- + Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS);
- + Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- + us.saveUser(user, null);
- + //trigger updating of the javascript file cache
- + PseudoStaticContentController.invalidateCachedResources(properties);
- + // update login user object so that the new name is visible
- + // in the webapp
- + Context.refreshAuthenticatedUser();
- + }
- + finally {
- + Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS);
- + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
- + }
- +
- + httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "options.saved");
- + } else {
- + return super.processFormSubmission(request, response, opts, errors);
- + }
- +
- + view = getSuccessView();
- + }
- + return new ModelAndView(new RedirectView(view));
- + }
- +
- + /**
- + * This is called prior to displaying a form for the first time. It tells Spring the
- + * form/command object to load into the request
- + *
- + * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
- + */
- + protected Object formBackingObject(HttpServletRequest request) throws ServletException {
- +
- + OptionsForm opts = new OptionsForm();
- +
- + if (Context.isAuthenticated()) {
- + User user = Context.getAuthenticatedUser();
- +
- + Map<String, String> props = user.getUserProperties();
- + opts.setDefaultLocation(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION));
- + opts.setDefaultLocale(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE));
- + opts.setProficientLocales(props.get(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES));
- + opts.setShowRetiredMessage(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED)));
- + opts.setVerbose(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE)));
- + opts.setUsername(user.getUsername());
- + opts.setSecretQuestionNew(user.getSecretQuestion());
- + // Get a copy of the current person name and clear the id so that
- + // they are separate objects
- + PersonName personName = PersonName.newInstance(user.getPersonName());
- + personName.setPersonNameId(null);
- + opts.setPersonName(personName);
- + opts.setNotification(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION));
- + opts.setNotificationAddress(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS));
- + }
- +
- + return opts;
- + }
- +
- + /**
- + * Called prior to form display. Allows for data to be put in the request to be used in the view
- + *
- + * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
- + */
- + protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
- +
- + HttpSession httpSession = request.getSession();
- +
- + Map<String, Object> map = new HashMap<String, Object>();
- +
- + if (Context.isAuthenticated()) {
- +
- + LocationService ls = Context.getLocationService();
- +
- + // set location options
- + map.put("locations", ls.getAllLocations());
- +
- + // set language/locale options
- + map.put("languages", Context.getAdministrationService().getPresentationLocales());
- +
- + String resetPassword = (String) httpSession.getAttribute("resetPassword");
- + if (resetPassword == null)
- + resetPassword = "";
- + else
- + httpSession.removeAttribute("resetPassword");
- + map.put("resetPassword", resetPassword);
- +
- + }
- +
- + return map;
- + }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement