KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > ldap > LdapUserStore


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

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 JavaDoc
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 JavaDoc
54     {
55         BasicAttributes attrs = new BasicAttributes();
56
57         //mapped user attributes
58
attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), user.getName()));
59         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("password"), user.getPassword()));
60         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("language"), user.getLanguage()));
61         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("startupPlugin"), user.getStartupPlugin()));
62         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("locked"), user.isLocked() ? "1" : "0"));
63
64         if(user.getRealName() != null && user.getRealName().length() > 0)
65             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("realName"), user.getRealName()));
66         if(user.getMailAddress() != null && user.getMailAddress().length() > 0)
67             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("mailAddress"), user.getMailAddress()));
68
69         if(user.getDescription() != null && user.getDescription().length() > 0)
70             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), user.getDescription()));
71
72         //other attributes
73
Iterator keys = attributes.keySet().iterator();
74         while(keys.hasNext())
75         {
76             String JavaDoc key = (String JavaDoc)keys.next();
77             attrs.put(new BasicAttribute(key, attributes.get(key)));
78         }
79
80         context.createSubcontext((String JavaDoc)mapping.get("name") + '=' + user.getName(), attrs);
81     }
82
83     public void updateUser(UserConcept user)
84     throws Exception JavaDoc
85     {
86         BasicAttributes attrs = new BasicAttributes();
87
88         //mapped user attributes
89
attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), user.getName()));
90         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("password"), user.getPassword()));
91         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("realName"), user.getRealName()));
92         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("mailAddress"), user.getMailAddress()));
93         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("language"), user.getLanguage()));
94         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("startupPlugin"), user.getStartupPlugin()));
95         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("locked"), user.isLocked() ? "1" : "0"));
96
97         if(user.getDescription() != null && user.getDescription().length() > 0)
98             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), user.getDescription()));
99
100         //other attributes
101
Iterator keys = attributes.keySet().iterator();
102         while(keys.hasNext())
103         {
104             String JavaDoc key = (String JavaDoc)keys.next();
105             attrs.put(new BasicAttribute(key, attributes.get(key)));
106         }
107
108         context.modifyAttributes((String JavaDoc)mapping.get("name") + '=' + user.getName(), DirContext.REPLACE_ATTRIBUTE, attrs);
109     }
110
111     public void removeUser(UserConcept user)
112     throws Exception JavaDoc
113     {
114         LdapGroupStore groups = (LdapGroupStore)Server.getInstance().getStore().getGroupStore();
115         groups.removeUserLinks(user.getName());
116
117         context.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + user.getName());
118     }
119
120     public UserConcept getUser(String JavaDoc login)
121     throws Exception JavaDoc
122     {
123         String JavaDoc userKey = (String JavaDoc)mapping.get("name") + '=' + login;
124
125         Attributes attrs = context.getAttributes(userKey);
126
127         String JavaDoc name = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("name")).get();
128         String JavaDoc password = new String JavaDoc((byte[])attrs.get((String JavaDoc)mapping.get("password")).get());
129         String JavaDoc language = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("language")).get();
130         String JavaDoc startupPlugin = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("startupPlugin")).get();
131         String JavaDoc locked = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("locked")).get();
132
133         String JavaDoc realName = "";
134         try {
135             realName = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("realName")).get();
136         } catch(Exception JavaDoc e) {
137             //no name
138
}
139
140         String JavaDoc mailAddress = "";
141         try {
142             mailAddress = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("mailAddress")).get();
143         } catch(Exception JavaDoc e) {
144             //no mail adress
145
}
146
147         UserConcept user = new UserConcept(name, password, realName, mailAddress, language,
148             (locked.equals("1")), startupPlugin);
149
150
151         try {
152             String JavaDoc description = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("description")).get();
153             user.setDescription(description);
154         } catch(Exception JavaDoc e) {
155             //no description
156
}
157
158         return user;
159     }
160
161     public Iterator getAllUsers()
162     throws Exception JavaDoc
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 JavaDoc 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