📄 usermanager.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Copyright (C) 2001-2003 Janne Jalkanen (Janne.Jalkanen@iki.fi) This program 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 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.ecyrd.jspwiki.auth;import java.util.*;import java.security.Principal;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Cookie;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.WikiEngine;import com.ecyrd.jspwiki.TextUtil;import com.ecyrd.jspwiki.WikiException;import com.ecyrd.jspwiki.util.ClassUtil;import com.ecyrd.jspwiki.util.HttpUtil;/** * Manages user accounts, logins/logouts, passwords, etc. * * @author Janne Jalkanen * @author Erik Bunn */public class UserManager{ static Logger log = Logger.getLogger( UserManager.class ); /** The name the UserProfile is stored in a Session by. */ public static final String WIKIUSER = "currentUser"; /** If true, logs the IP address of the editor on saving. */ public static final String PROP_STOREIPADDRESS= "jspwiki.storeIPAddress"; public static final String PROP_AUTHENTICATOR = "jspwiki.authenticator"; public static final String PROP_USERDATABASE = "jspwiki.userdatabase"; public static final String PROP_ADMINISTRATOR = "jspwiki.auth.administrator"; /** If true, logs the IP address of the editor */ private boolean m_storeIPAddress = true; private HashMap m_groups = new HashMap(); // FIXME: These should probably be localized. // FIXME: All is used as a catch-all. public static final String GROUP_GUEST = "Guest"; public static final String GROUP_NAMEDGUEST = "NamedGuest"; public static final String GROUP_KNOWNPERSON = "KnownPerson"; private static final String DEFAULT_DATABASE = "com.ecyrd.jspwiki.auth.modules.WikiDatabase"; /** * The default administrator group is called "AdminGroup" */ private static final String DEFAULT_ADMINISTRATOR = "AdminGroup"; private WikiAuthenticator m_authenticator; private UserDatabase m_database; private WikiEngine m_engine; private String m_administrator; private boolean m_useAuth = false; /** * Creates an UserManager instance for the given WikiEngine and * the specified set of properties. All initialization for the * modules is done here. */ public UserManager( WikiEngine engine, Properties props ) throws WikiException { m_engine = engine; m_storeIPAddress = TextUtil.getBooleanProperty( props, PROP_STOREIPADDRESS, m_storeIPAddress ); m_administrator = props.getProperty( PROP_ADMINISTRATOR, DEFAULT_ADMINISTRATOR ); m_useAuth = TextUtil.getBooleanProperty( props, AuthorizationManager.PROP_USEOLDAUTH, false ); if( !m_useAuth ) return; WikiGroup all = new AllGroup(); all.setName( "All" ); m_groups.put( GROUP_GUEST, new AllGroup() ); // m_groups.put( "All", all ); m_groups.put( GROUP_NAMEDGUEST, new NamedGroup() ); m_groups.put( GROUP_KNOWNPERSON, new KnownGroup() ); String authClassName = props.getProperty( PROP_AUTHENTICATOR ); if( authClassName != null ) { try { Class authenticatorClass = ClassUtil.findClass( "com.ecyrd.jspwiki.auth.modules", authClassName ); m_authenticator = (WikiAuthenticator)authenticatorClass.newInstance(); m_authenticator.initialize( props ); log.info("Initialized "+authClassName+" for authentication."); } catch( ClassNotFoundException e ) { log.fatal( "Authenticator "+authClassName+" cannot be found", e ); throw new WikiException("Authenticator cannot be found"); } catch( InstantiationException e ) { log.fatal( "Authenticator "+authClassName+" cannot be created", e ); throw new WikiException("Authenticator cannot be created"); } catch( IllegalAccessException e ) { log.fatal( "You are not allowed to access this authenticator class", e ); throw new WikiException("You are not allowed to access this authenticator class"); } } String dbClassName = props.getProperty( PROP_USERDATABASE, DEFAULT_DATABASE ); try { Class dbClass = ClassUtil.findClass( "com.ecyrd.jspwiki.auth.modules", dbClassName ); m_database = (UserDatabase)dbClass.newInstance(); m_database.initialize( m_engine, props ); } catch( ClassNotFoundException e ) { log.fatal( "UserDatabase "+dbClassName+" cannot be found", e ); throw new WikiException("UserDatabase cannot be found"); } catch( InstantiationException e ) { log.fatal( "UserDatabase "+dbClassName+" cannot be created", e ); throw new WikiException("UserDatabase cannot be created"); } catch( IllegalAccessException e ) { log.fatal( "You are not allowed to access this user database class", e ); throw new WikiException("You are not allowed to access this user database class"); } } /** * Convenience shortcut to UserDatabase.getUserProfile(). */ public UserProfile getUserProfile( String name ) { if( m_database == null ) { // No user database, so return a dummy profile UserProfile wup = new UserProfile(); wup.setName( name ); wup.setLoginName( name ); wup.setLoginStatus( UserProfile.COOKIE ); return wup; } WikiPrincipal up = m_database.getPrincipal( name ); if( !(up instanceof UserProfile) ) { log.info( name + " is not a user!" ); up = null; } return( (UserProfile)up ); } /** * Returns the UserDatabase employed by this UserManager. */ public UserDatabase getUserDatabase() { return( m_database ); } /** * Returns the WikiAuthenticator object employed by this UserManager. */ public WikiAuthenticator getAuthenticator() { return( m_authenticator ); } /** * Returns true, if the user or the group represents a super user, * which should be allowed access to everything. * * @param p Principal to check for administrator access. * @return true, if the principal is an administrator. */ public boolean isAdministrator( WikiPrincipal p ) { // // Direct name matches are returned always. // if( p.getName().equals( m_administrator ) ) { return true; } // // Try to get the super group and check if the user is a part // of it. // WikiGroup superPrincipal = getWikiGroup( m_administrator ); if( superPrincipal == null ) { // log.warn("No supergroup '"+m_administrator+"' exists; you should create one."); return false; } return superPrincipal.isMember( p ); } /** * Returns a WikiGroup instance for a given name. WikiGroups are cached, * so there is basically a singleton across the Wiki for a group. * The reason why this class caches them instead of the WikiGroup * class itself is that it is the business of the User Manager to * handle such issues. * * @param name Name of the group. This is case-sensitive. * @return A WikiGroup instance. */ // FIXME: Someone should really check when groups cease to be used, // and release groups that are not being used. // FIXME: Error handling is still deficient. public WikiGroup getWikiGroup( String name ) { WikiGroup group; synchronized( m_groups ) { group = (WikiGroup) m_groups.get( name ); if( group == null ) { WikiPrincipal p = m_database.getPrincipal( name ); if( !(p instanceof WikiGroup) ) { log.info( name+" is not a group!" ); } else { group = (WikiGroup) p; } } } return group; } /** * Returns a list of all WikiGroups this Principal is a member * of. */ // FIXME: This is not a very good solution; UserProfile
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -