KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > ldap > UserLDAPHandler


1 /**
2  * Copyright 2001-2004 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.organization.ldap;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.List JavaDoc;
10
11 import net.sf.hibernate.Session;
12 import netscape.ldap.LDAPAttribute;
13 import netscape.ldap.LDAPAttributeSet;
14 import netscape.ldap.LDAPCompareAttrNames;
15 import netscape.ldap.LDAPConnection;
16 import netscape.ldap.LDAPEntry;
17 import netscape.ldap.LDAPModification;
18 import netscape.ldap.LDAPModificationSet;
19 import netscape.ldap.LDAPSearchResults;
20
21 import org.exoplatform.commons.utils.ObjectPageList;
22 import org.exoplatform.commons.utils.PageList;
23 import org.exoplatform.services.database.HibernateService;
24 import org.exoplatform.services.database.XResources;
25 import org.exoplatform.services.ldap.LDAPService;
26 import org.exoplatform.services.ldap.LDAPServiceContainer;
27 import org.exoplatform.services.organization.Query;
28 import org.exoplatform.services.organization.User;
29 import org.exoplatform.services.organization.UserEventListener;
30 import org.exoplatform.services.organization.impl.UserImpl;
31 /**
32  * Created by the eXo platform team
33  * User: Daniel Summer
34  * Date: 28 august 2004
35  *
36  */

