コード例 #1
0
  @After(on = "register", stages = LifecycleStage.BindingAndValidation)
  public void userViewInterceptor() throws Exception {
    log.info("RegisterInterceptor.intercept()");

    AccountTypeConverter atc = new AccountTypeConverter();

    /** Do Type Conversion */
    try {
      Account account =
          atc.convert(
              getContext().getRequest().getParameter("account"),
              Account.class,
              new ArrayList<ValidationError>());
      setAccount(account);
      log.info("    Set account to " + account.getId());
    } catch (Exception x) {
      x.printStackTrace();
      throw x;
    }

    try {
      ViewTypeConverter vtc = new ViewTypeConverter(getAccount());
      View view =
          vtc.convert(
              getContext().getRequest().getParameter("view"),
              View.class,
              new ArrayList<ValidationError>());
      setView(view);
      log.info("    Set view to " + view.getId());
    } catch (Exception x) {
      log.info("    Exception getting view: " + getContext().getRequest().getParameter("view"));
      throw x;
    }

    setEmail(getContext().getRequest().getParameter("email"));
    setUsername(getContext().getRequest().getParameter("username"));
    setPassword(getContext().getRequest().getParameter("password"));
  }
コード例 #2
0
  public Resolution register() throws AjaxException, RegisterException {
    log.info("RegisterAction.register() - Creating new user / userview");

    //		Object nextPage = getContext().getRequest().getSession().getAttribute("nextPage");

    ConnectionSource conn = null;
    try {
      conn = EntityManager.getConnection();

      /** Check that the view allows creating users */
      Dao<View, String> vDao = DaoManager.createDao(conn, View.class);
      View view = vDao.queryForId(getView().getId().toString());

      if (!view.getCreateUser()) {
        log.info("    View does not allow creating new users");
        throw new AjaxException(this, "userView", "Create User Disabled");
      }

      Dao<UserView, String> uvDao = DaoManager.createDao(conn, UserView.class);
      Dao<User, String> uDao = DaoManager.createDao(conn, User.class);

      /**
       * Try to create a user and userview If this fails there is likely already a user with the
       * same name and account
       */
      try {
        log.info("    Creating new user in system: " + getAccount().getAlias());

        User user = new User();
        user.setAccount(getAccount());
        user.setEmail(getEmail());
        user.setName(getUsername());
        user.setPassword(getPassword());

        uDao.create(user);

        log.info("    Creating new userview in for: " + getView().getAlias());

        UserView userView = new UserView();
        userView.setUser(user);
        userView.setView(getView());

        uvDao.create(userView);

        getContext().setUser(user);
        getContext().setAccessLevel(MyActionBeanContext.ACCESS_LEVEL_PASSWORD);

        /** User exists but nextPage was not specified. Redirect to the manager page */
        if (getContext().getUser() != null && nextPage == null) {
          log.info("    User exists but nextpage is null - redirect to ManagerAction");
          RedirectResolution resolution =
              new RedirectResolution(ManagerAction.class).includeRequestParameters(true);
          return resolution;
        }

        log.info("    Login successful - redirect to " + nextPage);

        RedirectResolution resolution = new RedirectResolution(getNextPage());
        return resolution;

      } catch (SQLException x) {
        log.error("RegisterException", x);

        return new ErrorResolution(
            HttpServletResponse.SC_PRECONDITION_FAILED,
            "{\"result\":\"error\",\"message\":\"" + x.getMessage() + "\"}");
      }
    } catch (Exception x) {
      x.printStackTrace();

      log.error("RegisterException", x);

      getContext().getValidationErrors().add("entity", new SimpleError("create user error"));
      throw new RegisterException(this, getAccount(), getView(), "Exception encountered");
    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
      }
    }
  }