Subversion Repositories bacoAlunos

Rev

Rev 1310 | Rev 1808 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package openldap.impl;


import jomm.utils.DiacriticFilter;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPException;
import openldap.ILdapManager;
import openldap.LdapProperties;
import org.apache.log4j.Logger;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: User
 * Date: 9/Abr/2005
 * Time: 12:04:08
 * To change this template use File | Settings | File Templates.
 */

public class LdapManager implements ILdapManager{

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(LdapManager.class);

    private static final 1.5.0/docs/api/java/lang/String.html">String host =LdapProperties.getProperty("ldap.host");
    private static final int port =LdapProperties.getIntProperty("ldap.port");
    private static final 1.5.0/docs/api/java/lang/String.html">String baseQueries =LdapProperties.getProperty("ldap.base.queries");
    private static final 1.5.0/docs/api/java/lang/String.html">String login =LdapProperties.getProperty("ldap.queries.login");
    private static final 1.5.0/docs/api/java/lang/String.html">String password =LdapProperties.getProperty("ldap.queries.password");
    private static final 1.5.0/docs/api/java/lang/String.html">String userField =LdapProperties.getProperty("ldap.username.field");
    private static final 1.5.0/docs/api/java/lang/String.html">String dnField =LdapProperties.getProperty("ldap.dn.field");


    public boolean login(1.5.0/docs/api/java/lang/String.html">String user, 1.5.0/docs/api/java/lang/String.html">String password) {
        return authenticate(user,password);
    }

    public 1.5.0/docs/api/java/util/HashMap.html">HashMap getUserInfo(1.5.0/docs/api/java/lang/String.html">String username) {
        1.5.0/docs/api/java/sql/Connection.html">Connection con = null;
        1.5.0/docs/api/java/util/HashMap.html">HashMap result = null;
        try {
            con = getConnection();
            5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement();
            1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username);
            if(rs.next()) {
                result = new 1.5.0/docs/api/java/util/HashMap.html">HashMap();
                5+0%2Fdocs%2Fapi+List">List propertyValues = LdapProperties.getListValuesProperties("ldap.map");
                1.5.0/docs/api/java/util/Iterator.html">Iterator iter = propertyValues.iterator();
                while (iter.hasNext()) {
                    1.5.0/docs/api/java/lang/String.html">String propValue =  (1.5.0/docs/api/java/lang/String.html">String) iter.next();
                    result.put(propValue,rs.getString(propValue));
                }
            }
            stmt.close();
            con.close();

        } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return result;
    }

    public 1.5.0/docs/api/java/sql/Connection.html">Connection getConnection() throws 1.5.0/docs/api/java/sql/SQLException.html">SQLException, 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException {
        1.5.0/docs/api/java/lang/Class.html">Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
                1.5.0/docs/api/java/lang/String.html">String ldapConnectString = "jdbc:ldap://" + host + ":" + port + "/" + baseQueries + "?SEARCH_SCOPE:=subTreeScope";
                java.sql.1.5.0/docs/api/java/sql/Connection.html">Connection con;
        con = 1.5.0/docs/api/java/sql/DriverManager.html">DriverManager.getConnection(ldapConnectString,login,password);
        return con;
    }
    public 1.5.0/docs/api/java/lang/String.html">String getDN(1.5.0/docs/api/java/lang/String.html">String username) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException, 1.5.0/docs/api/java/sql/SQLException.html">SQLException {

        1.5.0/docs/api/java/sql/Connection.html">Connection con = getConnection();
        1.5.0/docs/api/java/lang/String.html">String DN= null;
        5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement();
        //" + dnField + "
        1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username);
        //ResultSet rs = stmt.executeQuery("SELECT " + dnField + " FROM " + baseQueries + " where cn=" + username);
        if(rs.next()) {
            DN = rs.getString("DN");
        }
        stmt.close();
        con.close();
        return DN;
    }

    public boolean authenticate(1.5.0/docs/api/java/lang/String.html">String username,1.5.0/docs/api/java/lang/String.html">String password){
        try {
            1.5.0/docs/api/java/lang/String.html">String dn = getDN(username);
            if(dn == null)
                return false;

            LDAPConnection ld = new LDAPConnection();
                ld.connect( host, 389 );

                if (! ld.isConnected() ){
                logger.error("Cant connect to LDAP");
                    return false;
            }

            try {
                ld.authenticate(DiacriticFilter.clean(dn),password);
            } catch ( LDAPException e ) {
                1.5.0/docs/api/java/lang/System.html">System.out.println(e.toString());
                ld.disconnect();
                return false;
            } //catch

            if (ld.isAuthenticated()) {
                ld.disconnect();
                return true;
            } else {
                ld.disconnect();
                return false;
            }
        } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) {
            logger.error("Logging LDAP",e);
        } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) {
            logger.error("Logging LDAP",e);
        } catch (LDAPException e) {
            logger.error("Logging LDAP",e);
        }
        return false;
    }

   

}