⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useraccountform.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  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.forms;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.MessageResources;

import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.PropertyList;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.core.forms.CoreForm;
import com.sslexplorer.policyframework.PolicyUtil;
import com.sslexplorer.security.Role;
import com.sslexplorer.security.User;
import com.sslexplorer.security.UserAttributeDefinition;
import com.sslexplorer.security.UserAttributeValueItem;
import com.sslexplorer.security.UserDatabase;
import com.sslexplorer.tabs.TabModel;

/**
 * Implementation of a tabbed {@link com.sslexplorer.core.forms.CoreForm} that
 * allows an administrator to edit account details.
 * <p>
 * The amount of editable details will depend on whether the underlying user
 * database supports account creation / editing.
 * <p>
 * Editing of user attributes and the enabled flag will always be available, the
 * rest such as name, fullname, email etc will only be available when an
 * appropriate user database is used.
 * 
 * @author Brett Smith <a href="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.41 $
 */
public class UserAccountForm extends CoreForm implements TabModel {
    static Log log = LogFactory.getLog(UserAccountForm.class);

    // Private instance variables

    private String username;
    private String email;
    private String fullname;
    private boolean setPassword;
    private boolean enabled;
    private Properties attributes;
    private PropertyList roles;
    private List userAttributeValueItems;
    private String selectedTab = "details";
    private List categoryIds;
    private List categoryTitles;

    /**
     * Get the username
     * 
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * Set the username
     * 
     * @param username username
     */
    public void setUsername(String username) {
        this.username = username.trim();
    }

    /**
     * Get the full name
     * 
     * @return full name
     */
    public String getFullname() {
        return fullname;
    }

    /**
     * Set the full name
     * 
     * @param fullname full name
     */
    public void setFullname(String fullname) {
        this.fullname = fullname.trim();
    }

    /**
     * Get whether this account should be enabled or not.
     * 
     * @return enabled
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Set whether this account should be enabled or not.
     * 
     * @param enabled
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * Get the email address
     * 
     * @return email address
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set the email address
     * 
     * @param email email address
     */
    public void setEmail(String email) {
        this.email = email.trim();
    }

    /**
     * Get the user attributes
     * 
     * @return user attributes
     */
    public Properties getAttributes() {
        return attributes;
    }

    /**
     * Set whether the password should be set when this form is commited.
     * 
     * @param setPassword set password when commited
     */
    public void setSetPassword(boolean setPassword) {
        this.setPassword = setPassword;
    }

    /**
     * Get whether the password should be set when this form is commited.
     * 
     * @return set password when commited
     */
    public boolean isSetPassword() {
        return setPassword;
    }

    /**
     * Initialise the form
     * 
     * @param user account to edit
     * @param editing editing
     * @param session session
     * @throws Exception on any error
     */
    public void initialize(User user, boolean editing, HttpSession session) throws Exception {
        username = user == null ? "" : user.getPrincipalName();
        email = user == null ? "" : user.getEmail();
        fullname = user == null ? "" : user.getFullname();
        attributes = user == null ? null : user.getAttributes();
        try {
            enabled = user == null ? true : PolicyUtil.isEnabled(user);
        } catch (Exception e) {
            log.warn("Failed to determine if user is enabled, defaulting to disabled.");
            enabled = false;
        }
        setActionTarget("commit");
        setPassword = false;
        this.editing = editing;
        roles = new PropertyList();
        Role[] allRoles = user == null ? new Role[0] : user.getRoles();
        for (int i = 0; i < allRoles.length; i++) {
            roles.add(allRoles[i].getPrincipalName());
        }

        /*
         * Get all of the user attribute definitions and wrap them in 
         * item objects
         */
        
        userAttributeValueItems = new ArrayList();
        List l = CoreServlet.getServlet().getUserDatabase().getUserAttributeDefinitions();
        for (Iterator i = l.iterator(); i.hasNext();) {
            UserAttributeDefinition def = (UserAttributeDefinition) i.next();
            if (!def.isHidden()){
                if(def.getVisibility() != UserAttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
                    MessageResources mr = null;
                    if(def.getMessageResourcesKey() != null) {
                        mr = CoreUtil.getMessageResources(session, def.getMessageResourcesKey()); 
                    }
                    String value = def.getDefaultValue();
                    if (user != null) {
                        value = user.getAttributes().getProperty(def.getName(), value);
                    }
                    UserAttributeValueItem item = new UserAttributeValueItem(def, mr, value);
                    userAttributeValueItems.add(item);
                }
            }
        }
        
        /*
         * Sort the list of items and build up the list of categories
         */
        
        Collections.sort(userAttributeValueItems);
        categoryIds = new ArrayList();
        categoryTitles = new ArrayList();
        for(Iterator i = userAttributeValueItems.iterator(); i.hasNext(); ) {        
            UserAttributeValueItem item = (UserAttributeValueItem) i.next();
            int idx = categoryIds.indexOf(item.getCategoryId());
            if (idx == -1) {
                categoryIds.add(item.getCategoryId());
                categoryTitles.add(item.getCategoryLabel());
            } 
        }
    }

