1 package com.pow2.user.dao;
2
3
4 import org.apache.log4j.Category;
5 import com.novell.ldap.*;
6
7 import com.pow2.resources.Prefs;
8 import com.pow2.user.User;
9 import com.pow2.util.Util;
10
11
12 /***
13 *
14 *
15 * @author Luca Fossato
16 * @created 22 aprile 2002
17 */
18 public class LDAPUserDAO implements UserDAO
19 {
20 /*** Log4J category */
21 protected Category cat = Category.getInstance(this.getClass());
22
23 /*** an handle to the unique UserDAO instance. */
24 private static LDAPUserDAO instance = null;
25
26 /*** key for ldap hostname value */
27 protected final static String KEY_LDAP_HOST = "ldap.host";
28
29 /*** key for ldap server port value */
30 protected final static String KEY_LDAP_PORT = "ldap.port";
31
32 /*** key for ldap server base dn */
33 protected final static String KEY_LDAP_BASEDN = "ldap.basedn";
34
35 /*** key for ldap server login dn */
36 protected final static String KEY_LDAP_LOGINDN = "ldap.logindn";
37
38 /*** key for ldap server password */
39 protected final static String KEY_LDAP_PASSWD = "ldap.passwd";
40
41 /*** key for the LDAP user id attribute */
42 protected final static String KEY_LDAP_USER_ID = "ldap.user.id";
43
44 /*** key for the LDAP user login attribute */
45 protected final static String KEY_LDAP_USER_LOGIN = "ldap.user.login";
46
47 /*** key for the LDAP user passwd attribute */
48 protected final static String KEY_LDAP_USER_PASSWD = "ldap.user.passwd";
49
50 /*** key for the LDAP user first name attribute */
51 protected final static String KEY_LDAP_USER_FIRSTNAME = "ldap.user.firstName";
52
53 /*** key for the LDAP user last name attribute */
54 protected final static String KEY_LDAP_USER_LASTNAME = "ldap.user.lastName";
55
56 /*** key for the LDAP user email attribute */
57 protected final static String KEY_LDAP_USER_EMAIL = "ldap.user.email";
58
59
60 /***
61 * Default private constructor
62 */
63 private LDAPUserDAO()
64 {
65 }
66
67
68 /***
69 * Get the unique instance of LDAPUserDAO class.
70 * <br>
71 * This method is protected; use UserDAOFactory.getUserDAO() instead.
72 *
73 * @return the instance of LDAPUserDAO class
74 */
75 protected static synchronized LDAPUserDAO instance()
76 {
77 if (instance == null)
78 {
79 instance = new LDAPUserDAO();
80 }
81
82 return instance;
83 }
84
85
86 /***
87 * Get the data provider type
88 *
89 * @return the data provider type
90 */
91 public String getProviderType()
92 {
93 return UserDAOFactory.USER_DATA_PROVIDER_LDAP;
94 }
95
96
97 /***
98 * Update the input User object with the user informations
99 * retrieved from the data provider.
100 * <br>
101 * This method uses the User <code>login</code>
102 * and <code>passwd</code> properties to execute the login procedure.
103 *
104 * @param dao the UserDAO instance
105 * @param login the user login
106 * @param passwd the user password
107 * @return the retrieved User object if the login is succesfull;
108 * null if login fails
109 * @exception Exception if any error occurs
110 */
111 public User getUser(String login, String passwd) throws Exception
112 {
113 return UserDAOUtil.getUser(this, login, passwd);
114 }
115
116
117 /***
118 * Get the registered User identified by the input
119 * <code>login</code> and <code>passwd</code> properties.
120 *
121 * @param login
122 * @param passwd Description of the Parameter
123 * @return the registered User object if the login procedure is successful;
124 * null otherwise
125 * @exception Exception Description of the Exception
126 */
127 public void getUser(User user) throws Exception
128 {
129 if (user == null)
130 {
131 cat.error("::getUser - input user is null. Why ?");
132 return;
133 }
134
135 LDAPConnection con = null;
136 Prefs prefs = Prefs.instance();
137 String baseDN = prefs.getProperty(KEY_LDAP_BASEDN);
138
139
140 String filter = new StringBuffer("(&(cn=")
141 .append(user.getLogin())
142 .append(")(userPassword=")
143 .append(user.getPasswd())
144 .append("))").toString();
145
146 try
147 {
148 con = getConnection();
149
150
151 LDAPSearchResults results =
152 con.search(baseDN, LDAPConnection.SCOPE_SUB, filter, null, false);
153
154
155 if (results.getCount() == 0) return;
156
157
158 if (results.getCount() > 1)
159 cat.warn("::getUser - LDAP search returned more than one User element; get the first one");
160
161
162
163
164
165
166
167
168
169
170
171
172 user.setAuthentication(false);
173
174
175 if (results.hasMoreElements())
176 {
177 LDAPEntry entry = results.next();
178 user.setId (entry.getAttribute(prefs.getProperty(KEY_LDAP_USER_ID)).getStringValue());
179 user.setLogin (entry.getAttribute(prefs.getProperty(KEY_LDAP_USER_LOGIN)).getStringValue());
180 user.setPasswd (entry.getAttribute(prefs.getProperty(KEY_LDAP_USER_PASSWD)).getStringValue());
181 user.setFirstName(entry.getAttribute(prefs.getProperty(KEY_LDAP_USER_FIRSTNAME)).getStringValue());
182 user.setLastName (entry.getAttribute(prefs.getProperty(KEY_LDAP_USER_LASTNAME)).getStringValue());
183 user.setEmail (entry.getAttribute(prefs.getProperty(KEY_LDAP_USER_EMAIL)).getStringValue());
184
185
186 user.setAuthentication(true);
187 }
188 }
189 finally
190 {
191 if (con != null) con.disconnect();
192 }
193
194
195 if (cat.isDebugEnabled())
196 cat.info(UserDAOUtil.getAuthenticationLog(user));
197 }
198
199
200 /***
201 * Persist the user informations into the database.
202 * <br>
203 * This method updates or insert a new User entry into the database
204 * depending on the user id value.
205 *
206 * @param user the user object to persist into the database
207 * @exception Exception if any error occurs
208 */
209 public void setUser(User user) throws Exception
210 {
211 throw new Exception("method not yet implemented...");
212 }
213
214
215 /***
216 * Delete the User having the input id from the database.
217 *
218 * @param id the identifier of the user to remove from the database
219 * @throws Exception if any error occurs
220 */
221 public void deleteUser(String id) throws Exception
222 {
223 throw new Exception("method not yet implemented...");
224 }
225
226
227 /***
228 * Test if the input user <code>login</code> property
229 * already exists into the application database.
230 *
231 * @param user the user object
232 * @return true if the input user login already exists into the application database;
233 * false otherwise
234 * @exception Exception if any error occurs
235 */
236 public boolean loginExists(User user) throws Exception
237 {
238 return loginExists(user.getLogin());
239 }
240
241
242 /***
243 * Test if the input <code>login</code> property
244 * already exists into the application database.
245 *
246 * @param login the login string
247 * @return true if the input login already exists into the application database;
248 * false otherwise
249 * @exception Exception if any error occurs
250 */
251 public boolean loginExists(String login) throws Exception
252 {
253 if (Util.isNull(login))
254 throw new Exception("login is null or empty.");
255
256 LDAPConnection con = null;
257 LDAPSearchResults res = null;
258 Prefs prefs = Prefs.instance();
259 String baseDN = prefs.getProperty(KEY_LDAP_BASEDN);
260
261
262 String filter = new StringBuffer("(cn=")
263 .append(login)
264 .append(")")
265 .toString();
266
267
268 try
269 {
270 con = getConnection();
271 res = con.search(baseDN, LDAPConnection.SCOPE_SUB, filter, null, true);
272 }
273 finally
274 {
275 if (con != null) con.disconnect();
276 }
277
278 return (res.getCount() > 0);
279 }
280
281
282 /***
283 * Refactoring notes:
284 *
285 * move the following methods in a LDAP base class;
286 * this class should extends that base class.
287 *
288 * The following methods are protected because of
289 * JUnit tests.
290 */
291
292
293 /***
294 * Connect and bind with the LDAP server.
295 * <br>
296 * Connection is done using the LDAP properties
297 * retrieved from the Prefs object.
298 */
299 protected LDAPConnection getConnection() throws Exception
300 {
301 LDAPConnection con = new LDAPConnection();
302 Prefs prefs = Prefs.instance();
303
304
305 con.connect(prefs.getProperty(KEY_LDAP_HOST),
306 Integer.parseInt(prefs.getProperty(KEY_LDAP_PORT)));
307
308 con.bind(LDAPConnection.LDAP_V3,
309 prefs.getProperty(KEY_LDAP_LOGINDN),
310 prefs.getProperty(KEY_LDAP_PASSWD));
311
312 return con;
313 }
314
315
316 /***
317 * disconnect with the server
318 */
319 protected void closeConnection(LDAPConnection con) throws Exception
320 {
321 con.disconnect();
322 }
323 }