1 19 package org.lucane.server.store.ldap; 20 21 import java.util.*; 22 23 import org.lucane.common.concepts.UserConcept; 24 import org.lucane.common.concepts.GroupConcept; 25 import org.lucane.server.store.UserStore; 26 import org.lucane.server.Server; 27 28 import javax.naming.*; 29 import javax.naming.directory.*; 30 31 public class LdapUserStore extends UserStore 32 { 33 private DirContext context; 34 private HashMap mapping; 35 private HashMap attributes; 36 37 public LdapUserStore(LdapConfig config) 38 throws Exception 39 { 40 Hashtable ht = new Hashtable(); 41 ht.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 42 ht.put(DirContext.SECURITY_AUTHENTICATION, config.getAuthType()); 43 ht.put(DirContext.SECURITY_PRINCIPAL, config.getAuthBindDn()); 44 ht.put(DirContext.SECURITY_CREDENTIALS, config.getAuthPassword()); 45 ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + config.getUsersDn()); 46 this.context = new InitialDirContext(ht); 47 48 this.attributes = config.getUsersAttributes(); 49 this.mapping = config.getUsersMapping(); 50 } 51 52 public void storeUser(UserConcept user) 53 throws Exception 54 { 55 BasicAttributes attrs = new BasicAttributes(); 56 57 attrs.put(new BasicAttribute((String )mapping.get("name"), user.getName())); 59 attrs.put(new BasicAttribute((String )mapping.get("password"), user.getPassword())); 60 attrs.put(new BasicAttribute((String )mapping.get("language"), user.getLanguage())); 61 attrs.put(new BasicAttribute((String )mapping.get("startupPlugin"), user.getStartupPlugin())); 62 attrs.put(new BasicAttribute((String )mapping.get("locked"), user.isLocked() ? "1" : "0")); 63 64 if(user.getRealName() != null && user.getRealName().length() > 0) 65 attrs.put(new BasicAttribute((String )mapping.get("realName"), user.getRealName())); 66 if(user.getMailAddress() != null && user.getMailAddress().length() > 0) 67 attrs.put(new BasicAttribute((String )mapping.get("mailAddress"), user.getMailAddress())); 68 69 if(user.getDescription() != null && user.getDescription().length() > 0) 70 attrs.put(new BasicAttribute((String )mapping.get("description"), user.getDescription())); 71 72 Iterator keys = attributes.keySet().iterator(); 74 while(keys.hasNext()) 75 { 76 String key = (String )keys.next(); 77 attrs.put(new BasicAttribute(key, attributes.get(key))); 78 } 79 80 context.createSubcontext((String )mapping.get("name") + '=' + user.getName(), attrs); 81 } 82 83 public void updateUser(UserConcept user) 84 throws Exception 85 { 86 BasicAttributes attrs = new BasicAttributes(); 87 88 attrs.put(new BasicAttribute((String )mapping.get("name"), user.getName())); 90 attrs.put(new BasicAttribute((String )mapping.get("password"), user.getPassword())); 91 attrs.put(new BasicAttribute((String )mapping.get("realName"), user.getRealName())); 92 attrs.put(new BasicAttribute((String )mapping.get("mailAddress"), user.getMailAddress())); 93 attrs.put(new BasicAttribute((String )mapping.get("language"), user.getLanguage())); 94 attrs.put(new BasicAttribute((String )mapping.get("startupPlugin"), user.getStartupPlugin())); 95 attrs.put(new BasicAttribute((String )mapping.get("locked"), user.isLocked() ? "1" : "0")); 96 97 if(user.getDescription() != null && user.getDescription().length() > 0) 98 attrs.put(new BasicAttribute((String )mapping.get("description"), user.getDescription())); 99 100 Iterator keys = attributes.keySet().iterator(); 102 while(keys.hasNext()) 103 { 104 String key = (String )keys.next(); 105 attrs.put(new BasicAttribute(key, attributes.get(key))); 106 } 107 108 context.modifyAttributes((String )mapping.get("name") + '=' + user.getName(), DirContext.REPLACE_ATTRIBUTE, attrs); 109 } 110 111 public void removeUser(UserConcept user) 112 throws Exception 113 { 114 LdapGroupStore groups = (LdapGroupStore)Server.getInstance().getStore().getGroupStore(); 115 groups.removeUserLinks(user.getName()); 116 117 context.destroySubcontext((String )mapping.get("name") + '=' + user.getName()); 118 } 119 120 public UserConcept getUser(String login) 121 throws Exception 122 { 123 String userKey = (String )mapping.get("name") + '=' + login; 124 125 Attributes attrs = context.getAttributes(userKey); 126 127 String name = (String )attrs.get((String )mapping.get("name")).get(); 128 String password = new String ((byte[])attrs.get((String )mapping.get("password")).get()); 129 String language = (String )attrs.get((String )mapping.get("language")).get(); 130 String startupPlugin = (String )attrs.get((String )mapping.get("startupPlugin")).get(); 131 String locked = (String )attrs.get((String )mapping.get("locked")).get(); 132 133 String realName = ""; 134 try { 135 realName = (String )attrs.get((String )mapping.get("realName")).get(); 136 } catch(Exception e) { 137 } 139 140 String mailAddress = ""; 141 try { 142 mailAddress = (String )attrs.get((String )mapping.get("mailAddress")).get(); 143 } catch(Exception e) { 144 } 146 147 UserConcept user = new UserConcept(name, password, realName, mailAddress, language, 148 (locked.equals("1")), startupPlugin); 149 150 151 try { 152 String description = (String )attrs.get((String )mapping.get("description")).get(); 153 user.setDescription(description); 154 } catch(Exception e) { 155 } 157 158 return user; 159 } 160 161 public Iterator getAllUsers() 162 throws Exception 163 { 164 ArrayList users = new ArrayList(); 165 166 NamingEnumeration list = context.list(""); 167 while(list.hasMore()) 168 { 169 NameClassPair pair = (NameClassPair)list.next(); 170 String login = pair.getName(); 171 login = login.substring(login.indexOf('=')+1); 172 173 users.add(getUser(login)); 174 } 175 176 return users.iterator(); 177 } 178 } 179 | Popular Tags |