Advertisement
Guest User

3

a guest
Apr 3rd, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.23 KB | None | 0 0
  1. Index: webapp/src/main/webapp/WEB-INF/messages.properties
  2. ===================================================================
  3. --- webapp/src/main/webapp/WEB-INF/messages.properties (revision 26253)
  4. +++ webapp/src/main/webapp/WEB-INF/messages.properties (revision )
  5. @@ -339,6 +339,9 @@
  6. error.options.secretAnswer.match=Answers do not match
  7. error.options.secretAnswer.empty=Answer should not be empty
  8. error.options.secretQuestion.empty=Question should not be empty
  9. +error.options.notificationAddress.empty=Notifications-Email address should not be empty
  10. +error.options.notificationAddress.invalid=Notifications-Email address should be a valid one
  11. +
  12. error.username.weak=Invalid username. Must be at least 6 characters
  13. error.username.invalid=Invalid username. Username must be alphanumeric and cannot start with a number
  14. error.retired.requireMetadata=Who retired this and why?
  15. Index: web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java
  16. ===================================================================
  17. --- web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision 17337)
  18. +++ web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision )
  19. @@ -1,20 +1,28 @@
  20. package org.openmrs.web.controller;
  21.  
  22. -import static org.junit.Assert.assertEquals;
  23. -import static org.junit.Assert.assertNull;
  24. -
  25. import javax.servlet.http.HttpServletResponse;
  26.  
  27. +import org.databene.commons.Assert;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.openmrs.User;
  31. import org.openmrs.api.context.Context;
  32. import org.openmrs.api.db.LoginCredential;
  33. import org.openmrs.api.db.UserDAO;
  34. +import org.openmrs.test.Verifies;
  35. +import org.openmrs.util.OpenmrsConstants;
  36. +import org.openmrs.web.OptionsForm;
  37. +import org.openmrs.web.controller.encounter.LocationFormController;
  38. import org.openmrs.web.test.BaseWebContextSensitiveTest;
  39. import org.springframework.mock.web.MockHttpServletRequest;
  40. import org.springframework.mock.web.MockHttpServletResponse;
  41. +import org.springframework.validation.BeanPropertyBindingResult;
  42. +import org.springframework.validation.BindingResult;
  43. +import org.springframework.web.servlet.ModelAndView;
  44.  
  45. +import static org.junit.Assert.*;
  46. +import static org.junit.Assert.assertEquals;
  47. +
  48. public class OptionsFormControllerTest extends BaseWebContextSensitiveTest {
  49.  
  50. private User user;
  51. @@ -31,57 +39,112 @@
  52. userDao = (UserDAO) applicationContext.getBean("userDAO");
  53. }
  54.  
  55. - @Test
  56. - public void shouldChangeSecretQuestionAndAnswer() throws Exception {
  57. - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  58. - request.setParameter("secretQuestionPassword", "test");
  59. - request.setParameter("secretQuestionNew", "test_question");
  60. -
  61. - String answer = "test_answer";
  62. - request.setParameter("secretAnswerNew", answer);
  63. - request.setParameter("secretAnswerConfirm", answer);
  64. -
  65. - HttpServletResponse response = new MockHttpServletResponse();
  66. - controller.handleRequest(request, response);
  67. -
  68. - LoginCredential loginCredential = userDao.getLoginCredential(user);
  69. - assertEquals(answer, loginCredential.getSecretAnswer());
  70. - }
  71. -
  72. - @Test
  73. - public void shouldRejectEmptySecretAnswer() throws Exception {
  74. - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  75. - request.setParameter("secretQuestionPassword", "test");
  76. - request.setParameter("secretQuestionNew", "test_question");
  77. -
  78. - String emptyAnswer = "";
  79. - request.setParameter("secretAnswerNew", emptyAnswer);
  80. - request.setParameter("secretAnswerConfirm", emptyAnswer);
  81. -
  82. - HttpServletResponse response = new MockHttpServletResponse();
  83. - controller.handleRequest(request, response);
  84. -
  85. - LoginCredential loginCredential = userDao.getLoginCredential(user);
  86. - assertNull(loginCredential.getSecretAnswer());
  87. - }
  88. -
  89. - @Test
  90. - public void shouldRejectEmptySecretQuestion() throws Exception {
  91. - LoginCredential loginCredential = userDao.getLoginCredential(user);
  92. - String originalQuestion = loginCredential.getSecretQuestion();
  93. -
  94. - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  95. - request.setParameter("secretQuestionPassword", "test");
  96. - request.setParameter("secretQuestionNew", "");
  97. -
  98. - String emptyAnswer = "test_answer";
  99. - request.setParameter("secretAnswerNew", emptyAnswer);
  100. - request.setParameter("secretAnswerConfirm", emptyAnswer);
  101. -
  102. - HttpServletResponse response = new MockHttpServletResponse();
  103. - controller.handleRequest(request, response);
  104. -
  105. - loginCredential = userDao.getLoginCredential(user);
  106. - assertEquals(originalQuestion, loginCredential.getSecretQuestion());
  107. - }
  108. + @Test
  109. + public void shouldChangeSecretQuestionAndAnswer() throws Exception {
  110. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  111. + request.setParameter("secretQuestionPassword", "test");
  112. + request.setParameter("secretQuestionNew", "test_question");
  113. +
  114. + String answer = "test_answer";
  115. + request.setParameter("secretAnswerNew", answer);
  116. + request.setParameter("secretAnswerConfirm", answer);
  117. +
  118. + HttpServletResponse response = new MockHttpServletResponse();
  119. + controller.handleRequest(request, response);
  120. +
  121. + LoginCredential loginCredential = userDao.getLoginCredential(user);
  122. + assertEquals(answer, loginCredential.getSecretAnswer());
  123. + }
  124. +
  125. + @Test
  126. + public void shouldRejectEmptySecretAnswer() throws Exception {
  127. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  128. + request.setParameter("secretQuestionPassword", "test");
  129. + request.setParameter("secretQuestionNew", "test_question");
  130. +
  131. + String emptyAnswer = "";
  132. + request.setParameter("secretAnswerNew", emptyAnswer);
  133. + request.setParameter("secretAnswerConfirm", emptyAnswer);
  134. +
  135. + HttpServletResponse response = new MockHttpServletResponse();
  136. + controller.handleRequest(request, response);
  137. +
  138. + LoginCredential loginCredential = userDao.getLoginCredential(user);
  139. + assertNull(loginCredential.getSecretAnswer());
  140. + }
  141. +
  142. + @Test
  143. + public void shouldRejectEmptySecretQuestion() throws Exception {
  144. + LoginCredential loginCredential = userDao.getLoginCredential(user);
  145. + String originalQuestion = loginCredential.getSecretQuestion();
  146. +
  147. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  148. + request.setParameter("secretQuestionPassword", "test");
  149. + request.setParameter("secretQuestionNew", "");
  150. +
  151. + String emptyAnswer = "test_answer";
  152. + request.setParameter("secretAnswerNew", emptyAnswer);
  153. + request.setParameter("secretAnswerConfirm", emptyAnswer);
  154. +
  155. + HttpServletResponse response = new MockHttpServletResponse();
  156. + controller.handleRequest(request, response);
  157. +
  158. + loginCredential = userDao.getLoginCredential(user);
  159. + assertEquals(originalQuestion, loginCredential.getSecretQuestion());
  160. + }
  161. +
  162. + @Test
  163. + public void shouldRejectEmptyNotificationAddress() throws Exception {
  164. + String emptyAddress = "";
  165. +
  166. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  167. + request.setParameter("notification", "internal");
  168. + request.setParameter("notificationAddress", emptyAddress);
  169. +
  170. + HttpServletResponse response = new MockHttpServletResponse();
  171. + ModelAndView modelAndView = controller.handleRequest(request, response);
  172. + assertEquals("", request.getParameter("notificationAddress"));
  173. +
  174. + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
  175. + "org.springframework.validation.BindingResult.opts");
  176. + org.junit.Assert.assertTrue(bindingResult.hasErrors());
  177. -}
  178. + }
  179. +
  180. + @Test
  181. + public void shouldRejectInvalidNotificationAddress() throws Exception {
  182. + String incorrectAddress = "gayan@gmail";
  183. +
  184. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  185. + request.setParameter("notification", "internal");
  186. + request.setParameter("notificationAddress", incorrectAddress);
  187. +
  188. + HttpServletResponse response = new MockHttpServletResponse();
  189. + ModelAndView modelAndView = controller.handleRequest(request, response);
  190. +
  191. + OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
  192. + assertEquals(incorrectAddress, optionsForm.getNotificationAddress());
  193. +
  194. + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
  195. + "org.springframework.validation.BindingResult.opts");
  196. + org.junit.Assert.assertTrue(bindingResult.hasErrors());
  197. + }
  198. +
  199. + @Test
  200. + public void shouldAcceptValidNotificationAddress() throws Exception {
  201. + String correctAddress = "gayan@gmail.com";
  202. +
  203. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  204. + request.setParameter("notification", "internal");
  205. + request.setParameter("notificationAddress", correctAddress);
  206. +
  207. + HttpServletResponse response = new MockHttpServletResponse();
  208. + ModelAndView modelAndView = controller.handleRequest(request, response);
  209. +
  210. + OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
  211. + assertEquals(correctAddress, optionsForm.getNotificationAddress());
  212. +
  213. + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
  214. + "org.springframework.validation.BindingResult.opts");
  215. + org.junit.Assert.assertFalse(bindingResult.hasErrors());
  216. + }
  217. +}
  218. Index: web/src/main/java/org/openmrs/web/controller/OptionsFormController.java
  219. ===================================================================
  220. --- web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision 23745)
  221. +++ web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision )
  222. @@ -17,6 +17,8 @@
  223. import java.util.HashMap;
  224. import java.util.Locale;
  225. import java.util.Map;
  226. +import java.util.regex.Matcher;
  227. +import java.util.regex.Pattern;
  228.  
  229. import javax.servlet.ServletException;
  230. import javax.servlet.http.HttpServletRequest;
  231. @@ -47,279 +49,295 @@
  232. /**
  233. * This is the controller for the "My Profile" page. This lets logged in users set personal
  234. * preferences, update their own information, etc.
  235. - *
  236. + *
  237. * @see OptionsForm
  238. */
  239. public class OptionsFormController extends SimpleFormController {
  240. -
  241. - /** Logger for this class and subclasses */
  242. - protected final Log log = LogFactory.getLog(getClass());
  243. -
  244. - /**
  245. - * @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
  246. - * javax.servlet.http.HttpServletResponse, java.lang.Object,
  247. - * org.springframework.validation.BindException)
  248. - */
  249. - protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object object,
  250. - BindException errors) throws Exception {
  251. - OptionsForm opts = (OptionsForm) object;
  252. -
  253. - if (opts.getUsername().length() > 0) {
  254. - if (opts.getUsername().length() < 3) {
  255. - errors.rejectValue("username", "error.username.weak");
  256. - }
  257. - if (opts.getUsername().charAt(0) < 'A' || opts.getUsername().charAt(0) > 'z') {
  258. - errors.rejectValue("username", "error.username.invalid");
  259. - }
  260. -
  261. - }
  262. - if (opts.getUsername().length() > 0)
  263. -
  264. - if (!opts.getOldPassword().equals("")) {
  265. - if (opts.getNewPassword().equals(""))
  266. - errors.rejectValue("newPassword", "error.password.weak");
  267. - else if (!opts.getNewPassword().equals(opts.getConfirmPassword())) {
  268. - errors.rejectValue("newPassword", "error.password.match");
  269. - errors.rejectValue("confirmPassword", "error.password.match");
  270. - }
  271. - }
  272. -
  273. - if (!opts.getSecretQuestionPassword().equals("")) {
  274. - if (!opts.getSecretAnswerConfirm().equals(opts.getSecretAnswerNew())) {
  275. - errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.match");
  276. - errors.rejectValue("secretAnswerConfirm", "error.options.secretAnswer.match");
  277. - }
  278. - if (opts.getSecretAnswerNew().isEmpty()) {
  279. - errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.empty");
  280. - }
  281. - if (opts.getSecretQuestionNew().isEmpty()) {
  282. - errors.rejectValue("secretQuestionNew", "error.options.secretQuestion.empty");
  283. - }
  284. - }
  285. -
  286. - return super.processFormSubmission(request, response, object, errors);
  287. - }
  288. -
  289. - /**
  290. - * The onSubmit function receives the form/command object that was modified by the input form
  291. - * and saves it to the db
  292. - *
  293. - * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  294. - * javax.servlet.http.HttpServletResponse, java.lang.Object,
  295. - * org.springframework.validation.BindException)
  296. - */
  297. - protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  298. - BindException errors) throws Exception {
  299. -
  300. - HttpSession httpSession = request.getSession();
  301. -
  302. - String view = getFormView();
  303. -
  304. - if (!errors.hasErrors()) {
  305. - User loginUser = Context.getAuthenticatedUser();
  306. - UserService us = Context.getUserService();
  307. - User user = null;
  308. - try {
  309. - Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  310. - user = us.getUser(loginUser.getUserId());
  311. - }
  312. - finally {
  313. - Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  314. - }
  315. -
  316. - OptionsForm opts = (OptionsForm) obj;
  317. -
  318. - Map<String, String> properties = user.getUserProperties();
  319. -
  320. - properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION, opts.getDefaultLocation());
  321. -
  322. - Locale locale = WebUtil.normalizeLocale(opts.getDefaultLocale());
  323. - if (locale != null)
  324. - properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE, locale.toString());
  325. -
  326. - properties.put(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, WebUtil.sanitizeLocales(opts
  327. - .getProficientLocales()));
  328. - properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED, opts.getShowRetiredMessage().toString());
  329. - properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE, opts.getVerbose().toString());
  330. - properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION, opts.getNotification() == null ? "" : opts
  331. - .getNotification().toString());
  332. - properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS, opts.getNotificationAddress() == null ? ""
  333. - : opts.getNotificationAddress().toString());
  334. -
  335. - if (!opts.getOldPassword().equals("")) {
  336. - try {
  337. - String password = opts.getNewPassword();
  338. -
  339. - // check password strength
  340. - if (password.length() > 0) {
  341. - try {
  342. - OpenmrsUtil.validatePassword(user.getUsername(), password, String.valueOf(user.getUserId()));
  343. - }
  344. - catch (PasswordException e) {
  345. - errors.reject(e.getMessage());
  346. - }
  347. - if (password.equals(opts.getOldPassword()) && !errors.hasErrors())
  348. - errors.reject("error.password.different");
  349. - }
  350. -
  351. - if (!errors.hasErrors()) {
  352. - us.changePassword(opts.getOldPassword(), password);
  353. - opts.setSecretQuestionPassword(password);
  354. - new UserProperties(user.getUserProperties()).setSupposedToChangePassword(false);
  355. - }
  356. - }
  357. - catch (APIException e) {
  358. - errors.rejectValue("oldPassword", "error.password.match");
  359. - }
  360. - } else {
  361. - // if they left the old password blank but filled in new
  362. - // password
  363. - if (!opts.getNewPassword().equals("")) {
  364. - errors.rejectValue("oldPassword", "error.password.incorrect");
  365. - }
  366. - }
  367. -
  368. - if (!opts.getSecretQuestionPassword().equals("")) {
  369. - if (!errors.hasErrors()) {
  370. - try {
  371. - user.setSecretQuestion(opts.getSecretQuestionNew());
  372. - us.changeQuestionAnswer(opts.getSecretQuestionPassword(), opts.getSecretQuestionNew(), opts
  373. - .getSecretAnswerNew());
  374. - }
  375. - catch (APIException e) {
  376. - errors.rejectValue("secretQuestionPassword", "error.password.match");
  377. - }
  378. - }
  379. - } else if (!opts.getSecretAnswerNew().equals("")) {
  380. - // if they left the old password blank but filled in new
  381. - // password
  382. - errors.rejectValue("secretQuestionPassword", "error.password.incorrect");
  383. - }
  384. -
  385. - if (opts.getUsername().length() > 0 && !errors.hasErrors()) {
  386. - try {
  387. - Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  388. - if (us.hasDuplicateUsername(user)) {
  389. - errors.rejectValue("username", "error.username.taken");
  390. - }
  391. - }
  392. - finally {
  393. - Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  394. - }
  395. - }
  396. -
  397. +
  398. + /** Logger for this class and subclasses */
  399. + protected final Log log = LogFactory.getLog(getClass());
  400. +
  401. + /**
  402. + * @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
  403. + * javax.servlet.http.HttpServletResponse, java.lang.Object,
  404. + * org.springframework.validation.BindException)
  405. + */
  406. + protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object object,
  407. + BindException errors) throws Exception {
  408. + OptionsForm opts = (OptionsForm) object;
  409. +
  410. + if (opts.getUsername().length() > 0) {
  411. + if (opts.getUsername().length() < 3) {
  412. + errors.rejectValue("username", "error.username.weak");
  413. + }
  414. + if (opts.getUsername().charAt(0) < 'A' || opts.getUsername().charAt(0) > 'z') {
  415. + errors.rejectValue("username", "error.username.invalid");
  416. + }
  417. +
  418. + }
  419. + if (opts.getUsername().length() > 0)
  420. +
  421. + if (!opts.getOldPassword().equals("")) {
  422. + if (opts.getNewPassword().equals(""))
  423. + errors.rejectValue("newPassword", "error.password.weak");
  424. + else if (!opts.getNewPassword().equals(opts.getConfirmPassword())) {
  425. + errors.rejectValue("newPassword", "error.password.match");
  426. + errors.rejectValue("confirmPassword", "error.password.match");
  427. + }
  428. + }
  429. +
  430. + if (!opts.getSecretQuestionPassword().equals("")) {
  431. + if (!opts.getSecretAnswerConfirm().equals(opts.getSecretAnswerNew())) {
  432. + errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.match");
  433. + errors.rejectValue("secretAnswerConfirm", "error.options.secretAnswer.match");
  434. + }
  435. + if (opts.getSecretAnswerNew().isEmpty()) {
  436. + errors.rejectValue("secretAnswerNew", "error.options.secretAnswer.empty");
  437. + }
  438. + if (opts.getSecretQuestionNew().isEmpty()) {
  439. + errors.rejectValue("secretQuestionNew", "error.options.secretQuestion.empty");
  440. + }
  441. + }
  442. +
  443. + return super.processFormSubmission(request, response, object, errors);
  444. + }
  445. +
  446. + /**
  447. + * The onSubmit function receives the form/command object that was modified by the input form
  448. + * and saves it to the db
  449. + *
  450. + * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  451. + * javax.servlet.http.HttpServletResponse, java.lang.Object,
  452. + * org.springframework.validation.BindException)
  453. + */
  454. + protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  455. + BindException errors) throws Exception {
  456. +
  457. + HttpSession httpSession = request.getSession();
  458. +
  459. + String view = getFormView();
  460. +
  461. + if (!errors.hasErrors()) {
  462. + User loginUser = Context.getAuthenticatedUser();
  463. + UserService us = Context.getUserService();
  464. + User user = null;
  465. + try {
  466. + Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  467. + user = us.getUser(loginUser.getUserId());
  468. + }
  469. + finally {
  470. + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  471. + }
  472. +
  473. + OptionsForm opts = (OptionsForm) obj;
  474. +
  475. + Map<String, String> properties = user.getUserProperties();
  476. +
  477. + properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION, opts.getDefaultLocation());
  478. +
  479. + Locale locale = WebUtil.normalizeLocale(opts.getDefaultLocale());
  480. + if (locale != null)
  481. + properties.put(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE, locale.toString());
  482. +
  483. + properties.put(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, WebUtil.sanitizeLocales(opts
  484. + .getProficientLocales()));
  485. + properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED, opts.getShowRetiredMessage().toString());
  486. + properties.put(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE, opts.getVerbose().toString());
  487. + properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION, opts.getNotification() == null ? "" : opts
  488. + .getNotification().toString());
  489. + properties.put(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS, opts.getNotificationAddress() == null ? ""
  490. + : opts.getNotificationAddress().toString());
  491. +
  492. + if (!opts.getOldPassword().equals("")) {
  493. + try {
  494. + String password = opts.getNewPassword();
  495. +
  496. + // check password strength
  497. + if (password.length() > 0) {
  498. + try {
  499. + OpenmrsUtil.validatePassword(user.getUsername(), password, String.valueOf(user.getUserId()));
  500. + }
  501. + catch (PasswordException e) {
  502. + errors.reject(e.getMessage());
  503. + }
  504. + if (password.equals(opts.getOldPassword()) && !errors.hasErrors())
  505. + errors.reject("error.password.different");
  506. + }
  507. +
  508. + if (!errors.hasErrors()) {
  509. + us.changePassword(opts.getOldPassword(), password);
  510. + opts.setSecretQuestionPassword(password);
  511. + new UserProperties(user.getUserProperties()).setSupposedToChangePassword(false);
  512. + }
  513. + }
  514. + catch (APIException e) {
  515. + errors.rejectValue("oldPassword", "error.password.match");
  516. + }
  517. + } else {
  518. + // if they left the old password blank but filled in new
  519. + // password
  520. + if (!opts.getNewPassword().equals("")) {
  521. + errors.rejectValue("oldPassword", "error.password.incorrect");
  522. + }
  523. + }
  524. +
  525. + if (!opts.getSecretQuestionPassword().equals("")) {
  526. + if (!errors.hasErrors()) {
  527. + try {
  528. + user.setSecretQuestion(opts.getSecretQuestionNew());
  529. + us.changeQuestionAnswer(opts.getSecretQuestionPassword(), opts.getSecretQuestionNew(), opts
  530. + .getSecretAnswerNew());
  531. + }
  532. + catch (APIException e) {
  533. + errors.rejectValue("secretQuestionPassword", "error.password.match");
  534. + }
  535. + }
  536. + } else if (!opts.getSecretAnswerNew().equals("")) {
  537. + // if they left the old password blank but filled in new
  538. + // password
  539. + errors.rejectValue("secretQuestionPassword", "error.password.incorrect");
  540. + }
  541. +
  542. + if (opts.getUsername().length() > 0 && !errors.hasErrors()) {
  543. + try {
  544. + Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  545. + if (us.hasDuplicateUsername(user)) {
  546. + errors.rejectValue("username", "error.username.taken");
  547. + }
  548. + }
  549. + finally {
  550. + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  551. + }
  552. + }
  553. +
  554. + String notifyType = opts.getNotification();
  555. + if (notifyType != null) {
  556. + if (notifyType.equals("internal") || notifyType.equals("internalProtected") || notifyType.equals("email")) {
  557. + if (opts.getNotificationAddress().isEmpty()) {
  558. + errors.reject("error.options.notificationAddress.empty");
  559. + } else {
  560. + String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  561. + Pattern pattern = Pattern.compile(EMAIL_PATTERN);
  562. + Matcher matcher = pattern.matcher(opts.getNotificationAddress());
  563. + if (matcher.matches() == false) {
  564. + errors.reject("error.options.notificationAddress.invalid");
  565. + }
  566. + }
  567. + }
  568. + }
  569. +
  570. - if (!errors.hasErrors()) {
  571. -
  572. - user.setUsername(opts.getUsername());
  573. - user.setUserProperties(properties);
  574. -
  575. - // new name
  576. - PersonName newPersonName = opts.getPersonName();
  577. -
  578. - // existing name
  579. - PersonName existingPersonName = user.getPersonName();
  580. -
  581. - // if two are not equal then make the new one the preferred,
  582. - // make the old one voided
  583. - if (!existingPersonName.equalsContent(newPersonName)) {
  584. - existingPersonName.setPreferred(false);
  585. - existingPersonName.setVoided(true);
  586. - existingPersonName.setVoidedBy(user);
  587. - existingPersonName.setDateVoided(new Date());
  588. - existingPersonName.setVoidReason("Changed name on own options form");
  589. -
  590. - newPersonName.setPreferred(true);
  591. - user.addName(newPersonName);
  592. - }
  593. -
  594. - try {
  595. - Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS);
  596. - Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  597. - us.saveUser(user, null);
  598. - //trigger updating of the javascript file cache
  599. - PseudoStaticContentController.invalidateCachedResources(properties);
  600. - // update login user object so that the new name is visible
  601. - // in the webapp
  602. - Context.refreshAuthenticatedUser();
  603. - }
  604. - finally {
  605. - Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS);
  606. - Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  607. - }
  608. -
  609. - httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "options.saved");
  610. - } else {
  611. - return super.processFormSubmission(request, response, opts, errors);
  612. - }
  613. -
  614. - view = getSuccessView();
  615. - }
  616. - return new ModelAndView(new RedirectView(view));
  617. - }
  618. -
  619. - /**
  620. - * This is called prior to displaying a form for the first time. It tells Spring the
  621. - * form/command object to load into the request
  622. - *
  623. - * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  624. - */
  625. - protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  626. -
  627. - OptionsForm opts = new OptionsForm();
  628. -
  629. - if (Context.isAuthenticated()) {
  630. - User user = Context.getAuthenticatedUser();
  631. -
  632. - Map<String, String> props = user.getUserProperties();
  633. - opts.setDefaultLocation(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION));
  634. - opts.setDefaultLocale(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE));
  635. - opts.setProficientLocales(props.get(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES));
  636. - opts.setShowRetiredMessage(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED)));
  637. - opts.setVerbose(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE)));
  638. - opts.setUsername(user.getUsername());
  639. - opts.setSecretQuestionNew(user.getSecretQuestion());
  640. - // Get a copy of the current person name and clear the id so that
  641. - // they are separate objects
  642. - PersonName personName = PersonName.newInstance(user.getPersonName());
  643. - personName.setPersonNameId(null);
  644. - opts.setPersonName(personName);
  645. - opts.setNotification(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION));
  646. - opts.setNotificationAddress(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS));
  647. - }
  648. -
  649. - return opts;
  650. - }
  651. -
  652. - /**
  653. - * Called prior to form display. Allows for data to be put in the request to be used in the view
  654. - *
  655. - * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
  656. - */
  657. - protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
  658. -
  659. - HttpSession httpSession = request.getSession();
  660. -
  661. - Map<String, Object> map = new HashMap<String, Object>();
  662. -
  663. - if (Context.isAuthenticated()) {
  664. -
  665. - LocationService ls = Context.getLocationService();
  666. -
  667. - // set location options
  668. - map.put("locations", ls.getAllLocations());
  669. -
  670. - // set language/locale options
  671. - map.put("languages", Context.getAdministrationService().getPresentationLocales());
  672. -
  673. - String resetPassword = (String) httpSession.getAttribute("resetPassword");
  674. - if (resetPassword == null)
  675. - resetPassword = "";
  676. - else
  677. - httpSession.removeAttribute("resetPassword");
  678. - map.put("resetPassword", resetPassword);
  679. -
  680. - }
  681. -
  682. - return map;
  683. - }
  684. + if (!errors.hasErrors()) {
  685. +
  686. + user.setUsername(opts.getUsername());
  687. + user.setUserProperties(properties);
  688. +
  689. + // new name
  690. + PersonName newPersonName = opts.getPersonName();
  691. +
  692. + // existing name
  693. + PersonName existingPersonName = user.getPersonName();
  694. +
  695. + // if two are not equal then make the new one the preferred,
  696. + // make the old one voided
  697. + if (!existingPersonName.equalsContent(newPersonName)) {
  698. + existingPersonName.setPreferred(false);
  699. + existingPersonName.setVoided(true);
  700. + existingPersonName.setVoidedBy(user);
  701. + existingPersonName.setDateVoided(new Date());
  702. + existingPersonName.setVoidReason("Changed name on own options form");
  703. +
  704. + newPersonName.setPreferred(true);
  705. + user.addName(newPersonName);
  706. + }
  707. +
  708. + try {
  709. + Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS);
  710. + Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  711. + us.saveUser(user, null);
  712. + //trigger updating of the javascript file cache
  713. + PseudoStaticContentController.invalidateCachedResources(properties);
  714. + // update login user object so that the new name is visible
  715. + // in the webapp
  716. + Context.refreshAuthenticatedUser();
  717. + }
  718. + finally {
  719. + Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS);
  720. + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_USERS);
  721. + }
  722. +
  723. + httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "options.saved");
  724. + } else {
  725. + return super.processFormSubmission(request, response, opts, errors);
  726. + }
  727. +
  728. + view = getSuccessView();
  729. + }
  730. + return new ModelAndView(new RedirectView(view));
  731. + }
  732. +
  733. + /**
  734. + * This is called prior to displaying a form for the first time. It tells Spring the
  735. + * form/command object to load into the request
  736. + *
  737. + * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  738. + */
  739. + protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  740. +
  741. + OptionsForm opts = new OptionsForm();
  742. +
  743. + if (Context.isAuthenticated()) {
  744. + User user = Context.getAuthenticatedUser();
  745. +
  746. + Map<String, String> props = user.getUserProperties();
  747. + opts.setDefaultLocation(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCATION));
  748. + opts.setDefaultLocale(props.get(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE));
  749. + opts.setProficientLocales(props.get(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES));
  750. + opts.setShowRetiredMessage(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_RETIRED)));
  751. + opts.setVerbose(new Boolean(props.get(OpenmrsConstants.USER_PROPERTY_SHOW_VERBOSE)));
  752. + opts.setUsername(user.getUsername());
  753. + opts.setSecretQuestionNew(user.getSecretQuestion());
  754. + // Get a copy of the current person name and clear the id so that
  755. + // they are separate objects
  756. + PersonName personName = PersonName.newInstance(user.getPersonName());
  757. + personName.setPersonNameId(null);
  758. + opts.setPersonName(personName);
  759. + opts.setNotification(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION));
  760. + opts.setNotificationAddress(props.get(OpenmrsConstants.USER_PROPERTY_NOTIFICATION_ADDRESS));
  761. + }
  762. +
  763. + return opts;
  764. + }
  765. +
  766. + /**
  767. + * Called prior to form display. Allows for data to be put in the request to be used in the view
  768. + *
  769. + * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
  770. + */
  771. + protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
  772. +
  773. + HttpSession httpSession = request.getSession();
  774. +
  775. + Map<String, Object> map = new HashMap<String, Object>();
  776. +
  777. + if (Context.isAuthenticated()) {
  778. +
  779. + LocationService ls = Context.getLocationService();
  780. +
  781. + // set location options
  782. + map.put("locations", ls.getAllLocations());
  783. +
  784. + // set language/locale options
  785. + map.put("languages", Context.getAdministrationService().getPresentationLocales());
  786. +
  787. + String resetPassword = (String) httpSession.getAttribute("resetPassword");
  788. + if (resetPassword == null)
  789. + resetPassword = "";
  790. + else
  791. + httpSession.removeAttribute("resetPassword");
  792. + map.put("resetPassword", resetPassword);
  793. +
  794. + }
  795. +
  796. + return map;
  797. + }
  798. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement