1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.selfregistration.business;
35
36 import fr.paris.lutece.plugins.selfregistration.utils.SelfRegistrationUtils;
37
38 import java.util.Hashtable;
39 import java.util.Properties;
40
41 import javax.naming.Context;
42 import javax.naming.NamingEnumeration;
43 import javax.naming.NamingException;
44 import javax.naming.directory.Attribute;
45 import javax.naming.directory.Attributes;
46 import javax.naming.directory.BasicAttribute;
47 import javax.naming.directory.BasicAttributes;
48 import javax.naming.directory.DirContext;
49 import javax.naming.directory.InitialDirContext;
50 import javax.naming.directory.SearchResult;
51
52 import javax.servlet.http.HttpServletRequest;
53
54
55
56
57
58
59
60 public class LdapUserDAO implements ILdapUserDAO
61 {
62
63 private static final String PROPERTY_CIVILITY_ITEMS_NUMBER = "selfregistration.civility.items.numbers";
64 private static final String PROPERTY_LABEL_CIVYLITY = "selfregistration.xpage.registration.civility";
65 private static final String PROPERTY_STREET_SUFFIX_ITEMS_NUMBER = "selfregistration.streetNumberSuffix.items.numbers";
66 private static final String PROPERTY_LABEL_STREET_SUFFIX = "selfregistration.xpage.registration.streetNumberSuffix";
67 private static final String PROPERTY_STREET_TYPE_ITEMS_NUMBER = "selfregistration.streetType.items.numbers";
68 private static final String PROPERTY_LABEL_STREET_TYPE = "selfregistration.xpage.registration.streetType";
69
70
71 private static final String LDAP_CNX_POOL = "com.sun.jndi.ldap.connect.pool";
72 private static final String LDAP_CNX_POOL_MAXSIZE = "com.sun.jndi.ldap.connect.pool.maxsize";
73 private static final String LDAP_CNX_POOL_PREFSIZE = "com.sun.jndi.ldap.connect.pool.prefsize";
74 private static final String LDAP_CNX_POOL_TIMEOUT = "com.sun.jndi.ldap.connect.pool.timeout";
75 private static final String LDAP_ATTRIBUTE_UID = "ldap.attribute.uid";
76 private static final String LDAP_ATTRIBUTE_CN = "ldap.attribute.cn";
77 private static final String LDAP_ATTRIBUTE_OBJECT_CLASS = "ldap.attribute.objectClass";
78 private static final String LDAP_ATTRIBUTE_OBJECT_CLASS_PERSON = "ldap.attribute.objectClass.person";
79 private static final String LDAP_ATTRIBUTE_OBJECT_CLASS_TOP = "ldap.attribute.objectClass.top";
80 private static final String LDAP_ATTRIBUTE_OBJECT_CLASS_ORG_PERSON = "ldap.attribute.objectClass.orgPerson";
81 private static final String LDAP_ATTRIBUTE_OBJECT_CLASS_INET_ORG_PERSON = "ldap.attribute.objectClass.inetOrgPerson";
82 private static final String LDAP_ATTRIBUTE_OBJECT_CLASS_PARIS_PERSON = "ldap.attribute.objectClass.parisPerson";
83 private static final String LDAP_ATTRIBUTE_CIVILITY = "ldap.attribute.civility";
84 private static final String LDAP_ATTRIBUTE_LAST_NAME = "ldap.attribute.lastName";
85 private static final String LDAP_ATTRIBUTE_GIVEN_NAME = "ldap.attribute.givenName";
86 private static final String LDAP_ATTRIBUTE_EMAIL = "ldap.attribute.mail";
87 private static final String LDAP_ATTRIBUTE_PASSWD = "ldap.attribute.userPassword";
88 private static final String LDAP_ATTRIBUTE_PHONE_NUMBER = "ldap.attribute.phoneNumber";
89 private static final String LDAP_ATTRIBUTE_STREET_NUMBER = "ldap.attribute.streetNumber";
90 private static final String LDAP_ATTRIBUTE_STREET_NUMBER_SUFFIX = "ldap.attribute.streetNumberSuffix";
91 private static final String LDAP_ATTRIBUTE_STREET_TYPE = "ldap.attribute.streetType";
92 private static final String LDAP_ATTRIBUTE_STREET = "ldap.attribute.streetName";
93 private static final String LDAP_ATTRIBUTE_DISTRICT_NUMBER = "ldap.attribute.districtNumber";
94 private static final String LDAP_ATTRIBUTE_POSTAL_CODE = "ldap.attribute.postalCode";
95 private static final String LDAP_ATTRIBUTE_STATE_PROV = "ldap.attribute.stateProv";
96 private static final String LDAP_ATTRIBUTE_CITY = "ldap.attribute.city";
97 private static final String LDAP_ATTRIBUTE_COUNTRY = "ldap.attribute.country";
98 private String _strContextFactory;
99 private String _strProviderUrl;
100 private String _strOu;
101 private String _strSecurityAuthentication;
102 private String _strLogin;
103 private String _strPassword;
104 private String _strPoolMaxSize;
105 private String _strPoolPrefSize;
106 private String _strPoolTimeout;
107 private Properties _ldapAttributes;
108
109
110
111
112
113
114 public DirContext openConnexion( ) throws NamingException
115 {
116 Hashtable ldapEnv = new Hashtable( );
117 ldapEnv.put( Context.INITIAL_CONTEXT_FACTORY, getContextFactory( ) );
118 ldapEnv.put( Context.PROVIDER_URL, getProviderUrl( ) );
119 ldapEnv.put( Context.SECURITY_AUTHENTICATION, getSecurityAuthentication( ) );
120 ldapEnv.put( Context.SECURITY_PRINCIPAL, getLogin( ) );
121 ldapEnv.put( Context.SECURITY_CREDENTIALS, getPassword( ) );
122 ldapEnv.put( LDAP_CNX_POOL, Boolean.TRUE.toString( ) );
123 ldapEnv.put( LDAP_CNX_POOL_MAXSIZE, getPoolMaxSize( ) );
124 ldapEnv.put( LDAP_CNX_POOL_PREFSIZE, getPoolPrefSize( ) );
125 ldapEnv.put( LDAP_CNX_POOL_TIMEOUT, getPoolTimeout( ) );
126
127 DirContext ldapContext = new InitialDirContext( ldapEnv );
128
129 return ldapContext;
130 }
131
132
133
134
135
136
137 public void closeConnexion( DirContext ldapContext )
138 throws NamingException
139 {
140 ldapContext.close( );
141 }
142
143
144
145
146
147
148 public void registration( LdapUser ldapUser ) throws NamingException
149 {
150 Attributes attributes = getAttributes( ldapUser, false );
151
152 DirContext ldapContext = openConnexion( );
153 ldapContext.createSubcontext( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_UID ) + "=" +
154 ldapUser.getMail( ) + "," + _strOu, attributes );
155 closeConnexion( ldapContext );
156 }
157
158
159
160
161
162
163
164 public boolean uidExit( String strUid ) throws NamingException
165 {
166 NamingEnumeration objs = null;
167
168 BasicAttributes searchAttrs = new BasicAttributes( );
169 searchAttrs.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_UID ), strUid );
170
171 DirContext ldapContext = openConnexion( );
172 objs = ldapContext.search( _strOu, searchAttrs );
173 closeConnexion( ldapContext );
174
175 return objs.hasMoreElements( );
176 }
177
178
179
180
181
182
183
184
185 public boolean checkOldPassword( String strUid, String strOldPassword )
186 throws NamingException
187 {
188 NamingEnumeration objs = null;
189
190 BasicAttributes searchAttrs = new BasicAttributes( );
191 searchAttrs.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_UID ), strUid );
192 searchAttrs.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_PASSWD ), strOldPassword );
193
194 DirContext ldapContext = openConnexion( );
195 objs = ldapContext.search( _strOu, searchAttrs );
196 closeConnexion( ldapContext );
197
198 return objs.hasMoreElements( );
199 }
200
201
202
203
204
205
206
207
208 public LdapUser getLdapUserByUid( HttpServletRequest request, String strUid )
209 throws NamingException
210 {
211 NamingEnumeration objs = null;
212
213 BasicAttributes searchAttrs = new BasicAttributes( );
214 searchAttrs.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_UID ), strUid );
215
216 DirContext ldapContext = openConnexion( );
217 objs = ldapContext.search( _strOu, searchAttrs );
218 closeConnexion( ldapContext );
219
220 if ( objs.hasMoreElements( ) )
221 {
222 SearchResult match = (SearchResult) objs.nextElement( );
223
224 return createLdapUser( request, match.getAttributes( ) );
225 }
226 else
227 {
228 return null;
229 }
230 }
231
232
233
234
235
236
237 public void modification( LdapUser ldapUser ) throws NamingException
238 {
239 Attributes attributes = getAttributes( ldapUser, true );
240
241 DirContext ldapContext = openConnexion( );
242 ldapContext.modifyAttributes( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_UID ) + "=" +
243 ldapUser.getMail( ) + "," + _strOu, DirContext.REPLACE_ATTRIBUTE, attributes );
244 }
245
246
247
248
249
250
251
252
253 private LdapUser createLdapUser( HttpServletRequest request, Attributes attrs )
254 throws NamingException
255 {
256 LdapUser ldapUser = new LdapUser( );
257 ldapUser.setTitleLabel( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_CIVILITY ) ).get( ) );
258
259 if ( ldapUser.getTitleLabel( ) != null )
260 {
261 String strTitleCode = SelfRegistrationUtils.getCodeOfLabel( request, PROPERTY_CIVILITY_ITEMS_NUMBER,
262 PROPERTY_LABEL_CIVYLITY, ldapUser.getTitleLabel( ) );
263 ldapUser.setTitle( strTitleCode );
264 }
265
266 ldapUser.setSn( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_LAST_NAME ) ).get( ) );
267 ldapUser.setGivenName( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_GIVEN_NAME ) ).get( ) );
268 ldapUser.setMail( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_EMAIL ) ).get( ) );
269
270 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_PHONE_NUMBER ) ) != null )
271 {
272 ldapUser.setTelephoneNumber( (String) attrs.get( getLdapAttributes( )
273 .getProperty( LDAP_ATTRIBUTE_PHONE_NUMBER ) ).get( ) );
274 }
275
276 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STREET_NUMBER ) ) != null )
277 {
278 ldapUser.setStreetNumber( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STREET_NUMBER ) )
279 .get( ) );
280 }
281
282 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STREET_NUMBER_SUFFIX ) ) != null )
283 {
284 ldapUser.setStreetNumberSuffixLabel( (String) attrs.get( getLdapAttributes( )
285 .getProperty( LDAP_ATTRIBUTE_STREET_NUMBER_SUFFIX ) )
286 .get( ) );
287 }
288
289 if ( ldapUser.getStreetNumberSuffixLabel( ) != null )
290 {
291 String strStreetNumberSuffixCode = SelfRegistrationUtils.getCodeOfLabel( request,
292 PROPERTY_STREET_SUFFIX_ITEMS_NUMBER, PROPERTY_LABEL_STREET_SUFFIX,
293 ldapUser.getStreetNumberSuffixLabel( ) );
294 ldapUser.setStreetNumberSuffix( strStreetNumberSuffixCode );
295 }
296
297 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STREET_TYPE ) ) != null )
298 {
299 ldapUser.setStreetTypeLabel( (String) attrs.get( getLdapAttributes( )
300 .getProperty( LDAP_ATTRIBUTE_STREET_TYPE ) ).get( ) );
301 }
302
303 if ( ldapUser.getStreetTypeLabel( ) != null )
304 {
305 String strStreetTypeCode = SelfRegistrationUtils.getCodeOfLabel( request,
306 PROPERTY_STREET_TYPE_ITEMS_NUMBER, PROPERTY_LABEL_STREET_TYPE, ldapUser.getStreetTypeLabel( ) );
307 ldapUser.setStreetType( strStreetTypeCode );
308 }
309
310 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STREET ) ) != null )
311 {
312 ldapUser.setStreet( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STREET ) ).get( ) );
313 }
314
315 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_DISTRICT_NUMBER ) ) != null )
316 {
317 ldapUser.setDistrictNumber( (String) attrs.get( getLdapAttributes( )
318 .getProperty( LDAP_ATTRIBUTE_DISTRICT_NUMBER ) ).get( ) );
319 }
320
321 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_POSTAL_CODE ) ) != null )
322 {
323 ldapUser.setPostalCode( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_POSTAL_CODE ) )
324 .get( ) );
325 }
326
327 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_CITY ) ) != null )
328 {
329 ldapUser.setLn( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_CITY ) ).get( ) );
330 }
331
332 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STATE_PROV ) ) != null )
333 {
334 ldapUser.setSt( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_STATE_PROV ) ).get( ) );
335 }
336
337 if ( attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_COUNTRY ) ) != null )
338 {
339 ldapUser.setCo( (String) attrs.get( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_COUNTRY ) ).get( ) );
340 }
341
342 return ldapUser;
343 }
344
345
346
347
348
349
350
351 private Attributes getAttributes( LdapUser ldapUser, boolean isModification )
352 {
353 Attributes attributes = new BasicAttributes( );
354
355 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_CN ),
356 ldapUser.getSn( ) + " " + ldapUser.getGivenName( ) );
357
358 Attribute classes = new BasicAttribute( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_OBJECT_CLASS ) );
359 classes.add( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_OBJECT_CLASS_PERSON ) );
360 classes.add( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_OBJECT_CLASS_TOP ) );
361 classes.add( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_OBJECT_CLASS_ORG_PERSON ) );
362 classes.add( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_OBJECT_CLASS_INET_ORG_PERSON ) );
363 classes.add( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_OBJECT_CLASS_PARIS_PERSON ) );
364
365 attributes.put( classes );
366 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_CIVILITY ), ldapUser.getTitleLabel( ) );
367 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_LAST_NAME ), ldapUser.getSn( ) );
368 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_GIVEN_NAME ), ldapUser.getGivenName( ) );
369 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_EMAIL ), ldapUser.getMail( ) );
370
371 if ( ( ldapUser.getPasswd( ) != null ) && !ldapUser.getPasswd( ).equals( "" ) )
372 {
373 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_PASSWD ), ldapUser.getPasswd( ) );
374 }
375
376 putAttribute( attributes, LDAP_ATTRIBUTE_PHONE_NUMBER, ldapUser.getTelephoneNumber( ), isModification );
377 putAttribute( attributes, LDAP_ATTRIBUTE_STREET_NUMBER, ldapUser.getStreetNumber( ), isModification );
378 putAttribute( attributes, LDAP_ATTRIBUTE_STREET_NUMBER_SUFFIX, ldapUser.getStreetNumberSuffixLabel( ),
379 isModification );
380 putAttribute( attributes, LDAP_ATTRIBUTE_STREET_TYPE, ldapUser.getStreetTypeLabel( ), isModification );
381 putAttribute( attributes, LDAP_ATTRIBUTE_STREET, ldapUser.getStreet( ), isModification );
382
383 if ( ( ldapUser.getDistrictNumber( ) != null ) && !ldapUser.getDistrictNumber( ).equals( "0" ) )
384 {
385 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_DISTRICT_NUMBER ),
386 ldapUser.getDistrictNumber( ) );
387 }
388 else if ( isModification )
389 {
390 attributes.put( getLdapAttributes( ).getProperty( LDAP_ATTRIBUTE_DISTRICT_NUMBER ), null );
391 }
392
393 putAttribute( attributes, LDAP_ATTRIBUTE_POSTAL_CODE, ldapUser.getPostalCode( ), isModification );
394 putAttribute( attributes, LDAP_ATTRIBUTE_CITY, ldapUser.getLn( ), isModification );
395 putAttribute( attributes, LDAP_ATTRIBUTE_STATE_PROV, ldapUser.getSt( ), isModification );
396 putAttribute( attributes, LDAP_ATTRIBUTE_COUNTRY, ldapUser.getCo( ), isModification );
397
398 return attributes;
399 }
400
401
402
403
404
405
406
407
408 private void putAttribute( Attributes attributes, String attribute, String strValue, boolean isModification )
409 {
410 if ( ( strValue != null ) && !strValue.equals( "" ) )
411 {
412 attributes.put( getLdapAttributes( ).getProperty( attribute ), strValue );
413 }
414 else if ( isModification )
415 {
416 attributes.put( getLdapAttributes( ).getProperty( attribute ), null );
417 }
418 }
419
420
421
422
423 public String getLogin( )
424 {
425 return _strLogin;
426 }
427
428
429
430
431 public void setLogin( String strLogin )
432 {
433 _strLogin = strLogin;
434 }
435
436
437
438
439 public String getPassword( )
440 {
441 return _strPassword;
442 }
443
444
445
446
447 public void setPassword( String strPassword )
448 {
449 _strPassword = strPassword;
450 }
451
452
453
454
455 public String getContextFactory( )
456 {
457 return _strContextFactory;
458 }
459
460
461
462
463 public void setContextFactory( String strContextFactory )
464 {
465 _strContextFactory = strContextFactory;
466 }
467
468
469
470
471 public String getPoolMaxSize( )
472 {
473 return _strPoolMaxSize;
474 }
475
476
477
478
479 public void setPoolMaxSize( String strPoolMaxSize )
480 {
481 _strPoolMaxSize = strPoolMaxSize;
482 }
483
484
485
486
487 public String getPoolPrefSize( )
488 {
489 return _strPoolPrefSize;
490 }
491
492
493
494
495 public void setPoolPrefSize( String strPoolPrefSize )
496 {
497 _strPoolPrefSize = strPoolPrefSize;
498 }
499
500
501
502
503 public String getPoolTimeout( )
504 {
505 return _strPoolTimeout;
506 }
507
508
509
510
511 public void setPoolTimeout( String strPoolTimeout )
512 {
513 _strPoolTimeout = strPoolTimeout;
514 }
515
516
517
518
519 public String getProviderUrl( )
520 {
521 return _strProviderUrl;
522 }
523
524
525
526
527 public void setProviderUrl( String strProviderUrl )
528 {
529 _strProviderUrl = strProviderUrl;
530 }
531
532
533
534
535 public String getSecurityAuthentication( )
536 {
537 return _strSecurityAuthentication;
538 }
539
540
541
542
543 public void setSecurityAuthentication( String strSecurityAuthentication )
544 {
545 _strSecurityAuthentication = strSecurityAuthentication;
546 }
547
548
549
550
551 public String getOu( )
552 {
553 return _strOu;
554 }
555
556
557
558
559 public void setOu( String strOu )
560 {
561 _strOu = strOu;
562 }
563
564
565
566
567 public Properties getLdapAttributes( )
568 {
569 return _ldapAttributes;
570 }
571
572
573
574
575 public void setLdapAttributes( Properties ldapAttributes )
576 {
577 _ldapAttributes = ldapAttributes;
578 }
579 }