setpasswordaction.java

来自「这是linux下ssl vpn的实现程序」· Java 代码 · 共 98 行

JAVA
98
字号
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.security.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.sslexplorer.core.CoreAttributeConstants;
import com.sslexplorer.core.CoreEvent;
import com.sslexplorer.core.CoreEventConstants;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
import com.sslexplorer.policyframework.Permission;
import com.sslexplorer.policyframework.PolicyConstants;
import com.sslexplorer.security.PublicKeyStore;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.security.User;
import com.sslexplorer.security.UserDatabase;
import com.sslexplorer.security.forms.SetPasswordForm;

public class SetPasswordAction extends AuthenticatedDispatchAction {
    final static Log log = LogFactory.getLog(SetPasswordAction.class);

    public SetPasswordAction() {
        super(PolicyConstants.ACCOUNTS_RESOURCE_TYPE, new Permission[] {
            PolicyConstants.PERM_EDIT
        });
    }

    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                    HttpServletResponse response) throws Exception {
        ((SetPasswordForm) form).initialize((User) request.getSession().getAttribute("setPassword.user"));
        if (((SetPasswordForm) form).getReferer() == null) {
            ((SetPasswordForm) form).setReferer(CoreUtil.getReferer(request));
        }
        request.getSession().removeAttribute("setPassword.user");
        ActionMessages messages = new ActionMessages();
        try {
            messages.add(Globals.MESSAGE_KEY, new ActionMessage("setPassword.message.passwordPolicy", CoreServlet.getServlet().
                getPropertyDatabase().getProperty(0, null, "security.password.pattern.description")));
        } catch (Exception e) {
            log.error("Failed to get password policy text.", e);
        }
        saveMessages(request, messages);
        CoreUtil.addRequiredFieldMessage(this, request);
        return mapping.findForward("display");
    }

    public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        User user = ((SetPasswordForm) form).getUser();
        UserDatabase udb = CoreServlet.getServlet().getUserDatabase();
        if (!udb.supportsPasswordChange()) {
            throw new Exception("Underlying database does not support changing of passwords.");
        }
        try {
            udb.changePassword(user.getPrincipalName(), ((SetPasswordForm) form).getNewPassword(), ((SetPasswordForm) form)
                .getForceChangePasswordAtLogon());
            PublicKeyStore.getInstance().removeCachedKeys(user.getPrincipalName());
            return mapping.findForward("success");
        } catch (Exception e) {
            throw e;
        }
    }

    public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?