KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > filesystem > FileSystemUser


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.users.filesystem;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import com.openedit.users.Group;
23 import com.openedit.users.StringEncrypter;
24 import com.openedit.users.User;
25 import com.openedit.users.UserManagerException;
26
27
28 /**
29  * This class represents a user as an XML file.
30  *
31  * @author Eric and Matt
32  */

33 public class FileSystemUser extends FileSystemObject implements User
34 {
35     protected Set JavaDoc fieldGroups;
36     protected String JavaDoc fieldPassword;
37     protected String JavaDoc fieldUserName;
38
39     
40     public FileSystemUser()
41     {
42         super();
43     }
44     
45     public String JavaDoc getEmail()
46     {
47         return getString(EMAIL_PROPERTY);
48     }
49
50     public String JavaDoc getFirstName()
51     {
52         return getString(FIRST_NAME_PROPERTY);
53     }
54     public void setFirstName( String JavaDoc inName)
55     {
56         safePut( FIRST_NAME_PROPERTY, inName);
57     }
58     /**
59      * @see com.openedit.users.User#getGroups()
60      */

61     public Collection JavaDoc getGroups()
62     {
63         if (fieldGroups == null)
64         {
65             fieldGroups = new HashSet JavaDoc(3);
66         }
67         return fieldGroups;
68     }
69
70     public String JavaDoc getLastName()
71     {
72         return getString(LAST_NAME_PROPERTY);
73     }
74
75     public void setLastName( String JavaDoc inName)
76     {
77         safePut( LAST_NAME_PROPERTY, inName);
78     }
79     public void setEmail( String JavaDoc inEmail )
80     {
81         safePut(EMAIL_PROPERTY, inEmail);
82     }
83     /**
84      * @see com.openedit.users.User#setPassword(String)
85      */

86     public void setPassword(String JavaDoc inPassword) throws UserManagerException
87     {
88         fieldPassword = inPassword;
89     }
90
91     /**
92      * @see com.openedit.users.User#getUserName()
93      */

94     public String JavaDoc getUserName()
95     {
96         return fieldUserName;
97     }
98
99     public void setUserName( String JavaDoc inName)
100     {
101         fieldUserName = inName;
102     }
103     
104     public String JavaDoc getShortDescription()
105     {
106         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
107         if ( getFirstName() != null)
108         {
109             out.append( getFirstName() );
110             out.append(" ");
111         }
112         if ( getLastName() != null)
113         {
114             out.append(getLastName());
115         }
116         if( out.length() == 0)
117         {
118             if( getEmail() != null && Character.isDigit(getUserName().charAt(0) ) )
119             {
120                 out.append(getEmail());
121             }
122             else
123             {
124                 out.append( getUserName());
125             }
126         }
127         return out.toString();
128     }
129
130     /**
131      * @deprecated Should be moved to authenticator API
132      */

133     public String JavaDoc getClearPassword() throws UserManagerException
134     {
135         String JavaDoc password = getPassword();
136         if( !password.startsWith("DES:") )
137         {
138             return password;
139         }
140         else
141         {
142             return decrypt(password);
143         }
144     }
145     
146     protected String JavaDoc decrypt(String JavaDoc inPassword) throws UserManagerException
147     {
148         long encryptionKey = 7939805759879765L; //TODO: Move this to authenticator
149
encryptionKey++;
150         try
151         {
152             StringEncrypter encrypter = new StringEncrypter( StringEncrypter.DES_ENCRYPTION_SCHEME, encryptionKey + "42" + encryptionKey );
153             String JavaDoc code = inPassword.substring(4,inPassword.length()); //take off the DES:
154
String JavaDoc decryptedString = encrypter.decrypt( code );
155             return decryptedString;
156         } catch ( Exception JavaDoc ex)
157         {
158             throw new UserManagerException(ex);
159         }
160     }
161
162     
163     /**
164      * @see com.openedit.users.User#hasPermission(String)
165      */

166     public boolean hasPermission(String JavaDoc inPermission)
167     {
168         for (Iterator JavaDoc iter = getGroups().iterator(); iter.hasNext();)
169         {
170             Group group = (Group) iter.next();
171
172             if (group.hasPermission(inPermission))
173             {
174                 return true;
175             }
176         }
177
178         //cburkey, seems like users may need custom permissions so I added this
179
String JavaDoc ok = getPropertyContainer().getString( inPermission );
180
181         if ("true".equals(ok))
182         {
183             return true;
184         }
185
186         return false;
187     }
188     
189     public Object JavaDoc getProperty( String JavaDoc inPropertyName )
190     {
191         Object JavaDoc value = getPropertyContainer().get( inPropertyName );
192         if( value == null) //this might be a new user
193
{
194             for (Iterator JavaDoc iterator = getGroups().iterator(); iterator.hasNext();)
195             {
196                 Group group = (Group) iterator.next();
197                 value = group.get(inPropertyName);
198                 if ( value != null)
199                 {
200                     return value;
201                 }
202             }
203         }
204         return value;
205     }
206
207     
208     public boolean hasProperty(String JavaDoc inName )
209     {
210         boolean has = getProperties().containsKey(inName);
211         return has;
212     }
213     public List JavaDoc listGroupPermissions()
214     {
215         List JavaDoc all = new ArrayList JavaDoc();
216         for (Iterator JavaDoc iter = getGroups().iterator(); iter.hasNext();)
217         {
218             Group group = (Group) iter.next();
219             all.addAll(group.getPermissions());
220         }
221         return all;
222     }
223     
224     /**
225      * Returns the password.
226      *
227      * @return String
228      */

229     public String JavaDoc getPassword()
230     {
231         return fieldPassword;
232     }
233
234     /**
235      * Add the given group to the list of groups to which this user belongs. If the given group is
236      * already in the list of groups, this method does nothing.
237      *
238      * @param inGroup The group to which to add this user
239      *
240      * @throws UserManagerException DOCUMENT ME!
241      */

242     public void addGroup(Group inGroup)
243     {
244         getGroups().add(inGroup);
245     }
246
247     /**
248      * Remove the given group from the list of groups to which this user belongs. If the given
249      * group is not in the list of groups, this method does nothing.
250      *
251      * @param inGroup The group from which to remove this user
252      *
253      * @throws UserManagerException DOCUMENT ME!
254      */

255     public void removeGroup(Group inGroup)
256     {
257         getGroups().remove(inGroup);
258     }
259
260     public String JavaDoc toString()
261     {
262         return getShortDescription();
263     }
264
265     public void clearGroups()
266     {
267         if ( fieldGroups != null)
268         {
269             getGroups().clear();
270         }
271     }
272
273     public boolean isInGroup(Group inGroup)
274     {
275         return getGroups().contains(inGroup);
276     }
277 }
278
Popular Tags