KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > sync > ldap > LDAPUser


1 package de.webman.sync.ldap;
2
3 import javax.naming.directory.*;
4 import javax.naming.*;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import de.webman.sync.User;
9
10
11 /**
12  * The ldap specific implementation of {@link de.webman.sync.User}.
13  *
14  * @author <a HREF="mailto:gregor@webman.de">Gregor Klinke</a>
15  * @version $Revision: 1.2 $
16  **/

17 public class LDAPUser
18     implements User
19 {
20     /* $Id: LDAPUser.java,v 1.2 2002/04/12 14:56:02 gregor Exp $ */
21
22     /**
23      * The relative distinguished name (e.g. cn=root)
24      **/

25     String JavaDoc dn = null;
26
27     /**
28      * the list of webman groups
29      **/

30     ArrayList JavaDoc groups = new ArrayList JavaDoc();
31     
32     /**
33      * has the user changed? (is the dirty flag set)
34      **/

35     boolean dirty = false;
36
37     /**
38      * the display name of the user
39      **/

40     String JavaDoc displayName = null;
41
42     /**
43      * the login name of the user
44      **/

45     String JavaDoc login = null;
46
47     /**
48      * creates a ldap user from a ldap searchresult
49      * @param sr a searchresult as returned from a
50      * <code>DirContext.search()</code> command
51      * @param attributes LDAP specific attributes from the LDAP
52      * structure,LDAPAdaptor.ATTRIBUTE_XXX fields specify the contents of
53      * the slots.
54      * @throws NamingException if ldap errors occur
55      **/

56     public LDAPUser (SearchResult sr, String JavaDoc[] attributes)
57         throws NamingException
58     {
59         dn = sr.getName();
60         
61         for (NamingEnumeration ae = sr.getAttributes().getAll(); ae.hasMore();) {
62             Attribute attr = (Attribute)ae.next();
63
64             if (attributes[LDAPAdaptor.ATTRIBUTE_WEBMAN_GROUP] != null &&
65                 attributes[LDAPAdaptor.ATTRIBUTE_WEBMAN_GROUP].equals(attr.getID())) {
66                 for (NamingEnumeration e = attr.getAll();
67                      e.hasMore(); ) {
68                     String JavaDoc val = e.next().toString();
69                     if (!groups.contains(val))
70                         groups.add(val);
71                 }
72             }
73             else if (attributes[LDAPAdaptor.ATTRIBUTE_DIRTY_FLAG] != null &&
74                      attributes[LDAPAdaptor.ATTRIBUTE_DIRTY_FLAG].equals(attr.getID())) {
75                 /* only check the first value of this attribute */
76                 NamingEnumeration e = attr.getAll();
77                 if (e.hasMore()) {
78                     /* FIXME: which type has this element?? */
79                     String JavaDoc val = e.next().toString();
80                     if (val != null)
81                         dirty = attributes[LDAPAdaptor.ATTRIBUTE_YES_VALUE].equals(val);
82                     else
83                         dirty = false;
84                 }
85             }
86             else if (attributes[LDAPAdaptor.ATTRIBUTE_DISPLAY_NAME] != null &&
87                      attributes[LDAPAdaptor.ATTRIBUTE_DISPLAY_NAME].equals(attr.getID())) {
88                 NamingEnumeration e = attr.getAll();
89                 if (e.hasMore())
90                     displayName = e.next().toString();
91             }
92             else if (attributes[LDAPAdaptor.ATTRIBUTE_LOGIN] != null &&
93                      attributes[LDAPAdaptor.ATTRIBUTE_LOGIN].equals(attr.getID())) {
94                 NamingEnumeration e = attr.getAll();
95                 if (e.hasMore())
96                     login = e.next().toString();
97             }
98         }
99     }
100     
101
102     /**
103      * returns a list of all webman groups the user is listed in the
104      * external acl system for. The list contains java.lang.String
105      * elements. */

106     public List JavaDoc getGroups() {
107         return groups;
108     }
109
110     /**
111      * returns the id of the user in the acl system specific format. */

112     public String JavaDoc getID() {
113         return dn;
114     }
115     
116     /**
117      * returns the id of the user in the webman specific format (the
118      * "login" name) */

119     public String JavaDoc getWebmanName() {
120         if (login != null)
121             return login;
122         if (dn != null && dn.startsWith("cn="))
123             return dn.substring(3);
124         return dn;
125     }
126
127     /**
128      * returns a display name of the user (which is maybe his/her real name)
129      **/

130     public String JavaDoc getDisplayName() {
131         if (displayName != null)
132             return displayName;
133         return getWebmanName();
134     }
135
136     /**
137      * returns <code>true</code> if the user entry has been changed/added
138      * in the external acl system */

139     public boolean isDirty() {
140         return dirty;
141     }
142
143
144
145     /**
146      * returns a (debug) string representation of this user
147      **/

148     public String JavaDoc toString() {
149         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
150
151         buf.append("[");
152         buf.append(dn);
153         buf.append(": ");
154         for (Iterator JavaDoc it = groups.iterator(); it.hasNext(); ) {
155             buf.append((String JavaDoc)it.next());
156             if (it.hasNext())
157                 buf.append(", ");
158         }
159
160         buf.append("]");
161
162         if (dirty)
163             buf.append("*");
164
165         return buf.toString();
166     }
167 }
168
Popular Tags