001    package org.esupportail.cas.server.handlers.ldap;
002    
003    import org.dom4j.Element;
004    import org.esupportail.cas.server.util.MisconfiguredHandlerException;
005    
006    /**
007     * This class implements a bind LDAP handler class. It is used by
008     * GenericHandler.
009     *
010     * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
011     */
012    public final class BindLdapHandler extends LdapHandler {
013            
014            /**
015             * the base for LDAP searches.
016             */
017            private String searchBase;
018            
019            /**
020             * the scope for LDAP searches.
021             */
022            private String scope;
023            
024            /**
025             * the DN to bind to the LDAP directory.
026             */
027            private String bindDn;
028            
029            /**
030             * the password to bind to the LDAP directory.
031             */
032            private String bindPassword;
033    
034            /**
035             * A flag set to true if multiple accounts in the LDAP directory are allowed.
036             */
037            private boolean multipleAccountsEnabled;
038            
039            /**
040             * Constructor.
041             *
042             * @param handlerElement the XML element that declares the handler 
043             * in the configuration file 
044             * @param configDebug debugging mode of the global configuration
045             * @throws Exception Exception
046             */
047            public BindLdapHandler(
048                            final Element handlerElement, 
049                            final Boolean configDebug) throws Exception {
050                    super(handlerElement, configDebug);
051                    traceBegin();
052    
053                    searchBase = getConfigSubElementContent("search_base", true/*needed*/);
054                    trace("search_base = " + searchBase);
055    
056                    scope = getConfigSubElementContent("scope", true/*needed*/);
057                    trace("scope = " + scope);
058    
059                    bindDn = getConfigSubElementContent("bind_dn", false/*not needed*/);
060                    if (bindDn.equals("")) {
061                            trace("An anonymous connection to the LDAP directory will be used.");
062                            bindPassword = "";
063                    } else {
064                            bindPassword = getConfigSubElementContent("bind_password", false/*not needed*/);
065                    }
066                    trace("bind_dn = " + bindDn);
067                    trace("bind_password = " + bindPassword);
068    
069                    boolean enableMultipleAccounts = hasConfigSubElement("enable_multiple_accounts");
070                    boolean disableMultipleAccounts = hasConfigSubElement("disable_multiple_accounts");
071                    
072                    if (enableMultipleAccounts && disableMultipleAccounts) {
073                            traceThrow(new MisconfiguredHandlerException(
074                                            "enable_multiple_accounts and disable_multiple_accounts "
075                                            + "tags can not be used simultaneously"));
076                    }
077                    multipleAccountsEnabled = false;
078                    if (enableMultipleAccounts) {
079                            multipleAccountsEnabled = true;
080                    }
081                    trace("enable_multiple_accounts = " + multipleAccountsEnabled);
082                    
083                    // add the LDAP servers
084                    addServers(true/*serverElementNeeded*/, getClass().getPackage().getName() + ".BindLdapServer");
085    
086                    traceEnd();
087            }
088            
089            /**
090             * Retrieve the base for LDAP searches.
091             *
092             * @return a string.
093             */
094            String getSearchBase() {
095                    return searchBase;
096            }
097            
098            /**
099             * Retrieve the scope for LDAP searches.
100             *
101             * @return a string.
102             */
103            String getScope() {
104                    return scope;
105            }
106            
107            /**
108             * Retrieve the DN to bind to the LDAP directory.
109             *
110             * @return a string.
111             */
112            String getBindDn() {
113                    return bindDn;
114            }
115            
116            /**
117             * Retrieve the password to bind to the LDAP directory.
118             *
119             * @return a string.
120             */
121            String getBindPassword() {
122                    return bindPassword;
123            }
124            
125            /**
126             * Tell if multiple accounts in the LDAP directory are allowed.
127             * 
128             * @return a boolean.
129             */
130            protected boolean areMultipleAccountsEnabled() {
131                    return multipleAccountsEnabled;
132            }
133    }