1 5 package org.exoplatform.services.organization.ldap; 6 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.List ; 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 37 public class UserLDAPHandler extends BaseLDAPHandler { 38 39 private final String BASEURL = OrganizationLDAPConfig.BASE_URL; 40 private final String PORTALURL = OrganizationLDAPConfig.PORTAL_URL; 41 private final String ROLESURL = OrganizationLDAPConfig.ROLES_URL; 42 private final String USERSURL = OrganizationLDAPConfig.USERS_URL; 43 44 private LDAPServiceContainer serviceContainer_; 45 private HibernateService hService_; 46 private List listeners_; 47 48 public UserLDAPHandler( 49 LDAPServiceContainer serviceContainer, 50 HibernateService hService) { 51 serviceContainer_ = serviceContainer; 52 hService_ = hService; 53 listeners_ = new ArrayList (5); 54 } 55 56 final public List 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 { 65 LDAPService service = null; 66 Session session = null; 67 try { 68 service = serviceContainer_.createLDAPService(); 69 session = hService_.openSession(); 70 String dn = "uid=" + user.getUserName() + "," + USERSURL; 71 LDAPAttributeSet attrs = new LDAPAttributeSet(); 72 73 String 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 { 96 LDAPService service = null; 97 Session session = null; 98 try { 99 service = serviceContainer_.createLDAPService(); 100 session = hService_.openSession(); 101 String 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 { 127 LDAPService service = null; 128 129 try { 130 service = serviceContainer_.createLDAPService(); 131 132 String dn = "uid=" + user.getUserName() + "," + USERSURL; 133 LDAPAttributeSet attrs = new LDAPAttributeSet(); 134 135 String 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 userName) throws Exception { 157 LDAPService service = null; 158 Session session = null; 159 User foundUser = null; 160 try { 161 service = serviceContainer_.createLDAPService(); 162 session = hService_.openSession(); 163 String MY_FILTER = "uid=" + userName + ","; 164 String MY_SEARCHBASE = USERSURL; 165 166 String 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 184 void removeUserEntry(User user, Session session) throws Exception { 185 LDAPService service = null; 186 187 service = serviceContainer_.createLDAPService(); 188 189 String MY_FILTER = "uid=" + user.getUserName(); 190 String MY_SEARCHBASE = USERSURL; 191 192 String ENTRYDN = MY_FILTER + MY_SEARCHBASE; 193 194 service.delete(ENTRYDN); 195 serviceContainer_.closeLDAPService(service); 196 197 } 198 199 public User findUserByName(String userName) throws Exception { 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 userName, LDAPService service) 212 throws Exception { 213 User user = new UserImpl(); 214 String MY_FILTER = "uid=" + userName; 215 String MY_SEARCHBASE = USERSURL; 216 217 LDAPSearchResults res = 218 service.search( MY_SEARCHBASE,LDAPConnection.SCOPE_SUB, MY_FILTER, null, false); 219 220 List 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 { 234 LDAPService service = null; 235 Session session = null; 236 service = serviceContainer_.createLDAPService(); 237 session = hService_.openSession(); 238 239 String 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 [] sortAttrs = { "uid" }; 249 boolean[] ascending = { true, true }; 250 res.sort(new LDAPCompareAttrNames(sortAttrs, ascending)); 251 252 List users = new ArrayList (); 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 new ObjectPageList(users, pageSize); 269 } 271 272 public PageList findUsers(Query q) throws Exception { 273 274 LDAPService service = null; 275 Session session = null; 276 service = serviceContainer_.createLDAPService(); 277 session = hService_.openSession(); 278 String MY_FILTER = null; 279 if (q.getUserName() != null) { 280 String 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 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 [] sortAttrs = { "uid" }; 298 boolean[] ascending = { true, true }; 299 res.sort(new LDAPCompareAttrNames(sortAttrs, ascending)); 300 301 List users = new ArrayList (); 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 332 hService_.closeSession(session); 333 serviceContainer_.closeLDAPService(service); 334 return new ObjectPageList(users, 20); 336 } 337 338 public PageList findUsersByGroup(String groupId) throws Exception { 339 340 LDAPService service = null; 341 Session session = null; 342 service = serviceContainer_.createLDAPService(); 343 session = hService_.openSession(); 344 345 String MY_SEARCHBASE = USERSURL; 346 String 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 [] sortAttrs = { "uid" }; 355 boolean[] ascending = { true, true }; 356 res.sort(new LDAPCompareAttrNames(sortAttrs, ascending)); 357 358 List users = new ArrayList (); 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 new ObjectPageList(users, 20); 379 } 380 381 public Collection findUsersByGroupAndRole(String groupName, String role) 382 throws Exception { 383 384 LDAPService service = null; 385 Session session = null; 386 service = serviceContainer_.createLDAPService(); 387 session = hService_.openSession(); 388 389 String MY_SEARCHBASE = USERSURL; 390 String 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 [] sortAttrs = { "uid" }; 401 boolean[] ascending = { true, true }; 402 res.sort(new LDAPCompareAttrNames(sortAttrs, ascending)); 403 404 List users = new ArrayList (); 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 { 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 { 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 { 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 { 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 |