/** * Metodo para inicial la sesion del usuario. * * @param request * @param response * @return */ public Object doLogin(Request request, Response response) { String nickname = request.queryParams("nickname").toLowerCase(); String password = request.queryParams("password"); SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); User user = (User) session .createCriteria(User.class) .add(Restrictions.eq("nickname", nickname)) .add(Restrictions.eq("password", password)) .uniqueResult(); UserValidator validator = new UserValidator(request.session()); validator.validateUser(user); if (null != user) { request.session(true); request.session().attribute("user", user); response.redirect("/orders"); } else { response.redirect("/"); } return null; }
/** * Metodo para registrar a un usuario. * * @param request * @param response * @return */ public Object doSignup(Request request, Response response) { String name = request.queryParams("name"); String lastName = request.queryParams("last_name"); String nickname = request.queryParams("nickname").toLowerCase(); String password = request.queryParams("password"); String confirmPassword = request.queryParams("confirm_password"); UserValidator validator = new UserValidator(request.session()); validator.validateUserName(name); validator.validateUserLastName(lastName); validator.validateNickname(nickname); validator.validatePassword(password, confirmPassword); if (!validator.error()) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User(); user.setName(name); user.setLastName(lastName); user.setNickname(nickname); user.setPassword(password); user.setIsAdmin(false); session.save(user); transaction.commit(); session.close(); doLogin(request, response); } else { response.redirect("/"); } return null; }