Exemplo n.º 1
0
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String address = null;

    // obtain parameters
    String login = request.getParameter("login");
    String pass = request.getParameter("password");

    List<String> errors = new ArrayList<>();
    if (login == null || login.isEmpty()) {
      errors.add("Login cannot be empty");
    }
    if (pass == null || pass.isEmpty()) {
      errors.add("Password cannot be empty");
    }

    if (errors.size() != 0) {
      request.setAttribute("errors", errors);
      Utils.forward("ErrorPage", request, response);
      return;
    }

    try {
      DBManager dbManager = DBManager.getInstance();
      User user = dbManager.findUser(login);
      if (user != null) {
        if (user.getPassword().equals(pass)) {
          // all ok
          HttpSession session = request.getSession();
          session.setAttribute("currentUser", user);

          Role role = dbManager.findRoleByUser(user);
          session.setAttribute("currentRole", role);

          // obtain role
          switch (role.getName()) {
            case Role.ROLE_ADMIN:
              address = "/AdminView";
              break;
            case Role.ROLE_CLIENT:
              address = "/ClientView";
              break;
            default:
              errors.add("Unknown role id for user: "******"Password incorrect for " + user.getLogin());
        }
      } else {
        errors.add("No user with such login: "******"Cannot authenticate user: "******"errors", errors);
      Utils.forward("ErrorPage", request, response);
    } else {
      Utils.forward(address, request, response);
    }
  }
Exemplo n.º 2
0
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   request.getSession().invalidate();
   Utils.forward("/IndexView", request, response);
 }