/**
   * 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;
  }
Exemplo n.º 2
0
  public static LayoutContent displayHome(spark.Request request, Configuration cfg) {
    String html = "";
    Map<String, String> root = Tools.listLayoutMap();
    root.replace("msg", DBH.getMsg(request.session().id()));
    Template temp;
    try {
      temp = cfg.getTemplate("user-home.htm");
      Writer out = new StringWriter();
      temp.process(root, out);
      html = out.toString();
    } catch (IOException | TemplateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      html = e.getMessage();
    }
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("addBtn", Tools.getAddBtn("/tramites/agregar", "Iniciar Trámite"));
    options.put(
        "toolBar",
        Tools.getToolbarItem("/tramites/agregar", "Iniciar Trámite", "", "btn red darken-3")
            + Tools.getToolbarItem("/tramites", "Trámites", "", "btn green darken-3"));

    LayoutContent lc = new LayoutContent(html, options);
    return lc;
  }
  public ModelAndView handle(Request request, Response response) throws Exception {
    // On regarde si l'utilisateur a accès
    ModelAndView modelAndView = Authentification.checkEnseignant(request, response);
    if (modelAndView != null) return modelAndView;

    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("title", "Inscription au créneau");
    attributes.put("connected", (request.session().attribute("email") != null));
    attributes.put("labo", (request.session().attribute("labo") != null));
    attributes.put("enseignant", (request.session().attribute("enseignant") != null));

    Integer idEnseignant = request.session().attribute("enseignant");

    if (idEnseignant == null) response.redirect("/enseignant");

    return new ModelAndView(attributes, "inscription-creneau.ftl");
  }
  /**
   * 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;
  }
Exemplo n.º 5
0
 private CallingContext getContext(Request req) {
   return ctxs.get(req.session().id());
 }
 /**
  * Metodo para cerrar la sesion del usuarios.
  *
  * @param request
  * @param response
  * @return
  */
 public Object doLogout(Request request, Response response) {
   request.session().removeAttribute("user");
   response.redirect("/");
   return null;
 }
 public boolean hasUserLoggedIn(Request request, Response response) {
   Session session = request.session(true);
   return session.attribute("user") != null;
 }