Subversion Repositories bacoAlunos

Rev

Rev 1808 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1808 Rev 1814
1
package openldap.impl;
1
package openldap.impl;
2
 
2
 
3
 
3
 
4
import jomm.utils.DiacriticFilter;
4
import jomm.utils.DiacriticFilter;
5
import netscape.ldap.LDAPConnection;
5
import netscape.ldap.LDAPConnection;
6
import netscape.ldap.LDAPException;
6
import netscape.ldap.LDAPException;
7
import openldap.ILdapManager;
7
import openldap.ILdapManager;
8
import openldap.LdapProperties;
8
import openldap.LdapProperties;
9
import org.apache.log4j.Logger;
9
import org.apache.log4j.Logger;
10
 
10
 
11
import java.sql.*;
11
import java.sql.*;
12
import java.util.HashMap;
12
import java.util.HashMap;
13
import java.util.List;
13
import java.util.List;
14
import java.util.Iterator;
14
import java.util.Iterator;
15
 
15
 
16
/**
16
/**
17
 * Created by IntelliJ IDEA.
17
 * Created by IntelliJ IDEA.
18
 * User: User
18
 * User: User
19
 * Date: 9/Abr/2005
19
 * Date: 9/Abr/2005
20
 * Time: 12:04:08
20
 * Time: 12:04:08
21
 * To change this template use File | Settings | File Templates.
21
 * To change this template use File | Settings | File Templates.
22
 */
22
 */
23
public class LdapManager implements ILdapManager{
23
public class LdapManager implements ILdapManager{
24
 
24
 
25
    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);
25
    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);
26
 
26
 
27
    private static final 1.5.0/docs/api/java/lang/String.html">String host =LdapProperties.getProperty("ldap.host");
27
    private static final 1.5.0/docs/api/java/lang/String.html">String host =LdapProperties.getProperty("ldap.host");
28
    private static final int port =LdapProperties.getIntProperty("ldap.port");
28
    private static final int port =LdapProperties.getIntProperty("ldap.port");
29
    private static final 1.5.0/docs/api/java/lang/String.html">String baseQueries =LdapProperties.getProperty("ldap.base.queries");
29
    private static final 1.5.0/docs/api/java/lang/String.html">String baseQueries =LdapProperties.getProperty("ldap.base.queries");
30
    private static final 1.5.0/docs/api/java/lang/String.html">String login =LdapProperties.getProperty("ldap.queries.login");
30
    private static final 1.5.0/docs/api/java/lang/String.html">String login =LdapProperties.getProperty("ldap.queries.login");
31
    private static final 1.5.0/docs/api/java/lang/String.html">String password =LdapProperties.getProperty("ldap.queries.password");
31
    private static final 1.5.0/docs/api/java/lang/String.html">String password =LdapProperties.getProperty("ldap.queries.password");
32
    private static final 1.5.0/docs/api/java/lang/String.html">String userField =LdapProperties.getProperty("ldap.username.field");
32
    private static final 1.5.0/docs/api/java/lang/String.html">String userField =LdapProperties.getProperty("ldap.username.field");
33
    private static final 1.5.0/docs/api/java/lang/String.html">String dnField =LdapProperties.getProperty("ldap.dn.field");
33
    private static final 1.5.0/docs/api/java/lang/String.html">String dnField =LdapProperties.getProperty("ldap.dn.field");
34
 
34
 
35
 
35
 
36
    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) {
36
    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) {
37
        return authenticate(user,password);
37
        return authenticate(user,password);
38
    }
38
    }
39
 
39
 
40
    public 1.5.0/docs/api/java/util/HashMap.html">HashMap getUserInfo(1.5.0/docs/api/java/lang/String.html">String username) {
40
    public 1.5.0/docs/api/java/util/HashMap.html">HashMap getUserInfo(1.5.0/docs/api/java/lang/String.html">String username) {
41
        1.5.0/docs/api/java/sql/Connection.html">Connection con = null;
41
        1.5.0/docs/api/java/sql/Connection.html">Connection con = null;
42
        1.5.0/docs/api/java/util/HashMap.html">HashMap result = null;
42
        1.5.0/docs/api/java/util/HashMap.html">HashMap result = null;
43
        try {
43
        try {
44
            con = getConnection();
44
            con = getConnection();
45
            5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement();
45
            5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement();
46
            1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username);
46
            1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username);
47
            if(rs.next()) {
47
            if(rs.next()) {
48
                result = new 1.5.0/docs/api/java/util/HashMap.html">HashMap();
48
                result = new 1.5.0/docs/api/java/util/HashMap.html">HashMap();
49
                5+0%2Fdocs%2Fapi+List">List propertyValues = LdapProperties.getListValuesProperties("ldap.map");
49
                5+0%2Fdocs%2Fapi+List">List propertyValues = LdapProperties.getListValuesProperties("ldap.map");
50
                1.5.0/docs/api/java/util/Iterator.html">Iterator iter = propertyValues.iterator();
50
                1.5.0/docs/api/java/util/Iterator.html">Iterator iter = propertyValues.iterator();
51
                while (iter.hasNext()) {
51
                while (iter.hasNext()) {
52
                    1.5.0/docs/api/java/lang/String.html">String propValue =  (1.5.0/docs/api/java/lang/String.html">String) iter.next();
52
                    1.5.0/docs/api/java/lang/String.html">String propValue =  (1.5.0/docs/api/java/lang/String.html">String) iter.next();
53
                    result.put(propValue,rs.getString(propValue));
53
                    result.put(propValue,rs.getString(propValue));
54
                }
54
                }
55
            }
55
            }
56
            stmt.close();
56
            stmt.close();
