cmsuserdataimportlist.java

来自「找了很久才找到到源代码」· Java 代码 · 共 826 行 · 第 1/3 页

JAVA
826
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsUserDataImportList.java,v $
 * Date   : $Date: 2007-08-13 16:29:46 $
 * Version: $Revision: 1.3 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.workplace.tools.accounts;

import org.opencms.file.CmsUser;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsPasswordEncryptionException;
import org.opencms.security.CmsRole;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.util.CmsXsltUtil;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplaceSettings;
import org.opencms.workplace.list.CmsListColumnAlignEnum;
import org.opencms.workplace.list.CmsListColumnDefinition;
import org.opencms.workplace.list.CmsListDirectAction;
import org.opencms.workplace.list.CmsListItem;
import org.opencms.workplace.list.CmsListItemDetails;
import org.opencms.workplace.list.CmsListMetadata;
import org.opencms.workplace.list.CmsListMultiAction;
import org.opencms.workplace.list.I_CmsListFormatter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

/**
 * Main system user account management view.<p>
 * 
 * @author Raphael Schnuck  
 * 
 * @version $Revision: 1.3 $ 
 * 
 * @since 6.5.6
 */
public class CmsUserDataImportList extends A_CmsUsersList {

    /** Value for the delete action. */
    public static final int ACTION_IMPORT = 121;

    /** Request parameter value for the import action. */
    public static final String IMPORT_ACTION = "import";

    /** list action id constant. */
    public static final String LIST_ACTION_VALIDATION = "av";

    /** list column id constant. */
    public static final String LIST_COLUMN_VALIDATION = "cv";

    /** list column id constant. */
    public static final String LIST_COLUMN_VALIDATION_HIDDEN = "cvh";

    /** list item detail id constant. */
    public static final String LIST_DETAIL_REASON = "dre";

    /** list id constant. */
    public static final String LIST_ID = "lsudi";

    /** list action id constant. */
    public static final String LIST_MACTION_SELECT = "ms";

    /** Stores the value of the request parameter for the group list. */
    private String m_paramGroups;

    /** Stores the value of the request parameter for the import file. */
    private String m_paramImportfile;

    /** Stores the value of the request parameter for the default password. */
    private String m_paramPassword;

    /** Stores the value of the request parameter for the role list. */
    private String m_paramRoles;

    /** Stores the reasons why users may not be imported. */
    private Map m_reasons;

    /** The file to upload. */
    private File m_uploadFile;

    /**
     * Public constructor.<p>
     * 
     * @param jsp an initialized JSP action element
     */
    public CmsUserDataImportList(CmsJspActionElement jsp) {

        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_IMPORTLISTCSV_LIST_NAME_0));
        getList().setSortedColumn(LIST_COLUMN_VALIDATION_HIDDEN);
    }

    /**
     * Public constructor with JSP variables.<p>
     * 
     * @param context the JSP page context
     * @param req the JSP request
     * @param res the JSP response
     */
    public CmsUserDataImportList(PageContext context, HttpServletRequest req, HttpServletResponse res) {

        this(new CmsJspActionElement(context, req, res));
    }

    /**
     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
     */
    public void actionDialog() throws JspException, ServletException, IOException {

        switch (getAction()) {
            case ACTION_IMPORT:
                List users = getUsers();
                Iterator itUsers = users.iterator();
                while (itUsers.hasNext()) {
                    CmsUser user = (CmsUser)itUsers.next();
                    if (((m_reasons == null) || !m_reasons.containsKey(user.getName()))
                        && !isAlreadyAvailable(user.getName())) {

                        String password = user.getPassword();
                        if (password.indexOf("_") == -1) {
                            try {
                                password = OpenCms.getPasswordHandler().digest(password);
                            } catch (CmsPasswordEncryptionException e) {
                                throw new CmsRuntimeException(
                                    Messages.get().container(Messages.ERR_DIGEST_PASSWORD_0),
                                    e);
                            }
                        } else {
                            password = password.substring(password.indexOf("_") + 1);
                        }
                        CmsUser createdUser;
                        try {
                            createdUser = getCms().importUser(
                                new CmsUUID().toString(),
                                getParamOufqn() + user.getName(),
                                password,
                                user.getFirstname(),
                                user.getLastname(),
                                user.getEmail(),
                                user.getFlags(),
                                System.currentTimeMillis(),
                                user.getAdditionalInfo());
                        } catch (CmsException e) {
                            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_IMPORT_USER_0), e);
                        }

                        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamGroups())) {
                            List groups = CmsStringUtil.splitAsList(getParamGroups(), ",");
                            Iterator itGroups = groups.iterator();
                            while (itGroups.hasNext()) {
                                try {
                                    getCms().addUserToGroup(createdUser.getName(), (String)itGroups.next());
                                } catch (CmsException e) {
                                    throw new CmsRuntimeException(Messages.get().container(
                                        Messages.ERR_ADD_USER_TO_GROUP_0), e);
                                }
                            }
                        }

                        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamRoles())) {
                            List roles = CmsStringUtil.splitAsList(getParamRoles(), ",");
                            Iterator itRoles = roles.iterator();
                            while (itRoles.hasNext()) {
                                try {
                                    OpenCms.getRoleManager().addUserToRole(
                                        getCms(),
                                        CmsRole.valueOfGroupName((String)itRoles.next()),
                                        createdUser.getName());
                                } catch (CmsException e) {
                                    throw new CmsRuntimeException(Messages.get().container(
                                        Messages.ERR_ADD_USER_TO_ROLE_0), e);
                                }
                            }
                        }
                    }
                }
                setAction(ACTION_CANCEL);
                actionCloseDialog();
                break;
            default:
                super.actionDialog();
        }
    }

    /**
     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#executeListMultiActions()
     */
    public void executeListMultiActions() throws CmsRuntimeException {

        if (getParamListAction().equals(LIST_MACTION_SELECT)) {
            Iterator itItems = getSelectedItems().iterator();
            while (itItems.hasNext()) {
                CmsListItem listItem = (CmsListItem)itItems.next();
                String userName = listItem.get(LIST_COLUMN_DISPLAY).toString();
                List users = getUsers();
                Iterator itUsers = users.iterator();
                while (itUsers.hasNext()) {
                    CmsUser user = (CmsUser)itUsers.next();
                    try {
                        if (user.getName().equals(userName)
                            && (m_reasons == null || !m_reasons.containsKey(userName))
                            && !isAlreadyAvailable(user.getName())) {

                            String password = user.getPassword();
                            if (password.indexOf("_") == -1) {
                                password = OpenCms.getPasswordHandler().digest(password);
                            } else {
                                password = password.substring(password.indexOf("_") + 1);
                            }

                            CmsUser createdUser = getCms().importUser(
                                new CmsUUID().toString(),
                                getParamOufqn() + user.getName(),
                                password,
                                user.getFirstname(),
                                user.getLastname(),
                                user.getEmail(),
                                user.getFlags(),
                                System.currentTimeMillis(),
                                user.getAdditionalInfo());

                            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamGroups())) {
                                List groups = CmsStringUtil.splitAsList(getParamGroups(), ",");
                                Iterator itGroups = groups.iterator();
                                while (itGroups.hasNext()) {
                                    getCms().addUserToGroup(createdUser.getName(), (String)itGroups.next());
                                }
                            }

                            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamRoles())) {

⌨️ 快捷键说明

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