37 public class UserLDAPHandler extends BaseLDAPHandler {
38
39     private final String JavaDoc BASEURL = OrganizationLDAPConfig.BASE_URL;
40     private final String JavaDoc PORTALURL = OrganizationLDAPConfig.PORTAL_URL;
41     private final String JavaDoc ROLESURL = OrganizationLDAPConfig.ROLES_URL;
42     private final String JavaDoc USERSURL = OrganizationLDAPConfig.USERS_URL;
43
44     private LDAPServiceContainer serviceContainer_;
45     private HibernateService hService_;
46     private List JavaDoc listeners_;
47
48     public UserLDAPHandler(
49         LDAPServiceContainer serviceContainer,
50         HibernateService hService) {
51         serviceContainer_ = serviceContainer;
52         hService_ = hService;
53         listeners_ = new ArrayList JavaDoc(5);
54     }
55
56     final public List JavaDoc getUserEventListeners() {
57         return listeners_;
58     }
59
60     public void addUserEventListener(UserEventListener listener) {
61         listeners_.add(listener);
62     }
63
64     public void createUser(User user) throws Exception JavaDoc {
65         LDAPService service = null;
66         Session session = null;
67         try {
68             service = serviceContainer_.createLDAPService();
69             session = hService_.openSession();
70             String JavaDoc dn = "uid=" + user.getUserName() + "," + USERSURL;
71             LDAPAttributeSet attrs = new LDAPAttributeSet();
72
73             String JavaDoc objectclass_values[] = { "exouser" };
74
75             setutil("objectclass", objectclass_values, attrs, false);
76             setutil("uid", user.getUserName(), attrs);
77             setutil("userPassword", user.getPassword(), attrs);
78             setutil("firstname", user.getFirstName(), attrs);
79             setutil("lastname", user.getLastName(), attrs);
80             setutil("mail", user.getEmail(), attrs);
81             UserProfileLDAPData upd =
82                 new UserProfileLDAPData(user.getUserName());
83             setutil("profile", upd.getProfile(), attrs);
84             LDAPEntry myEntry = new LDAPEntry(dn, attrs);
85             preSave(user, true, session);
86             service.add(myEntry);
87             postSave(user, true, session);
88             session.flush();
89         } finally {
90             serviceContainer_.closeLDAPService(service);
91             hService_.closeSession(session);
92         }
93     }
94
95     public void saveUser(User user) throws Exception JavaDoc {
96         LDAPService service = null;
97         Session session = null;
98         try {
99             service = serviceContainer_.createLDAPService();
100             session = hService_.openSession();
101             String JavaDoc dn = "uid=" + user.getUserName() + "," + USERSURL;
102             LDAPModificationSet mods = new LDAPModificationSet();
103             mods.add(
104                 LDAPModification.REPLACE,
105                 new LDAPAttribute("userPassword", user.getPassword()));
106             mods.add(
107                 LDAPModification.REPLACE,
108                 new LDAPAttribute("firstname", user.getFirstName()));
109             mods.add(
110                 LDAPModification.REPLACE,
111                 new LDAPAttribute("lastname", user.getLastName()));
112             mods.add(
113                 LDAPModification.REPLACE,
114                 new LDAPAttribute("mail", user.getEmail()));
115
116             preSave(user, false, session);
117             service.modify(dn, mods);
118             postSave(user, false, session);
119             session.flush();
120         } finally {
121             serviceContainer_.closeLDAPService(service);
122             hService_.closeSession(session);
123         }
124     }
125
126     void createUserEntry(User user, Session session) throws Exception JavaDoc {
127         LDAPService service = null;
128
129         try {
130             service = serviceContainer_.createLDAPService();
131
132             String JavaDoc dn = "uid=" + user.getUserName() + "," + USERSURL;
133             LDAPAttributeSet attrs = new LDAPAttributeSet();
134
135             String JavaDoc objectclass_values[] = { "exouser" };
136
137             setutil("objectclass", objectclass_values, attrs, false);
138             setutil("uid", user.getUserName(), attrs);
139             setutil("userPassword", user.getPassword(), attrs);
140             setutil("firstname", user.getFirstName(), attrs);
141             setutil("lastname", user.getLastName(), attrs);
142             setutil("mail", user.getEmail(), attrs);
143             UserProfileLDAPData upd =
144                 new UserProfileLDAPData(user.getUserName());
145             setutil("profile", upd.getProfile(), attrs);
146             LDAPEntry myEntry = new LDAPEntry(dn, attrs);
147
148             service.add(myEntry);
149
150         } finally {
151             serviceContainer_.closeLDAPService(service);
152
153         }
154     }
155
156     public User removeUser(String JavaDoc userName) throws Exception JavaDoc {
157         LDAPService service = null;
158         Session session = null;
159         User foundUser = null;
160         try {
161             service = serviceContainer_.createLDAPService();
162             session = hService_.openSession();
163             String JavaDoc MY_FILTER = "uid=" + userName + ",";
164             String JavaDoc MY_SEARCHBASE = USERSURL;
165
166             String JavaDoc ENTRYDN = MY_FILTER + MY_SEARCHBASE;
167
168             foundUser = findUserByName(userName);
169             preDelete(foundUser, session);
170             MembershipLDAPHandler.removeMembershipEntriesOfUser(userName, session,service);
171             UserProfileLDAPHandler.removeUserProfileEntry(userName, session, service);
172             service.delete(ENTRYDN);
173
174             postDelete(foundUser, session);
175             session.flush();
176         } finally {
177             hService_.closeSession(session);
178             serviceContainer_.closeLDAPService(service);
179         }
180         return foundUser;
181     }
182
183     /*This method use with importer/exporter */
184     void removeUserEntry(User user, Session session) throws Exception JavaDoc {
185         LDAPService service = null;
186
187         service = serviceContainer_.createLDAPService();
188
189         String JavaDoc MY_FILTER = "uid=" + user.getUserName();
190         String JavaDoc MY_SEARCHBASE = USERSURL;
191
192         String JavaDoc ENTRYDN = MY_FILTER + MY_SEARCHBASE;
193
194         service.delete(ENTRYDN);
195         serviceContainer_.closeLDAPService(service);
196
197     }
198
199     public User findUserByName(String JavaDoc userName) throws Exception JavaDoc {
200         User user = null;
201         LDAPService service = null;
202         try {
203             service = serviceContainer_.createLDAPService();
204             user = findUserByName(userName, service);
205         } finally {
206             serviceContainer_.closeLDAPService(service);
207         }
208         return user;
209     }
210
211     public User findUserByName(String JavaDoc userName, LDAPService service)
212         throws Exception JavaDoc {
213         User user = new UserImpl();
214         String JavaDoc MY_FILTER = "uid=" + userName;
215         String JavaDoc MY_SEARCHBASE = USERSURL;
216
217         LDAPSearchResults res =
218             service.search( MY_SEARCHBASE,LDAPConnection.SCOPE_SUB, MY_FILTER, null, false);
219
220         List JavaDoc l = getUtil(res);
221         LDAPEntry findEntry = (LDAPEntry) l.get(0);
222
223         LDAPAttributeSet attrs = findEntry.getAttributeSet();
224         user.setUserName(getutil("uid", attrs));
225         user.setPassword(getutil("userPassword", attrs));
226         user.setFirstName(getutil("firstname", attrs));
227         user.setLastName(getutil("lastname", attrs));
228         user.setEmail(getutil("mail", attrs));
229
230         return user;
231     }
232
233     public PageList getUserPageList(int pageSize) throws Exception JavaDoc {
234         LDAPService service = null;
235         Session session = null;
236         service = serviceContainer_.createLDAPService();
237         session = hService_.openSession();
238
239         String JavaDoc MY_SEARCHBASE = USERSURL;
240
241         LDAPSearchResults res =
242             service.search(
243                 MY_SEARCHBASE,
244                 LDAPConnection.SCOPE_SUB,
245                 null,
246                 null,
247                 false);
248         String JavaDoc[] sortAttrs = { "uid" };
249         boolean[] ascending = { true, true };
250         res.sort(new LDAPCompareAttrNames(sortAttrs, ascending));
251
252         List JavaDoc users = new ArrayList JavaDoc();
253         while (res.hasMoreElements()) {
254             LDAPEntry findEntry = null;
255             findEntry = res.next();
256             LDAPAttributeSet attrs = findEntry.getAttributeSet();
257             User user = new UserImpl();
258             user.setUserName(getutil("uid", attrs));
259             user.setPassword(getutil("userPassword", attrs));
260             user.setFirstName(getutil("firstname", attrs));
261             user.setLastName(getutil("lastname", attrs));
262             user.setEmail(getutil("mail", attrs));
263             users.add(user);
264         }
265         hService_.closeSession(session);
266         serviceContainer_.closeLDAPService(service);
267         //return users;
268
return new ObjectPageList(users, pageSize);
269         //return new DBObjectPageList(service_, UserImpl.class, pageSize);
270
}
271
272     public PageList findUsers(Query q) throws Exception JavaDoc {
273
274         LDAPService service = null;
275         Session session = null;
276         service = serviceContainer_.createLDAPService();
277         session = hService_.openSession();
278         String JavaDoc MY_FILTER = null;
279         if (q.getUserName() != null) {
280             String JavaDoc userName = q.getUserName();
281             if (userName == null || userName.length() == 0) {
282                 userName = "%";
283             }
284             userName = userName.replace('*', '%');
285             MY_FILTER = "uid ~= " + userName;
286         }
287
288         String JavaDoc MY_SEARCHBASE = USERSURL;
289
290         LDAPSearchResults res =
291             service.search(
292                 MY_SEARCHBASE,
293                 LDAPConnection.SCOPE_SUB,
294                 MY_FILTER,
295                 null,
296                 false);
297         String JavaDoc[] sortAttrs = { "uid" };
298         boolean[] ascending = { true, true };
299         res.sort(new LDAPCompareAttrNames(sortAttrs, ascending));
300
301         List JavaDoc users = new ArrayList JavaDoc();
302         while (res.hasMoreElements()) {
303             LDAPEntry findEntry = null;
304             findEntry = res.next();
305             LDAPAttributeSet attrs = findEntry.getAttributeSet();
306             User user = new UserImpl();
307             user.setUserName(getutil("uid", attrs));
308             user.setPassword(getutil("userPassword", attrs));
309             user.setFirstName(getutil("firstname", attrs));
310             user.setLastName(getutil("lastname", attrs));
311             user.setEmail(getutil("mail", attrs));
312             users.add(user);
313         }
314         /*
315                 int first = 0;
316                 int max = 0;
317                 int size = users.size();
318                 int from = 0, to = 0;
319                 if (q.getMaxSize() > 0) {
320                     first = q.getFrom();
321                     max = q.getTo();
322                     from = (first > size) ? 0 : first;
323                     if (max > size) {
324                         to = (size > 0) ? size : 0;
325                     } else {
326                         to = first + max - 1;
327                     }
328         
329                 }
330                 //users.subList(from, to);
331         */

332         hService_.closeSession(session);
333         serviceContainer_.closeLDAPService(service);
334         //return users;
335
return new ObjectPageList(users, 20);
336     }
337
338     public PageList findUsersByGroup(String JavaDoc groupId) throws Exception JavaDoc {
339
340         LDAPService service = null;
341         Session session = null;
342         service = serviceContainer_.createLDAPService();
343         session = hService_.openSession();
344
345         String JavaDoc MY_SEARCHBASE = USERSURL;
346         String JavaDoc MY_FILTER = "membership=" + "*" + groupId;
347         LDAPSearchResults res =
348             service.search(
349                 MY_SEARCHBASE,
350                 LDAPConnection.SCOPE_SUB,
351                 MY_FILTER,
352                 null,
353                 false);
354         String JavaDoc[] sortAttrs = { "uid" };
355         boolean[] ascending = { true, true };
356         res.sort(new LDAPCompareAttrNames(sortAttrs, ascending));
357
358         List JavaDoc users = new ArrayList JavaDoc();
359
360         while (res.hasMoreElements()) {
361             LDAPEntry findEntry = null;
362             findEntry = res.next();
363             LDAPAttributeSet attrs = findEntry.getAttributeSet();
364
365             User user = new UserImpl();
366             user.setUserName(getutil("uid", attrs));
367             user.setPassword(getutil("userPassword", attrs));
368             user.setFirstName(getutil("firstname", attrs));
369             user.setLastName(getutil("lastname", attrs));
370             user.setEmail(getutil("mail", attrs));
371             users.add(user);
372
373         }
374
375         hService_.closeSession(session);
376         serviceContainer_.closeLDAPService(service);
377         //return users;
378
return new ObjectPageList(users, 20);
379     }
380
381     public Collection JavaDoc findUsersByGroupAndRole(String JavaDoc groupName, String JavaDoc role)
382         throws Exception JavaDoc {
383
384         LDAPService service = null;
385         Session session = null;
386         service = serviceContainer_.createLDAPService();
387         session = hService_.openSession();
388
389         String JavaDoc MY_SEARCHBASE = USERSURL;
390         String JavaDoc FILTER =
391             "(&(membership=" + "*" + groupName + ")(exorole=" + role + "))";
392
393         LDAPSearchResults res =
394             service.search(
395                 MY_SEARCHBASE,
396                 LDAPConnection.SCOPE_SUB,
397                 FILTER,
398                 null,
399                 false);
400         String JavaDoc[] sortAttrs = { "uid" };
401         boolean[] ascending = { true, true };
402         res.sort(new LDAPCompareAttrNames(sortAttrs, ascending));
403
404         List JavaDoc users = new ArrayList JavaDoc();
405
406         while (res.hasMoreElements()) {
407             LDAPEntry findEntry = null;
408             findEntry = res.next();
409             LDAPAttributeSet attrs = findEntry.getAttributeSet();
410
411             User user = new UserImpl();
412             user.setUserName(getutil("uid", attrs));
413             user.setPassword(getutil("userPassword", attrs));
414             user.setFirstName(getutil("firstname", attrs));
415             user.setLastName(getutil("lastname", attrs));
416             user.setEmail(getutil("mail", attrs));
417             users.add(user);
418
419         }
420
421         hService_.closeSession(session);
422         serviceContainer_.closeLDAPService(service);
423         return users;
424     }
425
426     private void preSave(User user, boolean isNew, Session session)
427         throws Exception JavaDoc {
428         XResources xresources = new XResources();
429         xresources.addResource(Session.class, session);
430         for (int i = 0; i < listeners_.size(); i++) {
431             UserEventListener listener = (UserEventListener) listeners_.get(i);
432             listener.preSave(user, isNew, xresources);
433         }
434     }
435
436     private void postSave(User user, boolean isNew, Session session)
437         throws Exception JavaDoc {
438         XResources xresources = new XResources();
439         xresources.addResource(Session.class, session);
440         for (int i = 0; i < listeners_.size(); i++) {
441             UserEventListener listener = (UserEventListener) listeners_.get(i);
442             listener.postSave(user, isNew, xresources);
443         }
444     }
445
446     private void preDelete(User user, Session session) throws Exception JavaDoc {
447         XResources xresources = new XResources();
448         xresources.addResource(Session.class, session);
449         for (int i = 0; i < listeners_.size(); i++) {
450             UserEventListener listener = (UserEventListener) listeners_.get(i);
451             listener.preDelete(user, xresources);
452         }
453     }
454
455     private void postDelete(User user, Session session) throws Exception JavaDoc {
456         XResources xresources = new XResources();
457         xresources.addResource(Session.class, session);
458         for (int i = 0; i < listeners_.size(); i++) {
459             UserEventListener listener = (UserEventListener) listeners_.get(i);
460             listener.postDelete(user, xresources);
461         }
462     }
463 }
Popular Tags