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

📄 usermanager.java

📁 jspwiki source code,jspwiki source code
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*     JSPWiki - a JSP-based WikiWiki clone.    Licensed to the Apache Software Foundation (ASF) under one    or more contributor license agreements.  See the NOTICE file    distributed with this work for additional information    regarding copyright ownership.  The ASF licenses this file    to you under the Apache License, Version 2.0 (the    "License"); you may not use this file except in compliance    with the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0    Unless required by applicable law or agreed to in writing,    software distributed under the License is distributed on an    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY    KIND, either express or implied.  See the License for the    specific language governing permissions and limitations    under the License.   */package com.ecyrd.jspwiki.auth;import java.io.Serializable;import java.security.Permission;import java.security.Principal;import java.text.MessageFormat;import java.util.*;import javax.mail.MessagingException;import javax.mail.internet.AddressException;import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.*;import com.ecyrd.jspwiki.auth.permissions.AllPermission;import com.ecyrd.jspwiki.auth.permissions.WikiPermission;import com.ecyrd.jspwiki.auth.user.AbstractUserDatabase;import com.ecyrd.jspwiki.auth.user.DuplicateUserException;import com.ecyrd.jspwiki.auth.user.UserDatabase;import com.ecyrd.jspwiki.auth.user.UserProfile;import com.ecyrd.jspwiki.event.WikiEventListener;import com.ecyrd.jspwiki.event.WikiEventManager;import com.ecyrd.jspwiki.event.WikiSecurityEvent;import com.ecyrd.jspwiki.filters.PageFilter;import com.ecyrd.jspwiki.filters.SpamFilter;import com.ecyrd.jspwiki.i18n.InternationalizationManager;import com.ecyrd.jspwiki.rpc.RPCCallable;import com.ecyrd.jspwiki.rpc.json.JSONRPCManager;import com.ecyrd.jspwiki.ui.InputValidator;import com.ecyrd.jspwiki.util.ClassUtil;import com.ecyrd.jspwiki.util.MailUtil;import com.ecyrd.jspwiki.workflow.*;/** * Provides a facade for obtaining user information. * @author Andrew Jaquith * @since 2.3 */public final class UserManager{    private static final String USERDATABASE_PACKAGE = "com.ecyrd.jspwiki.auth.user";    private static final String SESSION_MESSAGES = "profile";    private static final String PARAM_EMAIL = "email";    private static final String PARAM_FULLNAME = "fullname";    private static final String PARAM_PASSWORD = "password";    private static final String PARAM_LOGINNAME = "loginname";    private static final String UNKNOWN_CLASS = "<unknown>";    private WikiEngine m_engine;    private static Logger log = Logger.getLogger(UserManager.class);    /** Message key for the "save profile" message. */    public  static final String SAVE_APPROVER               = "workflow.createUserProfile";    private static final String PROP_DATABASE               = "jspwiki.userdatabase";    protected static final String SAVE_TASK_MESSAGE_KEY     = "task.createUserProfile";    protected static final String SAVED_PROFILE             = "userProfile";    protected static final String SAVE_DECISION_MESSAGE_KEY = "decision.createUserProfile";    protected static final String FACT_SUBMITTER            = "fact.submitter";    protected static final String PREFS_LOGIN_NAME          = "prefs.loginname";    protected static final String PREFS_FULL_NAME           = "prefs.fullname";    protected static final String PREFS_EMAIL               = "prefs.email";    // private static final String  PROP_ACLMANAGER     = "jspwiki.aclManager";    /** Associates wiki sessions with profiles */    private final Map<WikiSession,UserProfile> m_profiles = new WeakHashMap<WikiSession,UserProfile>();    /** The user database loads, manages and persists user identities */    private UserDatabase     m_database;    private boolean          m_useJAAS      = true;    /**     * Constructs a new UserManager instance.     */    public UserManager()    {    }    /**     * Initializes the engine for its nefarious purposes.     * @param engine the current wiki engine     * @param props the wiki engine initialization properties     */    @SuppressWarnings("deprecation")    public final void initialize( WikiEngine engine, Properties props )    {        m_engine = engine;        m_useJAAS = AuthenticationManager.SECURITY_JAAS.equals( props.getProperty(AuthenticationManager.PROP_SECURITY, AuthenticationManager.SECURITY_JAAS ) );        // Attach the PageManager as a listener        // TODO: it would be better if we did this in PageManager directly        addWikiEventListener( engine.getPageManager() );        JSONRPCManager.registerGlobalObject( "users", new JSONUserModule(this), new AllPermission(null) );    }    /**     * Returns the UserDatabase employed by this WikiEngine. The UserDatabase is     * lazily initialized by this method, if it does not exist yet. If the     * initialization fails, this method will use the inner class     * DummyUserDatabase as a default (which is enough to get JSPWiki running).     * @return the dummy user database     * @since 2.3     */    public final UserDatabase getUserDatabase()    {        // FIXME: Must not throw RuntimeException, but something else.        if( m_database != null )        {            return m_database;        }        if( !m_useJAAS )        {            m_database = new DummyUserDatabase();            return m_database;        }        String dbClassName = UNKNOWN_CLASS;        try        {            dbClassName = WikiEngine.getRequiredProperty( m_engine.getWikiProperties(),                                                          PROP_DATABASE );            log.info("Attempting to load user database class "+dbClassName);            Class<?> dbClass = ClassUtil.findClass( USERDATABASE_PACKAGE, dbClassName );            m_database = (UserDatabase) dbClass.newInstance();            m_database.initialize( m_engine, m_engine.getWikiProperties() );            log.info("UserDatabase initialized.");        }        catch( NoRequiredPropertyException e )        {            log.error( "You have not set the '"+PROP_DATABASE+"'. You need to do this if you want to enable user management by JSPWiki." );        }        catch( ClassNotFoundException e )        {            log.error( "UserDatabase class " + dbClassName + " cannot be found", e );        }        catch( InstantiationException e )        {            log.error( "UserDatabase class " + dbClassName + " cannot be created", e );        }        catch( IllegalAccessException e )        {            log.error( "You are not allowed to access this user database class", e );        }        finally        {            if( m_database == null )            {                log.info("I could not create a database object you specified (or didn't specify), so I am falling back to a default.");                m_database = new DummyUserDatabase();            }        }        return m_database;    }    /**     * <p>Retrieves the {@link com.ecyrd.jspwiki.auth.user.UserProfile}for the     * user in a wiki session. If the user is authenticated, the UserProfile     * returned will be the one stored in the user database; if one does not     * exist, a new one will be initialized and returned. If the user is     * anonymous or asserted, the UserProfile will <i>always</i> be newly     * initialized to prevent spoofing of identities. If a UserProfile needs to     * be initialized, its     * {@link com.ecyrd.jspwiki.auth.user.UserProfile#isNew()} method will     * return <code>true</code>, and its login name will will be set     * automatically if the user is authenticated. Note that this method does     * not modify the retrieved (or newly created) profile otherwise; other     * fields in the user profile may be <code>null</code>.</p>     * <p>If a new UserProfile was created, but its     * {@link com.ecyrd.jspwiki.auth.user.UserProfile#isNew()} method returns     * <code>false</code>, this method throws an {@link IllegalStateException}.     * This is meant as a quality check for UserDatabase providers;     * it should only be thrown if the implementation is faulty.</p>     * @param session the wiki session, which may not be <code>null</code>     * @return the user's profile, which will be newly initialized if the user     * is anonymous or asserted, or if the user cannot be found in the user     * database     */    public final UserProfile getUserProfile( WikiSession session )    {        // Look up cached user profile        UserProfile profile = m_profiles.get( session );        boolean newProfile = profile == null;        Principal user = null;        // If user is authenticated, figure out if this is an existing profile        if ( session.isAuthenticated() )        {            user = session.getUserPrincipal();            try            {                profile = getUserDatabase().find( user.getName() );                newProfile = false;            }            catch( NoSuchPrincipalException e )            {            }        }        if ( newProfile )        {            profile = getUserDatabase().newProfile();            if ( user != null )            {                profile.setLoginName( user.getName() );            }            if ( !profile.isNew() )            {                throw new IllegalStateException(                        "New profile should be marked 'new'. Check your UserProfile implementation." );            }        }        // Stash the profile for next time        m_profiles.put( session, profile );        return profile;    }    /**     * <p>     * Saves the {@link com.ecyrd.jspwiki.auth.user.UserProfile}for the user in     * a wiki session. This method verifies that a user profile to be saved     * doesn't collide with existing profiles; that is, the login name     * or full name is already used by another profile. If the profile     * collides, a <code>DuplicateUserException</code> is thrown. After saving     * the profile, the user database changes are committed, and the user's     * credential set is refreshed; if custom authentication is used, this means     * the user will be automatically be logged in.     * </p>     * <p>     * When the user's profile is saved succcessfully, this method fires a     * {@link WikiSecurityEvent#PROFILE_SAVE} event with the WikiSession as the     * source and the UserProfile as target. For existing profiles, if the     * user's full name changes, this method also fires a "name changed"     * event ({@link WikiSecurityEvent#PROFILE_NAME_CHANGED}) with the     * WikiSession as the source and an array containing the old and new     * UserProfiles, respectively. The <code>NAME_CHANGED</code> event allows     * the GroupManager and PageManager can change group memberships and     * ACLs if needed.     * </p>     * <p>     * Note that WikiSessions normally attach event listeners to the     * UserManager, so changes to the profile will automatically cause the     * correct Principals to be reloaded into the current WikiSession's Subject.     * </p>     * @param session the wiki session, which may not be <code>null</code>     * @param profile the user profile, which may not be <code>null</code>     * @throws DuplicateUserException if the proposed profile's login name or full name collides with another     * @throws WikiException if the save fails for some reason. If the current user does not have     * permission to save the profile, this will be a {@link com.ecyrd.jspwiki.auth.WikiSecurityException};     * if if the user profile must be approved before it can be saved, it will be a     * {@link com.ecyrd.jspwiki.workflow.DecisionRequiredException}. All other WikiException     * indicate a condition that is not normal is probably due to mis-configuration     */    public final void setUserProfile( WikiSession session, UserProfile profile ) throws DuplicateUserException, WikiException    {        // Verify user is allowed to save profile!        Permission p = new WikiPermission( m_engine.getApplicationName(), WikiPermission.EDIT_PROFILE_ACTION );

⌨️ 快捷键说明

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