/*******************************************************************************
 * Copyright (c), 2001, 2002 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Author:  Drake H. Henderson
 * Created:  11-12-01
 *
 ******************************************************************************/

package com.n2bb.security;

/**** COMMON IMPORTS BEGIN ****/
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.n2bb.action.N2bbAction;
import com.n2bb.util.N2bbException;
import com.n2bb.util.ActionConstants;
import org.apache.struts.action.*;

public final class ChangePwdAction extends N2bbAction {

  public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request,
          HttpServletResponse response) throws IOException, ServletException {

    // defaultAction
    String defaultAction = ActionConstants.update;

    /******************** COMMON BEGIN *********************/
    String action = request.getParameter("action");
    if (action == null || action.equals(""))
      action = defaultAction;
    n2bbLog.debug("action... " + action);

    ActionErrors errors = new ActionErrors();
    ActionForward fwd = new ActionForward("home");
    /******************** COMMON END ***********************/

    try {
      ChangePwdForm uf = (ChangePwdForm)form;

      if (action.equals(ActionConstants.save)) {
        if (setPassword(errors, request.getRemoteUser(), uf))
          fwd = mapping.findForward("home");
        else
          fwd = mapping.findForward("failure");
      } else 
        fwd = mapping.findForward("changePwd");
    }
    catch (Exception e) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.other"));
      n2bbLog.error(e.getMessage(), e);
    }

    if (!errors.isEmpty()) {
      saveErrors(request, errors);
    }

    return fwd;
  }

  /************************
   *
   ************************/
  private boolean setPassword(ActionErrors errors, String user, ChangePwdForm form) {
    n2bbLog.debug("enter");

    try {
      SecurityManager.getInstance().setPassword(user, form.getPassword(), true);
      return true;
    } catch (N2bbException e) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.changePwdAction.change"));
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getErrorCode()));
      n2bbLog.error(e.getMessage(), e);
      return false;
    } catch (Exception e) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.changePwdAction.change"));
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.other"));
      n2bbLog.error(e.getMessage(), e);
      return false;
    }
  }


}