57
            con.close();
57
            con.close();
58
 
58
 
59
        } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) {
59
        } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) {
60
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
60
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
61
        } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) {
61
        } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) {
62
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
62
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
63
        }
63
        }
64
 
64
 
65
        return result;
65
        return result;
66
    }
66
    }
67
 
67
 
68
    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 {
68
    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 {
69
        1.5.0/docs/api/java/lang/Class.html">Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
69
        1.5.0/docs/api/java/lang/Class.html">Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
70
                1.5.0/docs/api/java/lang/String.html">String ldapConnectString = "jdbc:ldap://" + host + ":" + port + "/" + baseQueries + "?SEARCH_SCOPE:=subTreeScope";
70
                1.5.0/docs/api/java/lang/String.html">String ldapConnectString = "jdbc:ldap://" + host + ":" + port + "/" + baseQueries + "?SEARCH_SCOPE:=subTreeScope";
71
                java.sql.1.5.0/docs/api/java/sql/Connection.html">Connection con;
71
                java.sql.1.5.0/docs/api/java/sql/Connection.html">Connection con;
72
        con = 1.5.0/docs/api/java/sql/DriverManager.html">DriverManager.getConnection(ldapConnectString,login,password);
72
        con = 1.5.0/docs/api/java/sql/DriverManager.html">DriverManager.getConnection(ldapConnectString,login,password);
73
        return con;
73
        return con;
74
    }
74
    }
75
    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 {
75
    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 {
76
 
76
 
77
        1.5.0/docs/api/java/sql/Connection.html">Connection con = getConnection();
77
        1.5.0/docs/api/java/sql/Connection.html">Connection con = getConnection();
78
        1.5.0/docs/api/java/lang/String.html">String DN= null;
78
        1.5.0/docs/api/java/lang/String.html">String DN= null;
79
        5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement();
79
        5+0%2Fdocs%2Fapi+Statement">Statement stmt = con.createStatement();
80
        //" + dnField + "
80
        //" + dnField + "
81
        1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username);
81
        1.5.0/docs/api/java/sql/ResultSet.html">ResultSet rs = stmt.executeQuery("SELECT * FROM " + baseQueries + " where " + userField + "=" + username);
82
        //ResultSet rs = stmt.executeQuery("SELECT " + dnField + " FROM " + baseQueries + " where cn=" + username);
82
        //ResultSet rs = stmt.executeQuery("SELECT " + dnField + " FROM " + baseQueries + " where cn=" + username);
83
        if(rs.next()) {
83
        if(rs.next()) {
-
 
84
 
-
 
85
            //AQUI TIVEMOS DE MUDAR PARA DN EM VEZ DE DISTINGUISHEDNAME PORQUE VINHA EM HEX POR CAUSA DOS ACENTOS
84
            DN = rs.getString(dnField);
86
            DN = rs.getString(dnField);
85
        }
87
        }
86
        stmt.close();
88
        stmt.close();
87
        con.close();
89
        con.close();
88
        return DN;
90
        return DN;
89
    }
91
    }
90
 
92
 
91
    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){
93
    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){
92
        try {
94
        try {
93
            1.5.0/docs/api/java/lang/String.html">String dn = getDN(username);
95
            1.5.0/docs/api/java/lang/String.html">String dn = getDN(username);
94
            if(dn == null)
96
            if(dn == null)
95
                return false;
97
                return false;
96
 
98
 
97
            LDAPConnection ld = new LDAPConnection();
99
            LDAPConnection ld = new LDAPConnection();
98
                ld.connect( host, 389 );
100
                ld.connect( host, 389 );
99
 
101
 
100
                if (! ld.isConnected() ){
102
                if (! ld.isConnected() ){
101
                logger.error("Cant connect to LDAP");
103
                logger.error("Cant connect to LDAP");
102
                    return false;
104
                    return false;
103
            }
105
            }
104
 
106
 
105
            try {
107
            try {
-
 
108
 
-
 
109
                //FOI NECESSARIO REMOVER DIACRITNOS DO DN SENAO FALHAVA
106
                ld.authenticate(DiacriticFilter.clean(dn),password);
110
                ld.authenticate(DiacriticFilter.clean(dn),password);
107
            } catch ( LDAPException e ) {
111
            } catch ( LDAPException e ) {
108
                1.5.0/docs/api/java/lang/System.html">System.out.println(e.toString());
112
                1.5.0/docs/api/java/lang/System.html">System.out.println(e.toString());
109
                ld.disconnect();
113
                ld.disconnect();
110
                return false;
114
                return false;
111
            } //catch
115
            } //catch
112
 
116
 
113
            if (ld.isAuthenticated()) {
117
            if (ld.isAuthenticated()) {
114
                ld.disconnect();
118
                ld.disconnect();
115
                return true;
119
                return true;
116
            } else {
120
            } else {
117
                ld.disconnect();
121
                ld.disconnect();
118
                return false;
122
                return false;
119
            }
123
            }
120
        } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) {
124
        } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) {
121
            logger.error("Logging LDAP",e);
125
            logger.error("Logging LDAP",e);
122
        } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) {
126
        } catch (1.5.0/docs/api/java/sql/SQLException.html">SQLException e) {
123
            logger.error("Logging LDAP",e);
127
            logger.error("Logging LDAP",e);
124
        } catch (LDAPException e) {
128
        } catch (LDAPException e) {
125
            logger.error("Logging LDAP",e);
129
            logger.error("Logging LDAP",e);
126
        }
130
        }
127
        return false;
131
        return false;
128
    }
132
    }
129
 
133
 
130
   
134
   
131
 
135
 
132
}
136
}
133
 
137