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

📄 cmsloginnew.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsLoginNew.java,v $
 * Date   : $Date: 2005/06/27 23:22:07 $
 * Version: $Revision: 1.7 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2001  The OpenCms Group
 *
 * 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 OpenCms, please see the
 * OpenCms 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 com.opencms.workplace;

import org.opencms.db.CmsDbEntryNotFoundException;
import org.opencms.db.CmsUserSettings;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsUser;
import org.opencms.i18n.CmsMessages;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsSecurityException;
import org.opencms.workplace.CmsFrameset;
import org.opencms.workplace.CmsWorkplaceView;

import com.opencms.core.I_CmsSession;
import com.opencms.legacy.CmsLegacyException;
import com.opencms.legacy.CmsXmlTemplateLoader;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.template.CmsCacheDirectives;
import com.opencms.template.CmsXmlTemplate;
import com.opencms.template.CmsXmlTemplateFile;

import java.util.Hashtable;
import java.util.Iterator;

/**
 * Template class for displaying the login screen of the OpenCms workplace.<P>
 * Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
 *
 * @author Alexander Kandzior 
 * @version $Revision: 1.7 $ 
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */

public class CmsLoginNew extends CmsXmlTemplate {

    /** Debug flag, set to 9 for maximum verbosity */
    private static final int DEBUG = 0;

