1 19 package org.lucane.server.store.ldap; 20 21 import java.util.*; 22 23 import org.lucane.common.concepts.*; 24 import org.lucane.server.store.*; 25 26 import javax.naming.*; 27 import javax.naming.directory.*; 28 29 50 51 public class LdapGroupStore extends GroupStore 52 { 53 private Store store; 54 55 private DirContext groupContext; 56 private DirContext userContext; 57 private DirContext pluginContext; 58 private DirContext serviceContext; 59 private HashMap mapping; 60 private HashMap attributes; 61 62 public LdapGroupStore(Store store, LdapConfig config) 63 throws Exception 64 { 65 this.store = store; 66 67 Hashtable ht = new Hashtable(); 68 ht.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 69 ht.put(DirContext.SECURITY_AUTHENTICATION, config.getAuthType()); 70 ht.put(DirContext.SECURITY_PRINCIPAL, config.getAuthBindDn()); 71 ht.put(DirContext.SECURITY_CREDENTIALS, config.getAuthPassword()); 72 73 ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=groups," +config.getGroupsDn()); 74 this.groupContext = new InitialDirContext(ht); 75 ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=users," +config.getGroupsDn()); 76 this.userContext = new InitialDirContext(ht); 77 ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=plugins," +config.getGroupsDn()); 78 this.pluginContext = new InitialDirContext(ht); 79 ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=services," +config.getGroupsDn()); 80 this.serviceContext = new InitialDirContext(ht); 81 82 this.attributes = config.getGroupsAttributes(); 83 this.mapping = config.getGroupsMapping(); 84 } 85 86 91 public boolean isInitialized() 92 throws Exception 93 { 94 return getAllGroups().hasNext(); 95 } 96 97 public void storeGroup(GroupConcept group) 98 throws Exception 99 { 100 BasicAttributes attrs = getBasicAttributes(group); 102 103 Iterator i = group.getParents(); 105 BasicAttribute member = new BasicAttribute((String )mapping.get("member"), "cn=null"); 106 while(i.hasNext()) 107 { 108 GroupConcept parent = (GroupConcept)i.next(); 109 member.add("cn=" + parent.getName()); 110 } 111 attrs.put(member); 112 groupContext.createSubcontext((String )mapping.get("name") + '=' + group.getName(), attrs); 113 114 attrs = getBasicAttributes(group); 116 i = group.getUsers(); 117 member = new BasicAttribute((String )mapping.get("member"), "cn=null"); 118 while(i.hasNext()) 119 { 120 UserConcept user = (UserConcept)i.next(); 121 member.add("cn=" + user.getName()); 122 } 123 attrs.put(member); 124 userContext.createSubcontext((String )mapping.get("name") + '=' + group.getName(), attrs); 125 126 attrs = getBasicAttributes(group); 128 i = group.getServices(); 129 member = new BasicAttribute((String )mapping.get("member"), "cn=null"); 130 while(i.hasNext()) 131 { 132 ServiceConcept service = (ServiceConcept)i.next(); 133 member.add("cn=" + service.getName()); 134 } 135 attrs.put(member); 136 serviceContext.createSubcontext((String )mapping.get("name") + '=' + group.getName(), attrs); 137 138 attrs = getBasicAttributes(group); 140 i = group.getPlugins(); 141 member = new BasicAttribute((String )mapping.get("member"), "cn=null"); 142 while(i.hasNext()) 143 { 144 PluginConcept plugin = (PluginConcept)i.next(); 145 member.add("cn=" + plugin.getName()); 146 } 147 attrs.put(member); 148 pluginContext.createSubcontext((String )mapping.get("name") + '=' + group.getName(), attrs); 149 150 } 151 152 public void updateGroup(GroupConcept group) 153 throws Exception 154 { 155 removeGroupOnly(group); 156 storeGroup(group); 157 } 158 159 public void removeGroup(GroupConcept group) 160 throws Exception 161 { 162 removeGroupOnly(group); 163 removeGroupLinks(group.getName()); 164 } 165 166 public GroupConcept getGroup(String name) 167 throws Exception 168 { 169 GroupConcept group = null; 170 171 String groupKey = (String )mapping.get("name") + '=' + name; 172 Attributes attrs = groupContext.getAttributes(groupKey); 173 174 name = (String )attrs.get((String )mapping.get("name")).get(); 175 group = new GroupConcept(name); 176 177 try { 178 String description = (String )attrs.get((String )mapping.get("description")).get(); 179 group.setDescription(description); 180 } catch(Exception e) { 181 } 183 184 setGroupLinks(group); 185 setUserLinks(group); 186 setPluginLinks(group); 187 setServiceLinks(group); 188 189 return group; 190 } 191 192 public Iterator getAllGroups() 193 throws Exception 194 { 195 ArrayList groups = new ArrayList(); 196 197 NamingEnumeration list = groupContext.list(""); 198 while(list.hasMore()) 199 { 200 NameClassPair pair = (NameClassPair)list.next(); 201 String name = pair.getName(); 202 name = name.substring(name.indexOf('=')+1); 203 204 groups.add(getGroup(name)); 205 } 206 207 return groups.iterator(); 208 } 209 210 protected void removeServiceLinks(String name) 212 throws Exception 213 { 214 String memberKey = (String )mapping.get("name") + "=" + name; 215 216 BasicAttributes attrs = new BasicAttributes(); 217 attrs.put(new BasicAttribute((String )mapping.get("member"), memberKey)); 218 NamingEnumeration e = serviceContext.search("", attrs); 219 while(e.hasMoreElements()) 220 { 221 NameClassPair pair = (NameClassPair)e.nextElement(); 222 serviceContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs); 223 } 224 } 225 226 protected void removePluginLinks(String name) 227 throws Exception 228 { 229 String memberKey = (String )mapping.get("name") + "=" + name; 230 231 BasicAttributes attrs = new BasicAttributes(); 232 attrs.put(new BasicAttribute((String )mapping.get("member"), memberKey)); 233 NamingEnumeration e = pluginContext.search("", attrs); 234 while(e.hasMoreElements()) 235 { 236 NameClassPair pair = (NameClassPair)e.nextElement(); 237 pluginContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs); 238 } 239 } 240 241 protected void removeUserLinks(String name) 242 throws Exception 243 { 244 String memberKey = (String )mapping.get("name") + "=" + name; 245 246 BasicAttributes attrs = new BasicAttributes(); 247 attrs.put(new BasicAttribute((String )mapping.get("member"), memberKey)); 248 NamingEnumeration e = userContext.search("", attrs); 249 while(e.hasMoreElements()) 250 { 251 NameClassPair pair = (NameClassPair)e.nextElement(); 252 userContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs); 253 } 254 } 255 256 protected void removeGroupLinks(String name) 257 throws Exception 258 { 259 String memberKey = (String )mapping.get("name") + "=" + name; 260 261 BasicAttributes attrs = new BasicAttributes(); 262 attrs.put(new BasicAttribute((String )mapping.get("member"), memberKey)); 263 NamingEnumeration e = groupContext.search("", attrs); 264 while(e.hasMoreElements()) 265 { 266 NameClassPair pair = (NameClassPair)e.nextElement(); 267 groupContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs); 268 } 269 } 270 271 273 private void setServiceLinks(GroupConcept group) 274 throws Exception 275 { 276 String groupKey = (String )mapping.get("name") + '=' + group.getName(); 277 Attributes attrs = serviceContext.getAttributes(groupKey); 278 279 Attribute attr = attrs.get((String )mapping.get("member")); 280 NamingEnumeration list = attr.getAll(); 281 282 while(list.hasMore()) 283 { 284 String name = (String )list.next(); 285 name = name.substring(name.indexOf('=')+1); 286 if(name.equals("null")) 287 continue; 288 289 ServiceConcept service = store.getServiceStore().getService(name); 290 group.addService(service); 291 } 292 } 293 294 private void setPluginLinks(GroupConcept group) 295 throws Exception 296 { 297 String groupKey = (String )mapping.get("name") + '=' + group.getName(); 298 Attributes attrs = pluginContext.getAttributes(groupKey); 299 300 Attribute attr = attrs.get((String )mapping.get("member")); 301 NamingEnumeration list = attr.getAll(); 302 303 while(list.hasMore()) 304 { 305 String name = (String )list.next(); 306 name = name.substring(name.indexOf('=')+1); 307 if(name.equals("null")) 308 continue; 309 310 PluginConcept plugin = store.getPluginStore().getPlugin(name); 311 group.addPlugin(plugin); 312 } 313 } 314 315 private void setUserLinks(GroupConcept group) 316 throws Exception 317 { 318 String groupKey = (String )mapping.get("name") + '=' + group.getName(); 319 Attributes attrs = userContext.getAttributes(groupKey); 320 321 Attribute attr = attrs.get((String )mapping.get("member")); 322 NamingEnumeration list = attr.getAll(); 323 324 while(list.hasMore()) 325 { 326 String name = (String )list.next(); 327 name = name.substring(name.indexOf('=')+1); 328 if(name.equals("null")) 329 continue; 330 331 UserConcept user = store.getUserStore().getUser(name); 332 group.addUser(user); 333 } 334 } 335 336 private void setGroupLinks(GroupConcept group) 337 throws Exception 338 { 339 String groupKey = (String )mapping.get("name") + '=' + group.getName(); 340 Attributes attrs = groupContext.getAttributes(groupKey); 341 342 Attribute attr = attrs.get((String )mapping.get("member")); 343 NamingEnumeration list = attr.getAll(); 344 345 while(list.hasMore()) 346 { 347 String name = (String )list.next(); 348 name = name.substring(name.indexOf('=')+1); 349 if(name.equals("null")) 350 continue; 351 352 GroupConcept parent = store.getGroupStore().getGroup(name); 353 group.addParent(parent); 354 } 355 } 356 357 private void removeGroupOnly(GroupConcept group) 358 throws Exception 359 { 360 groupContext.destroySubcontext((String )mapping.get("name") + '=' + group.getName()); 362 363 userContext.destroySubcontext((String )mapping.get("name") + '=' + group.getName()); 365 366 serviceContext.destroySubcontext((String )mapping.get("name") + '=' + group.getName()); 368 369 pluginContext.destroySubcontext((String )mapping.get("name") + '=' + group.getName()); 371 } 372 373 private BasicAttributes getBasicAttributes(GroupConcept group) 374 { 375 BasicAttributes attrs = new BasicAttributes(); 376 attrs.put(new BasicAttribute((String )mapping.get("name"), group.getName())); 377 if(group.getDescription() != null && group.getDescription().length() > 0) 378 attrs.put(new BasicAttribute((String )mapping.get("description"), group.getDescription())); 379 380 Iterator keys = attributes.keySet().iterator(); 382 while(keys.hasNext()) 383 { 384 String key = (String )keys.next(); 385 attrs.put(new BasicAttribute(key, attributes.get(key))); 386 } 387 388 return attrs; 389 } 390 } | Popular Tags |