1 5 package org.exoplatform.services.organization.ldap; 6 7 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Vector ; 13 14 import net.sf.hibernate.Session; 15 import netscape.ldap.LDAPAttribute; 16 import netscape.ldap.LDAPAttributeSet; 17 import netscape.ldap.LDAPConnection; 18 import netscape.ldap.LDAPEntry; 19 import netscape.ldap.LDAPModification; 20 import netscape.ldap.LDAPModificationSet; 21 import netscape.ldap.LDAPSearchResults; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.exoplatform.services.database.HibernateService; 25 import org.exoplatform.services.database.XResources; 26 import org.exoplatform.services.ldap.LDAPService; 27 import org.exoplatform.services.ldap.LDAPServiceContainer; 28 import org.exoplatform.services.organization.Group; 29 import org.exoplatform.services.organization.GroupEventListener; 30 import org.exoplatform.services.organization.impl.GroupImpl; 31 32 38 public class GroupLDAPHandler extends BaseLDAPHandler { 39 public static final String BASEURL = OrganizationLDAPConfig.BASE_URL; 40 public static final String PORTALURL = OrganizationLDAPConfig.PORTAL_URL; 41 public static final String GROUPSURL = OrganizationLDAPConfig.GROUPS_URL; 42 public static final String USERSURL = OrganizationLDAPConfig.USERS_URL; 43 44 private LDAPServiceContainer serviceContainer_; 45 private HibernateService hService_; 46 private List listeners_; 47 48 public GroupLDAPHandler( 49 LDAPServiceContainer serviceContainer, 50 HibernateService hService) { 51 serviceContainer_ = serviceContainer; 52 hService_ = hService; 53 listeners_ = new ArrayList (5); 54 } 55 56 public void addGroupEventListener(GroupEventListener listener) { 57 listeners_.add(listener); 58 } 59 60 public void createGroup(Group group) throws Exception { 61 LDAPService service = null; 62 Session session = null; 63 try { 64 service = serviceContainer_.createLDAPService(); 65 session = hService_.openSession(); 66 String dn = "uid=" + group.getGroupName() + "," + GROUPSURL; 67 GroupImpl g = (GroupImpl) group; 68 String groupId = "/" + group.getGroupName(); 69 g.setId(groupId); 70 LDAPAttributeSet attrs = new LDAPAttributeSet(); 71 72 String objectclass_values[] = { "top", "exogroup" }; 73 setutil("objectclass", objectclass_values, attrs, false); 74 setutil("id", g.getId(), attrs); 75 setutil("uid", g.getGroupName(), attrs); 76 setutil("groupname", g.getGroupName(), attrs); 77 78 LDAPEntry myEntry = new LDAPEntry(dn, attrs); 79 preSave(g, true, session); 80 service.add(myEntry); 81 postSave(g, true, session); 82 session.flush(); 83 84 } finally { 85 serviceContainer_.closeLDAPService(service); 86 hService_.closeSession(session); 87 } 88 } 89 90 public void addChild(Group parent, Group child) throws Exception { 91 LDAPService service = null; 92 Session session = null; 93 try { 94 String MY_FILTER = "id=" + parent.getId(); 95 String MY_SEARCHBASE = GROUPSURL; 96 97 service = serviceContainer_.createLDAPService(); 98 session = hService_.openSession(); 99 LDAPSearchResults res = 100 service.search( 101 MY_SEARCHBASE, 102 LDAPConnection.SCOPE_SUB, 103 MY_FILTER, 104 null, 105 false); 106 107 List l = getUtil(res); 108 LDAPEntry findEntry = (LDAPEntry) l.get(0); 109 110 GroupImpl g = (GroupImpl) child; 111 g.setParentId(getutil("id", findEntry.getAttributeSet())); 112 113 if (findEntry != null) { 114 String dn = 115 "uid=" 116 + g.getGroupName() 117 + "," 118 + "uid=" 119 + getutil("groupname", findEntry.getAttributeSet()) 120 + "," 121 + GROUPSURL; 122 LDAPAttributeSet attrs = new LDAPAttributeSet(); 123 124 g.setId(g.getParentId() + "/" + child.getGroupName()); 125 String objectclass_values[] = { "top", "exogroup" }; 126 127 setutil("objectclass", objectclass_values, attrs, false); 128 setutil("id", g.getId(), attrs); 129 setutil("uid", g.getGroupName(), attrs); 130 setutil("groupname", g.getGroupName(), attrs); 131 132 LDAPEntry myEntry = new LDAPEntry(dn, attrs); 133 preSave(g, true, session); 134 service.add(myEntry); 135 postSave(g, true, session); 136 session.flush(); 137 } else { 138 throw new Exception ( 139 "cannot find the parent group" + parent.getId()); 140 } 141 } finally { 142 serviceContainer_.closeLDAPService(service); 143 hService_.closeSession(session); 144 } 145 } 146 147 public void saveGroup(Group group) throws Exception { 148 LDAPService service = null; 149 Session session = null; 150 GroupImpl groupImpl = (GroupImpl) group; 151 try { 152 service = serviceContainer_.createLDAPService(); 153 session = hService_.openSession(); 154 String olddn = groupImpl.getId(); 155 LDAPModificationSet mods = new LDAPModificationSet(); 156 mods.add( 157 LDAPModification.REPLACE, 158 new LDAPAttribute("groupname", groupImpl.getGroupName())); 159 160 preSave(group, false, session); 161 service.modify(olddn, mods); 162 service.rename(olddn, "uid=" + groupImpl.getGroupName(), true); 163 postSave(group, false, session); 164 session.flush(); 165 } finally { 166 hService_.closeSession(session); 167 serviceContainer_.closeLDAPService(service); 168 } 169 } 170 171 public Group removeGroup(Group group) throws Exception { 172 LDAPService service = null; 173 Session session = null; 174 Group foundGroup = null; 175 try { 176 service = serviceContainer_.createLDAPService(); 177 session = hService_.openSession(); 178 String MY_FILTER = "id=" + group.getId(); 179 String MY_SEARCHBASE = GROUPSURL; 180 181 LDAPSearchResults res = 182 service.search( 183 MY_SEARCHBASE, 184 LDAPConnection.SCOPE_SUB, 185 MY_FILTER, 186 null, 187 false); 188 189 List l = getUtil(res); 190 if (l.size() == 1) { 191 LDAPEntry findEntry = (LDAPEntry) l.get(0); 192 GroupImpl groupImpl = new GroupImpl(); 193 groupImpl.setId(getutil("id", findEntry.getAttributeSet())); 194 groupImpl.setGroupName( 195 getutil("groupname", findEntry.getAttributeSet())); 196 groupImpl.setGroupName( 197 getutil("description", findEntry.getAttributeSet())); 198 preDelete(foundGroup, session); 199 service.delete(findEntry.getDN()); 200 postDelete(foundGroup, session); 201 session.flush(); 202 } else { 203 throw new Exception ( 204 "Cannot find the group name " + group.getId()); 205 } 206 207 MY_FILTER = "membership=*"; 209 MY_SEARCHBASE = USERSURL; 210 211 res = 212 service.search( 213 MY_SEARCHBASE, 214 LDAPConnection.SCOPE_SUB, 215 MY_FILTER, 216 null, 217 false); 218 while (res.hasMoreElements()) { 219 LDAPEntry entry = res.next(); 220 221 Vector memberships = 222 getutil("membership", entry.getAttributeSet(), false); 223 for (Iterator i = memberships.iterator(); i.hasNext();) { 224 String s = (String ) i.next(); 225 String [] membership = StringUtils.split(s, ","); 226 if (membership[3].equals(group.getGroupName())) { 227 LDAPModificationSet mods = new LDAPModificationSet(); 228 mods.add( 229 LDAPModification.DELETE, 230 new LDAPAttribute("membership", s)); 231 service.modify(entry.getDN(), mods); 232 } 233 } 234 235 } 236 } finally { 237 service = serviceContainer_.createLDAPService(); 238 } 239 return foundGroup; 240 } 241 242 static void removeGroupEntry( 243 String groupName, 244 Session session, 245 LDAPService service) 246 throws Exception { 247 String MY_FILTER = "uid=" + groupName; 248 String MY_SEARCHBASE = GROUPSURL; 249 String ENTRYDN = MY_FILTER + MY_SEARCHBASE; 250 service.delete(ENTRYDN); 251 } 252 253 public Collection findGroupByMembership( 254 String userName, 255 String membershipType) 256 throws Exception { 257 LDAPService service = null; 258 List groups = new ArrayList (); 259 List groupsImpl = new ArrayList (); 260 Session session = null; 261 try { 262 service = serviceContainer_.createLDAPService(); 263 String MY_SEARCHBASE = USERSURL; 264 String MY_FILTER = "uid=" + userName; 265 LDAPSearchResults res = 266 service.search( 267 MY_SEARCHBASE, 268 LDAPConnection.SCOPE_SUB, 269 MY_FILTER, 270 null, 271 false); 272 273 List l = getUtil(res); 274 LDAPEntry findEntry = (LDAPEntry) l.get(0); 275 276 LDAPAttributeSet attrs = findEntry.getAttributeSet(); 277 Vector memberships = getutil("membership", attrs, false); 278 279 for (Iterator i = memberships.iterator(); i.hasNext();) { 280 String s = (String ) i.next(); 281 String [] membership = StringUtils.split(s, ","); 282 if (membership[1].equals(membershipType)) { 283 284 if (!groups.contains(membership[3])) { 285 groups.add(membership[3]); 286 GroupImpl groupImpl = new GroupImpl(); 287 groupImpl.setGroupName(membership[3]); 289 groupsImpl.add(groupImpl); 290 } 291 } 292 } 293 294 } finally { 295 serviceContainer_.closeLDAPService(service); 296 } 297 return groupsImpl; 298 } 299 300 public Group findGroupByName(String groupName) throws Exception { 301 Group group = null; 302 LDAPService service = null; 303 try { 304 service = serviceContainer_.createLDAPService(); 305 List l = null; 306 String MY_SEARCHBASE = GROUPSURL; 307 String MY_FILTER = "groupname=" + groupName; 308 309 LDAPSearchResults res = 310 service.search( 311 MY_SEARCHBASE, 312 LDAPConnection.SCOPE_SUB, 313 MY_FILTER, 314 null, 315 false); 316 317 l = getUtil(res); 318 LDAPEntry findEntry = (LDAPEntry) l.get(0); 319 320 LDAPAttributeSet attrs = findEntry.getAttributeSet(); 321 322 String groupname = getutil("groupname", attrs); 323 GroupImpl groupImpl = new GroupImpl(); 324 groupImpl.setId(findEntry.getDN()); 325 groupImpl.setParentId( 326 StringUtils.substringAfter(findEntry.getDN(), ",")); 327 groupImpl.setGroupName(groupname); 328 329 return (Group) groupImpl; 330 331 } finally { 332 serviceContainer_.closeLDAPService(service); 333 } 334 335 } 336 337 public Group findGroupById(String groupId) throws Exception { 338 LDAPService service = null; 339 try { 340 service = serviceContainer_.createLDAPService(); 341 LDAPEntry findEntry = service.read(groupId); 342 Group group = new GroupImpl(); 343 group.setGroupName( 344 getutil("groupname", findEntry.getAttributeSet())); 345 return group; 346 } finally { 347 serviceContainer_.closeLDAPService(service); 348 } 349 350 } 351 352 public Collection findGroups(Group parent) throws Exception { 353 Collection groups = null; 354 LDAPService service = null; 355 try { 356 service = serviceContainer_.createLDAPService(); 357 String MY_SEARCHBASE = GROUPSURL; 358 if (parent == null) { 359 360 LDAPSearchResults res = 361 service.search( 362 MY_SEARCHBASE, 363 LDAPConnection.SCOPE_SUB, 364 null, 365 null, 366 false); 367 List l = getUtil(res); 368 369 for (Iterator i = l.iterator(); i.hasNext();) { 370 LDAPEntry entry = (LDAPEntry) i.next(); 371 GroupImpl g = new GroupImpl(); 372 g.setGroupName( 373 getutil("groupname", entry.getAttributeSet())); 374 groups.add(g); 375 } 376 377 } else { 378 String MY_SEARCHBASE2 = "groupname=" + parent + MY_SEARCHBASE; 379 String MY_FILTER = "groupname=" + parent; 380 LDAPSearchResults res = 381 service.search( 382 MY_SEARCHBASE2, 383 LDAPConnection.SCOPE_SUB, 384 null, 385 null, 386 false); 387 List l = getUtil(res); 388 389 if (l.size() == 1) { 390 for (Iterator i = l.iterator(); i.hasNext();) { 391 LDAPEntry entry = (LDAPEntry) i.next(); 392 GroupImpl g = new GroupImpl(); 393 g.setGroupName( 394 getutil("groupname", entry.getAttributeSet())); 395 groups.add(g); 396 } 397 } else { 398 throw new Exception ( 399 "Cannot find the group parent " + parent); 400 } 401 } 402 } finally { 403 serviceContainer_.closeLDAPService(service); 404 } 405 return groups; 406 } 407 408 public Collection findGroupsOfUser(String user) throws Exception { 409 List groups = new ArrayList (); 410 LDAPService service = null; 411 try { 412 service = serviceContainer_.createLDAPService(); 413 String MY_SEARCHBASE = USERSURL; 414 String MY_FILTER = "uid=" + user; 415 LDAPSearchResults res = 416 service.search( 417 MY_SEARCHBASE, 418 LDAPConnection.SCOPE_SUB, 419 MY_FILTER, 420 null, 421 false); 422 423 List l = getUtil(res); 424 LDAPEntry findEntry = (LDAPEntry) l.get(0); 425 426 LDAPAttributeSet attrs = findEntry.getAttributeSet(); 427 Vector memberships = getutil("membership", attrs, false); 428 429 for (Iterator i = memberships.iterator(); i.hasNext();) { 430 String s = (String ) i.next(); 431 String [] membership = StringUtils.split(s, ","); 432 if (!groups.contains(membership[3])) { 433 groups.add(membership[3]); 434 } 435 } 436 437 } finally { 438 serviceContainer_.closeLDAPService(service); 439 } 440 return groups; 441 } 442 443 private void preSave(Group group, boolean isNew, Session session) 444 throws Exception { 445 XResources xresources = new XResources(); 446 xresources.addResource(Session.class, session); 447 for (int i = 0; i < listeners_.size(); i++) { 448 GroupEventListener listener = 449 (GroupEventListener) listeners_.get(i); 450 listener.preSave(group, isNew, xresources); 451 } 452 } 453 454 private void postSave(Group group, boolean isNew, Session session) 455 throws Exception { 456 XResources xresources = new XResources(); 457 xresources.addResource(Session.class, session); 458 for (int i = 0; i < listeners_.size(); i++) { 459 GroupEventListener listener = 460 (GroupEventListener) listeners_.get(i); 461 listener.postSave(group, isNew, xresources); 462 } 463 } 464 465 private void preDelete(Group group, Session session) throws Exception { 466 XResources xresources = new XResources(); 467 xresources.addResource(Session.class, session); 468 for (int i = 0; i < listeners_.size(); i++) { 469 GroupEventListener listener = 470 (GroupEventListener) listeners_.get(i); 471 listener.preDelete(group, xresources); 472 } 473 } 474 475 private void postDelete(Group group, Session session) throws Exception { 476 XResources xresources = new XResources(); 477 xresources.addResource(Session.class, session); 478 for (int i = 0; i < listeners_.size(); i++) { 479 GroupEventListener listener = 480 (GroupEventListener) listeners_.get(i); 481 listener.postDelete(group, xresources); 482 } 483 } 484 485 } | Popular Tags |