    /**
     * Gets the content of the login template and processes the data input.<p>
     * 
     * If the user has authenticated himself to the system, 
     * the login window is closed and the workplace is opened.
     * If the login was incorrect, an error message is displayed and the login
     * dialog is displayed again.
     * 
     * @param cms request initialized CmsObject
     * @param templateFile the login template file
     * @param elementName not used
     * @param parameters parameters of the request and the template
     * @param templateSelector selector of the template tag to be displayed
     * @return the processed data of the template
     * @throws CmsException if something goes wrong
     */
    public byte[] getContent(CmsObject cms, String templateFile, 
        String elementName, Hashtable parameters, String templateSelector) 
    throws CmsException {
                                              
        if (DEBUG > 1) System.err.println("\nCmsLoginNew: Login process started");
        
        // Initialize language and encoding
        CmsXmlLanguageFile langFile = new CmsXmlLanguageFile(cms);
        m_messages = langFile.getMessages();
        // Ensure encoding for the login page is set correctly accoring to selected language
        cms.getRequestContext().setEncoding(langFile.getEncoding());
        
        // Check if a "logout=true" parameter is present, if so trash the session
        boolean logout = (null != (String)parameters.get("logout"));
        
        I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), false);
        // Check if there already is a session
        if (session != null) {
            // Old session found, must be invalidated
            if (logout) {
                session.invalidate();
                if (DEBUG > 2) System.err.println("CmsLoginNew: logout, trashed old session");
            } else {
                if (DEBUG > 2) System.err.println("CmsLoginNew: kept old session, no logout parameter found");
            }
        } else {
            if (DEBUG > 2) System.err.println("CmsLoginNew: no current active session");
        }

        // the template to be displayed
        CmsXmlTemplateFile xmlTemplateDocument = new CmsXmlTemplateFile(cms, templateFile);
                
        String username = null;
        CmsUser user;

        // get user name and password
        String name = (String)parameters.get("OPENCMSUSERNAME");
        String password = (String)parameters.get("OPENCMSPASSWORD");
        // get further startup parameters
        String startTaskId = (String)parameters.get(CmsWorkplaceDefault.C_PARA_STARTTASKID);
        String startProjectId = (String)parameters.get(CmsWorkplaceDefault.C_PARA_STARTPROJECTID);

        if (DEBUG > 1) System.err.println("CmsLoginNew: name=" + name + " password=" + password + " task=" + startTaskId + " project=" + startProjectId);

        if ((name != null) && (password != null)) {
            if (DEBUG > 1) System.err.println("CmsLoginNew: trying to log in");
            // user and password have been submitted, try to log in the user        
            boolean validLogin;
            try {
                username = cms.loginUser(name, password);
                validLogin = true;
                if (DEBUG > 1) System.err.println("CmsLoginNew: cms.loginUser() successfull");
            } catch (CmsException e) {
                // invalid login
                validLogin = false;
                if (DEBUG > 1) System.err.println("CmsLoginNew: cms.loginUser() failed");
            }

            if ((username != null) && (username.equals(OpenCms.getDefaultUsers().getUserGuest()))) {
                // please no Guest user in the workplace
                // use the same behaviour as if the access was unauthorized
                validLogin = false;
                if (DEBUG > 1) System.err.println("CmsLoginNew: user was guest user");
            } else if ((username != null) 
                && (! cms.userInGroup(username, OpenCms.getDefaultUsers().getGroupUsers())) 
                && (! cms.userInGroup(username, OpenCms.getDefaultUsers().getGroupProjectmanagers())) 
                && (! cms.userInGroup(username, OpenCms.getDefaultUsers().getGroupAdministrators()))) {
                // user MUST be in at last one of the default groups for administrators, users or project managers
                // use the same behaviour as if the access was unauthorized
                validLogin = false;
                if (DEBUG > 1) System.err.println("CmsLoginNew: user was not in default groups");
            }
            
            if (! validLogin) {
                if (CmsLog.getLog(this).isInfoEnabled()) {
                    CmsLog.getLog(this).info("Failed login attempt for user '" + name + "'");
                }                
                throw new CmsLegacyException("[OpenCms login failed]", CmsLegacyException.C_NO_USER);
            }
            if (DEBUG > 0) System.err.println("CmsLoginNew: user " + username + " logged in");

            // get a session for this user so that he is authentificated at the
            // end of this request
            session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
            if (CmsLog.getLog(this).isInfoEnabled()) {
                CmsLog.getLog(this).info("Login of user '" + username + "'");
            }

            // read the user data from the databsse
            user = cms.readUser(username);
            
            // set the startup project id
            setStartProjectId(cms, session, startProjectId);

            // set startup task view
            setStartTaskId(session, startTaskId);            
            
            // set the additional user preferences
            Hashtable preferences = (Hashtable)user.getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_PREFERENCES);
            // check if preferences are existing, otherwise use defaults
            if (preferences == null) {
                preferences = getDefaultPreferences();
            }
            // check of the users language setting (if he has one)
            session.removeValue(com.opencms.core.I_CmsConstants.C_START_LOCALE);
            
            preferences.put(com.opencms.core.I_CmsConstants.C_START_LOCALE, cms.getRequestContext().getLocale().toString());
            session.putValue(CmsUserSettings.ADDITIONAL_INFO_PREFERENCES, preferences);    

            langFile = new CmsXmlLanguageFile(cms, cms.getRequestContext().getLocale().getLanguage());
            if (DEBUG > 1) System.err.println("CmsLoginNew: encoding: " + langFile.getEncoding());           
            cms.getRequestContext().setEncoding(langFile.getEncoding());        
            
            // trigger call of "login()" JavaScript in Template on page load
            xmlTemplateDocument.setData("onload", "onload='login();'");
        } else if ((! logout) && ((cms.getRequestContext().currentUser()) != null) 
            && (! OpenCms.getDefaultUsers().getUserGuest().equals(cms.getRequestContext().currentUser().getName())) 
            && ((cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupUsers())) 
                || (cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupProjectmanagers())) 
                || (cms.userInGroup(cms.getRequestContext().currentUser().getName(), OpenCms.getDefaultUsers().getGroupAdministrators())))) {
            // the user is already logged in and no logout parameter is present, open a new window
            if (DEBUG > 1) System.err.println("CmsLoginNew: re-using old login");            
            xmlTemplateDocument.setData("onload", "onload='login();'");        
        } else {

⌨️ 快捷键说明

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