Advertisement
Guest User

2

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