/src/com/part2/action/system/LoginAndOutAction.java

http://part2web.googlecode.com/ · Java · 177 lines · 134 code · 18 blank · 25 comment · 14 complexity · fd5f6d676226f9adcde85116284277b6 MD5 · raw file

  1. package com.part2.action.system;
  2. import java.io.IOException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.ajaxanywhere.AAUtils;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.apache.struts.action.ActionMessage;
  13. import org.apache.struts.action.ActionMessages;
  14. import com.part2.Constants;
  15. import com.part2.action.common.CommonAction;
  16. import com.part2.action.common.Password;
  17. import com.part2.action.common.form.CustomActionForm;
  18. import com.part2.action.common.form.ValueObject;
  19. import com.part2.action.common.validator.Validator;
  20. import com.part2.beans.Users;
  21. import com.part2.business.function.UsersService;
  22. public class LoginAndOutAction extends CommonAction<Users> {
  23. private final Log logger = LogFactory.getLog(getClass());
  24. private UsersService usersService;
  25. public void setUserService(UsersService usersService) {
  26. this.usersService = usersService;
  27. }
  28. // ??
  29. public ActionForward login(ActionMapping mapping, ActionForm form,
  30. HttpServletRequest request, HttpServletResponse response)
  31. throws Exception {
  32. logger.info("ready for login ....");
  33. ActionMessages errors = new ActionMessages();
  34. ValueObject userVo = ((CustomActionForm) form).getVoa()
  35. .getValue("User");
  36. String userId = userVo.getField("name");
  37. String password = userVo.getField("password");
  38. if (this.validatorUser(userId, password, errors, request, response)) {
  39. HttpSession session = request.getSession();
  40. this.loginServlet(userId, session);
  41. logger.info("login success !");
  42. int userType = usersService.findUsersByPrimeKey(userId)
  43. .getPurview();
  44. String returnString = "loginUser";
  45. returnString = this.getReturnPageString(userType, returnString);
  46. if (returnString.equals("loginAdminUser")) {
  47. return mapping.findForward("loginAdminUser");
  48. }
  49. } else {
  50. logger.info("login false !");
  51. this.saveErrors(request, errors);
  52. return mapping.findForward("error");
  53. }
  54. return mapping.findForward("error");
  55. }
  56. // ????
  57. // public ActionForward reLogin(ActionMapping mapping, ActionForm form,
  58. // HttpServletRequest request, HttpServletResponse response)
  59. // throws Exception {
  60. //
  61. // logger.info("ready for reLogin ....");
  62. //
  63. // ActionMessages errors = new ActionMessages();
  64. //
  65. // HttpSession session = request.getSession();
  66. //
  67. // Users user = (Users) session.getAttribute(Constants.USER_IN_SESSION);
  68. //
  69. // // ?????? session
  70. // if (session.isNew() || user == null) {
  71. // errors.add("you-did-not-login", new ActionMessage(
  72. // "you-did-not-login"));
  73. // this.saveErrors(request, errors);
  74. // return new ActionForward("/index.do");
  75. // }
  76. //
  77. // String returnString = "loginUser";
  78. // return mapping.findForward(returnString);
  79. // }
  80. public ActionForward logout(ActionMapping mapping, ActionForm form,
  81. HttpServletRequest request, HttpServletResponse response)
  82. throws Exception {
  83. logger.info("run logout ....");
  84. HttpSession session = request.getSession();
  85. logoutServlet(session);
  86. return mapping.findForward("index");
  87. }
  88. private boolean validatorUser(String userId, String password,
  89. ActionMessages errors, HttpServletRequest request,
  90. HttpServletResponse response) throws IOException {
  91. if (Validator.isNull(userId)) {
  92. errors.add("login-id-not-null", new ActionMessage(
  93. "login-id-not-null"));
  94. logger.info("id is null");
  95. return false;
  96. }
  97. if (Validator.isNull(password)) {
  98. errors.add("login-password-not-null", new ActionMessage(
  99. "login-password-not-null"));
  100. logger.info("password is null");
  101. return false;
  102. }
  103. Users user = null;
  104. try {
  105. user = usersService.findUsersByPrimeKey(userId);
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. user = null;
  109. }
  110. if (user == null) { // recycled? or valid?
  111. errors.add("login-user-not-exist", new ActionMessage(
  112. "login-user-not-exist"));
  113. logger.info("user existed? false");
  114. return false;
  115. } else if (!Password.authenticatePassword(user.getPassword(), password)) {
  116. errors.add("login-password-error", new ActionMessage(
  117. "login-password-error"));
  118. logger.info("password?false");
  119. return false;
  120. } else {
  121. if (user.getPurview() == (short) Constants.USER_TYPE_ADMIN) {
  122. request.getSession().setAttribute("adminUserId",
  123. user.getUserId());
  124. request.getSession().setAttribute("adminUserName",
  125. user.getUserName());
  126. } else {
  127. request.getSession().setAttribute("userId", user.getUserId());
  128. request.getSession().setAttribute("userName",
  129. user.getUserName());
  130. request.getSession()
  131. .setAttribute("userType", user.getPurview());
  132. }
  133. }
  134. return true;
  135. }
  136. private String getReturnPageString(int userType, String returnString) {
  137. switch (userType) {
  138. case Constants.USER_TYPE_ADMIN:
  139. logger.info("login admin page...");
  140. returnString = "loginAdminUser";
  141. break;
  142. default:
  143. returnString = "loginUser";
  144. }
  145. return returnString;
  146. }
  147. private void loginServlet(String userId, HttpSession session)
  148. throws Exception {
  149. Users user = usersService.findUsersByPrimeKey(userId);
  150. session.setAttribute(USER_IN_SESSION, user);
  151. }
  152. private void logoutServlet(HttpSession session) {
  153. try {
  154. session.invalidate();
  155. } catch (Exception ee) {
  156. ee.printStackTrace();
  157. }
  158. }
  159. }