Guest User

Untitled

a guest
May 27th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. package by.botyanov.library.web.command.auth;
  2.  
  3. import by.botyanov.library.domain.User;
  4. import by.botyanov.library.domain.UserRole;
  5. import by.botyanov.library.service.DataValidator;
  6. import by.botyanov.library.service.ServiceException;
  7. import by.botyanov.library.service.UserService;
  8. import by.botyanov.library.web.command.CommandException;
  9. import by.botyanov.library.web.PageConstants;
  10. import by.botyanov.library.web.ParamConstants;
  11. import by.botyanov.library.web.command.Command;
  12. import org.apache.log4j.Logger;
  13.  
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpSession;
  16.  
  17. public class LoginCommand implements Command {
  18. private static final Logger LOG = Logger.getLogger(LoginCommand.class);
  19.  
  20. private LoginCommand() {
  21. }
  22.  
  23. private static class LoginCommandHolder {
  24. private final static LoginCommand instance = new LoginCommand();
  25. }
  26.  
  27. public static LoginCommand getInstance() {
  28. return LoginCommandHolder.instance;
  29. }
  30.  
  31. @Override
  32. public String execute(HttpServletRequest request) throws CommandException {
  33. LOG.debug("Login started");
  34. HttpSession session = request.getSession();
  35. LOG.debug("Role " + session.getAttribute(ParamConstants.USER_ROLE));
  36. if (session.getAttribute(ParamConstants.USER_ROLE) != null) {
  37. if (session.getAttribute(ParamConstants.USER_ROLE).equals(UserRole.ADMIN)) {
  38. return PageConstants.WELCOME_ADMIN_PAGE;
  39. } else {
  40. return PageConstants.WELCOME_USER_PAGE;
  41. }
  42. }
  43. String page;
  44. String login = request.getParameter(ParamConstants.USER_LOGIN);
  45. String password = request.getParameter(ParamConstants.USER_PASSWORD);
  46. User user;
  47. try {
  48. user = UserService.find(login);
  49. } catch (ServiceException e) {
  50. throw new CommandException(e);
  51. }
  52. if (user == null) {
  53. request.setAttribute("errorLogin", true);
  54. return PageConstants.LOGIN_PAGE;
  55. }
  56. LOG.debug("Checking user password");
  57. if (DataValidator.checkPass(password, user.getPassword())) {
  58. session.setAttribute(ParamConstants.USER_ID, user.getId());
  59. session.setAttribute(ParamConstants.USER_LOGIN, user.getLogin());
  60. session.setAttribute(ParamConstants.USER_FIRST_NAME, user.getFirstName());
  61. session.setAttribute(ParamConstants.USER_LAST_NAME, user.getLastName());
  62. session.setAttribute(ParamConstants.USER_ROLE, user.getUserRole().toString().toUpperCase());
  63. if (user.getUserRole().equals(UserRole.ADMIN)) {
  64. page = PageConstants.WELCOME_ADMIN_PAGE;
  65. } else {
  66. page = PageConstants.WELCOME_USER_PAGE;
  67. }
  68. } else {
  69. request.setAttribute("errorPass", true);
  70. page = PageConstants.LOGIN_PAGE;
  71. }
  72. LOG.debug(page);
  73. return page;
  74. }
  75. }
Add Comment
Please, Sign In to add comment