    /**
     * Get a list of the category ids
     * 
     * @return category ids
     */
    public List getCategoryIds() {
        return categoryIds;
    }

    /**
     * Get the list of user attribute value items
     * 
     * @return user attribute value items
     */
    public List getUserAttributeValueItems() {
        return userAttributeValueItems;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping,
     *      javax.servlet.http.HttpServletRequest)
     */
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
//        super.reset(mapping, request);
//        username = "";
//        email = "";
//        fullname = "";
//        attributes = new Properties();
          enabled = false;
//        if (roles != null) {
//            roles.clear();
//        }
    }

    /**
     * Get whether the username is disabled or not
     * 
     * TODO is this required, can't editing be used?
     * 
     * @return username disabled
     */
    public String getUsernameDisabled() {
        return String.valueOf(getEditing());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
     *      javax.servlet.http.HttpServletRequest)
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        UserDatabase udb = CoreServlet.getServlet().getUserDatabase();
        if (isCommiting()) {
            if (username == null || username.length() == 0) {
                errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.noUsername"));
            }
            if (username.length() > 32) {
                errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.usernameExceeds32Chars"));
            }
            if (udb.supportsAccountCreation()) {
                if (fullname == null || fullname.length() == 0) {
                    errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.noFullName"));
                }
                if (fullname.length() > 32) {
                    errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.fullNameExceeds32Chars"));
                }
            }
            User currentUser;
            try {
                currentUser = ContextHolder.getContext().isSetupMode() ? null : CoreServlet.getServlet().getLogonController()
                                .getUser(request);
                if (currentUser != null && getEditing() && currentUser.getPrincipalName().equals(getUsername())) {
                    // Make sure there is at least one enabled account
                    if (!isEnabled()) {
                        errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.cantDisableYourself"));
                    }
                }
                if (!editing) {
                    try {
                        CoreServlet.getServlet().getUserDatabase().getAccount(username);
                        errors.add(Globals.ERROR_KEY, new ActionMessage("createAccount.error.userAlreadyExists", username));
                    } catch (Exception e) {
                    }
                }
            } catch (Exception e) {
                log.warn("Failed to validate user form.", e);
                e.printStackTrace();
            }
        }
        return errors;
    }

    /**
     * Get the list of roles this account is attached (as strings)
     * 
     * @return roles
     */
    public PropertyList getRolesList() {
        return roles;
    }

    /**
     * Get a list of selected roles as a list in <i>Text Field Text</i> format.
     * 
     * @return list of roles
     */
    public String getSelectedRoles() {
        return roles.getAsTextFieldText();
    }

    /**
     * Set a list of selected roles as a list in <i>Text Field Text</i> format.
     * 
     * @param selectedRoles selected roles
     */
    public void setSelectedRoles(String selectedRoles) {
        roles.setAsTextFieldText(selectedRoles);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.tabs.TabModel#getTabCount()
     */
    public int getTabCount() {
        return 1 + (categoryIds.size());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.tabs.TabModel#getTabName(int)
     */
    public String getTabName(int idx) {
        switch (idx) {
            case 0:
                return "details";
            default:
                return (String) categoryIds.get(idx - 1);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
     */
    public String getSelectedTab() {
        return selectedTab;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
     */
    public void setSelectedTab(String selectedTab) {
        this.selectedTab = selectedTab;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
     */
    public String getTabTitle(int idx) {
        switch (idx) {
            case 0:
                return null;
            default:
                return (String) categoryTitles.get(idx - 1);
        }
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
     */
    public String getTabBundle(int idx) {
        return null;
    }
}

⌨️ 快捷键说明

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