/** The login process starts from here. */
  public HttpResponse doCommenceLogin(
      StaplerRequest req,
      StaplerResponse rsp,
      @QueryParameter String from,
      @QueryParameter String ticket)
      throws ServletException, IOException {

    // TODO write login method
    String puid = authenticateInWwpass(ticket, certFile, keyFile);

    WwpassIdentity u;
    try {
      u = loadUserByUsername(puid);
    } catch (UsernameNotFoundException e) {
      if (allowsSignup()) {
        req.setAttribute("errorMessage", Messages.WwpassSecurityRealm_NoSuchUserAllowsSignup());
      } else {
        req.setAttribute("errorMessage", Messages.WwpassSecurityRealm_NoSuchUserDisableSignup());
      }
      req.getView(this, "login.jelly").forward(req, rsp);
      throw e;
    }
    if (!u.isAccountNonLocked() || !u.isEnabled()) {
      // throw new LockedException("Account is not activated for " + puid);
      throw new Failure(Messages.WwpassSecurityRealm_AccountNotActivated());
    }

    Authentication a = new WwpassAuthenticationToken(u.getNickname());
    a = this.getSecurityComponents().manager.authenticate(a);
    SecurityContextHolder.getContext().setAuthentication(a);

    return new HttpRedirect(Jenkins.getInstance().getRootUrl());
  }
  /**
   * Saves the form to the configuration and disk.
   *
   * @param req StaplerRequest
   * @param rsp StaplerResponse
   * @throws ServletException if something unfortunate happens.
   * @throws IOException if something unfortunate happens.
   * @throws InterruptedException if something unfortunate happens.
   */
  public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp)
      throws ServletException, IOException, InterruptedException {
    getProject().checkPermission(AbstractProject.BUILD);
    if (isRebuildAvailable()) {
      if (!req.getMethod().equals("POST")) {
        // show the parameter entry form.
        req.getView(this, "index.jelly").forward(req, rsp);
        return;
      }
      build = req.findAncestorObject(AbstractBuild.class);
      ParametersDefinitionProperty paramDefProp =
          build.getProject().getProperty(ParametersDefinitionProperty.class);
      List<ParameterValue> values = new ArrayList<ParameterValue>();
      ParametersAction paramAction = build.getAction(ParametersAction.class);
      JSONObject formData = req.getSubmittedForm();
      if (!formData.isEmpty()) {
        JSONArray a = JSONArray.fromObject(formData.get("parameter"));
        for (Object o : a) {
          JSONObject jo = (JSONObject) o;
          String name = jo.getString("name");
          ParameterValue parameterValue =
              getParameterValue(paramDefProp, name, paramAction, req, jo);
          if (parameterValue != null) {
            values.add(parameterValue);
          }
        }
      }

      CauseAction cause = new CauseAction(new RebuildCause(build));
      Hudson.getInstance()
          .getQueue()
          .schedule(build.getProject(), 0, new ParametersAction(values), cause);
      rsp.sendRedirect("../../");
    }
  }
 /**
  * Method for checking whether current build is sub job(MatrixRun) of Matrix build.
  *
  * @return boolean
  */
 public boolean isMatrixRun() {
   StaplerRequest request = Stapler.getCurrentRequest();
   if (request != null) {
     build = request.findAncestorObject(AbstractBuild.class);
     if (build != null && build instanceof MatrixRun) {
       return true;
     }
   }
   return false;
 }
  /**
   * @return <code>null</code> if failed. The browser is already redirected to retry by the time
   *     this method returns. a valid {@link User} object if the user creation was successful.
   */
  private User createAccount(StaplerRequest req, StaplerResponse rsp, String formView)
      throws ServletException, IOException {

    SignupInfo si = new SignupInfo(req);

    String puid = authenticateInWwpass(si.ticket, certFile, keyFile);

    try {
      if (loadUserByUsername(puid) != null) {
        si.errorMessages.add(Messages.WwpassSecurityRealm_PuidIsAlreadyTaken());
      }
    } catch (UsernameNotFoundException e) {

    }

    if (si.nickname == null || si.nickname.length() == 0)
      si.errorMessages.add(Messages.WwpassSecurityRealm_NicknameIsRequired());
    else {
      User user = User.get(si.nickname, false);
      if (null != user)
        if (user.getProperty(WwpassIdentity.class) != null)
          si.errorMessages.add(Messages.WwpassSecurityRealm_NicknameIsAlreadyTaken());
    }

    if (si.fullname == null || si.fullname.length() == 0)
      si.errorMessages.add(Messages.WwpassSecurityRealm_FullnameIsRequired());
    else {
      User user = User.get(si.fullname, false);
      if (null != user)
        if (user.getProperty(WwpassIdentity.class) != null)
          si.errorMessages.add(Messages.WwpassSecurityRealm_FullnameIsAlreadyTaken());
    }

    if (si.email == null || !si.email.contains("@"))
      si.errorMessages.add(Messages.WwpassSecurityRealm_InvalidEmailAddress());

    if (!si.errorMessages.isEmpty()) {
      // failed. ask the user to try again.
      req.setAttribute("data", si);
      req.getView(this, formView).forward(req, rsp);
      return null;
    }

    // register the user
    WwpassIdentity id = new WwpassIdentity(puid);
    id.populate(si);

    User user = createAccount(id);
    id.updateProfile(user);

    user.save();
    return user;
  }
 /**
  * Method will return current project.
  *
  * @return currentProject.
  */
 public AbstractProject getProject() {
   if (build != null) {
     return build.getProject();
   }
   AbstractProject currentProject = null;
   StaplerRequest request = Stapler.getCurrentRequest();
   if (request != null) {
     currentProject = request.findAncestorObject(AbstractProject.class);
   }
   if (currentProject == null) {
     throw new NullPointerException("Current Project is null");
   }
   return currentProject;
 }
  /** Lets the current user silently login as the given user and report back accordingly. */
  @SuppressWarnings("ACL.impersonate")
  private void loginAndTakeBack(StaplerRequest req, StaplerResponse rsp, User u)
      throws ServletException, IOException {
    // ... and let him login
    Authentication a = new WwpassAuthenticationToken(u.getId());
    a = this.getSecurityComponents().manager.authenticate(a);
    SecurityContextHolder.getContext().setAuthentication(a);

    // then back to top
    req.getView(this, "success.jelly").forward(req, rsp);
  }
 /**
  * Handles the rebuild request and redirects to parameterized and non parameterized build when
  * needed.
  *
  * @param request StaplerRequest the request.
  * @param response StaplerResponse the response handler.
  * @throws IOException in case of Stapler issues
  * @throws ServletException if something unfortunate happens.
  * @throws InterruptedException if something unfortunate happens.
  */
 public void doIndex(StaplerRequest request, StaplerResponse response)
     throws IOException, ServletException, InterruptedException {
   AbstractBuild currentBuild = request.findAncestorObject(AbstractBuild.class);
   if (currentBuild != null) {
     ParametersAction paramAction = currentBuild.getAction(ParametersAction.class);
     if (paramAction != null) {
       response.sendRedirect(PARAMETERIZED_URL);
     } else {
       nonParameterizedRebuild(currentBuild, response);
     }
   }
 }
 public SignupInfo(StaplerRequest req) {
   req.bindParameters(this);
 }