/src/main/java/org/telscenter/sail/webapp/presentation/validators/UserAccountFormValidator.java

https://github.com/WISEngineering-Dev-Group/webapp · Java · 109 lines · 58 code · 17 blank · 34 comment · 11 complexity · 5b10279e7c62793a7f56ec243259c1dc MD5 · raw file

  1. /**
  2. * Copyright (c) 2008 Regents of the University of California (Regents). Created
  3. * by TELS, Graduate School of Education, University of California at Berkeley.
  4. *
  5. * This software is distributed under the GNU Lesser General Public License, v2.
  6. *
  7. * Permission is hereby granted, without written agreement and without license
  8. * or royalty fees, to use, copy, modify, and distribute this software and its
  9. * documentation for any purpose, provided that the above copyright notice and
  10. * the following two paragraphs appear in all copies of this software.
  11. *
  12. * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  13. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14. * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  15. * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  16. * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  17. *
  18. * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  19. * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  20. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  21. * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. package org.telscenter.sail.webapp.presentation.validators;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.springframework.validation.Errors;
  26. import org.springframework.validation.ValidationUtils;
  27. import org.springframework.validation.Validator;
  28. import org.telscenter.sail.webapp.domain.authentication.MutableUserDetails;
  29. import org.telscenter.sail.webapp.presentation.web.UserAccountForm;
  30. /**
  31. * @author Hiroki Terashima
  32. * @author Patrick Lawler
  33. *
  34. * @version $Id$
  35. */
  36. public class UserAccountFormValidator implements Validator {
  37. protected static final int MAX_PASSWORD_LENGTH = 20;
  38. /**
  39. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  40. */
  41. public boolean supports(Class clazz) {
  42. return UserAccountForm.class.isAssignableFrom(clazz);
  43. }
  44. /**
  45. * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
  46. */
  47. public void validate(Object userAccountFormIn, Errors errors) {
  48. UserAccountForm userAccountForm = (UserAccountForm) userAccountFormIn;
  49. MutableUserDetails userDetails = userAccountForm.getUserDetails();
  50. if (userAccountForm.isNewAccount()) {
  51. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.password",
  52. "error.password-not-specified");
  53. if (errors.getFieldErrorCount("userDetails.password") > 0) {
  54. return;
  55. }
  56. if (userDetails.getPassword().length() > MAX_PASSWORD_LENGTH) {
  57. errors.rejectValue("userDetails.password", "error.password-too-long");
  58. return;
  59. }
  60. if (!StringUtils.isAlphanumeric(userDetails.getPassword())) {
  61. errors.rejectValue("userDetails.password", "error.password-illegal-characters");
  62. return;
  63. }
  64. if (userDetails.getSignupdate() == null) {
  65. errors.rejectValue("userDetails.signupdate", "error.signupdate-not-specified");
  66. return;
  67. }
  68. } else {
  69. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.username",
  70. "error.username-not-specified");
  71. if (!StringUtils.isAlphanumeric(userDetails.getUsername())) {
  72. errors.rejectValue("userDetails.username", "error.illegal-characters");
  73. }
  74. }
  75. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.firstname",
  76. "error.firstname-not-specified");
  77. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.lastname",
  78. "error.lastname-not-specified");
  79. if (!StringUtils.isAlphanumeric(userDetails.getFirstname()) ||
  80. !StringUtils.isAsciiPrintable(userDetails.getFirstname())) {
  81. errors.rejectValue("userDetails.firstname", "error.firstname-illegal-characters");
  82. return;
  83. }
  84. if (!StringUtils.isAlphanumeric(userDetails.getLastname()) ||
  85. !StringUtils.isAsciiPrintable(userDetails.getLastname())) {
  86. errors.rejectValue("userDetails.lastname", "error.lastname-illegal-characters");
  87. return;
  88. }
  89. if (errors.hasErrors())
  90. userDetails.setPassword("");
  91. }
  92. }