KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
16 import java.io.FileWriter JavaDoc;
17 import java.io.FilenameFilter JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Random JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.dom4j.Document;
32 import org.dom4j.DocumentFactory;
33 import org.dom4j.Element;
34
35 import com.openedit.OpenEditException;
36 import com.openedit.OpenEditRuntimeException;
37 import com.openedit.hittracker.HitTracker;
38 import com.openedit.hittracker.ListHitTracker;
39 import com.openedit.page.manage.PageManager;
40 import com.openedit.users.Authenticator;
41 import com.openedit.users.DuplicateGroupException;
42 import com.openedit.users.DuplicateUserException;
43 import com.openedit.users.Group;
44 import com.openedit.users.GroupComparator;
45 import com.openedit.users.StringEncrypter;
46 import com.openedit.users.User;
47 import com.openedit.users.UserComparator;
48 import com.openedit.users.UserListener;
49 import com.openedit.users.UserManager;
50 import com.openedit.users.UserManagerException;
51 import com.openedit.util.IntCounter;
52 import com.openedit.util.PathUtilities;
53 import com.openedit.util.XmlUtil;
54
55
56 /**
57  * This user manager stores its users as files in a directory. Note that clients of this user
58  * manager must set the user and group directories, then call {@link #load()}.
59  *
60  * @author Eric and Matt
61  *
62  * @see #setUserDirectory(File)
63  * @see #setGroupDirectory(File)
64  * @see #load()
65  */

66 public class FileSystemUserManager implements UserManager
67 {
68     protected File JavaDoc fieldGroupDirectory;
69     protected File JavaDoc fieldUserDirectory;
70     protected IntCounter fieldUserIdCounter;
71     protected Map JavaDoc fieldGroupNameToGroupMap;
72     protected Map JavaDoc fieldUserNameToUserMap;
73
74     protected long fieldLastEditTime;
75     protected Authenticator fieldAuthenticator;
76     
77     protected boolean fieldAllUsersLoaded = false;
78     protected PageManager fieldPageManager;
79     protected Map JavaDoc fieldUserListeners;
80     protected PermissionsManager fieldPermissionsManager;
81     protected XmlUtil fieldXmlUtil;
82     private static final Log log = LogFactory.getLog(FileSystemUserManager.class);
83     
84     /**
85      * Constructor for FileSystemUserManager.
86      */

87     public FileSystemUserManager()
88     {
89         super();
90     }
91     
92     public FileSystemUserManager( File JavaDoc inUserDirectory, File JavaDoc inGroupDirectory ) throws UserManagerException
93     {
94         setUserDirectory( inUserDirectory );
95         setGroupDirectory( inGroupDirectory );
96     }
97     
98     public List JavaDoc getPermissions() throws UserManagerException
99     {
100         return getPermissionsManager().getSystemPermissions();
101     }
102     
103     
104     public List JavaDoc getSystemPermissionGroups()
105     {
106         return getPermissionsManager().getSystemPermissionGroups();
107     }
108
109     /* (non-Javadoc)
110      * @see com.openedit.users.filesystem.FileSystemUserLocator#getFileSystemUser(String)
111      */

112     public FileSystemUser getFileSystemUser(String JavaDoc inUserName)
113         throws UserManagerException
114     {
115         return (FileSystemUser) getUser(inUserName);
116     }
117
118     /**
119      * @see com.openedit.users.UserManager#getGroup(String)
120      */

121     public Group getGroup(String JavaDoc inGroupName)
122     {
123         Group group = (Group) getGroupNameToGroupMap().get(inGroupName);
124         File JavaDoc find = loadGroupFile(inGroupName);
125         if ( group != null)
126         {
127             if( group.getLastModified() == find.lastModified() )
128             {
129                 return group;
130             }
131                     
132         }
133         if( !find.exists())
134         {
135             return null;
136         }
137         
138         if( group == null)
139         {
140             group = new FileSystemGroup();
141         }
142         FileSystemGroup loadgroup = (FileSystemGroup)group;
143         loadgroup.setName(inGroupName);
144         group = loadgroup;
145         loadgroup.setLastModified(find.lastModified());
146         getGroupNameToGroupMap().put(inGroupName, group);
147
148         Element root = getXmlUtil().getXml(find, "UTF-8");
149         Element perm = root.element("permissions");
150         if( perm != null)
151         {
152             for (Iterator JavaDoc iterator = perm.elementIterator("permission"); iterator.hasNext();)
153             {
154                 Element type = (Element) iterator.next();
155                 loadgroup.addPermission(type.getTextTrim());
156             }
157         }
158         MapPropertyContainer properties = new MapPropertyContainer();
159         Element props = root.element("properties");
160         properties.loadProperties(props);
161         loadgroup.setPropertyContainer(properties);
162
163         return loadgroup;
164         
165     }
166
167     /**
168      * Sets the directory in which all the group XML files reside.
169      *
170      * @param groupDirectory The new group directory
171      */

172     public void setGroupDirectory(File JavaDoc groupDirectory)
173     {
174         fieldGroupDirectory = groupDirectory;
175     }
176
177     /**
178      * Returns the directory in which all the group XML files reside.
179      *
180      * @return File
181      */

182     public File JavaDoc getGroupDirectory()
183     {
184         return fieldGroupDirectory;
185     }
186
187     /**
188      * @see com.openedit.users.UserManager#getGroups()
189      */

190     public Collection JavaDoc getGroups()
191     {
192         List JavaDoc names = listGroupNames();
193         List JavaDoc all = new ArrayList JavaDoc();
194         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();)
195         {
196             String JavaDoc name = (String JavaDoc) iterator.next();
197             all.add( getGroup( name));
198         }
199         return all;
200     }
201     public Collection JavaDoc getGroupsSorted()
202     {
203                 
204         TreeSet JavaDoc treeSet = new java.util.TreeSet JavaDoc( new GroupComparator() );
205         
206         treeSet.addAll( getGroups() );
207
208         return treeSet;
209     }
210
211     /**
212      * @see com.openedit.users.UserManager#getUser(String)
213      */

214     public User getUser(String JavaDoc inUserName)
215     {
216         User user = (User) getUserNameToUserMap().get(inUserName);
217         if ( user == null )
218         {
219             user = loadUser( inUserName );
220         }
221         return user;
222     }
223
224     /**
225      * Sets the directory in which all the user XML files reside.
226      *
227      * @param userDirectory The new user directory
228      */

229     public void setUserDirectory(File JavaDoc userDirectory)
230     {
231         fieldUserDirectory = userDirectory;
232     }
233
234     /**
235      * Returns the directory in which all the user XML files reside.
236      *
237      * @return File
238      */

239     public File JavaDoc getUserDirectory()
240     {
241         return fieldUserDirectory;
242     }
243     
244     public void setPageManager(PageManager pageManager)
245     {
246         fieldPageManager = pageManager;
247     }
248     
249     public PageManager getPageManager()
250     {
251         return fieldPageManager;
252     }
253
254     /**
255      * @see com.openedit.users.UserManager#getUsers()
256      */

257     public HitTracker getUsers()
258     {
259         List JavaDoc col = listUserNames();
260         //return getUserNameToUserMap().values();
261
HitTracker tracker = new ListHitTracker(col);
262         return tracker;
263     }
264
265     protected List JavaDoc listUserNames()
266     {
267         List JavaDoc all = new ArrayList JavaDoc();
268         File JavaDoc[] files = getUserDirectory().listFiles(
269             new FilenameFilter JavaDoc()
270             {
271                 public boolean accept(File JavaDoc dir, String JavaDoc name)
272                 {
273                     return name.endsWith(".xml");
274                 }
275             });
276
277             if (files != null)
278             {
279                 for (int i = 0; i < files.length; i++)
280                 {
281                     String JavaDoc username = PathUtilities.extractPageName(files[i].getName() );
282                     all.add(username);
283                 }
284             }
285             return all;
286     }
287
288     /**
289      * @see com.openedit.users.UserManager#authenticate(User, String)
290      */

291     public boolean authenticate(User inUser, String JavaDoc inPassword)
292         throws UserManagerException
293     {
294         
295         boolean success = getAuthenticator().authenticate(inUser, inPassword);
296         if (success)
297         {
298             fireUserLoggedIn(inUser);
299         }
300         return success;
301     }
302
303     /**
304      * @see com.openedit.users.UserManager#createGroup(String)
305      */

306     public Group createGroup(String JavaDoc inGroupName) throws UserManagerException
307     {
308         if (getGroupNameToGroupMap().containsKey(inGroupName))
309         {
310             throw new DuplicateGroupException(
311                 "A group already exists with the name \"" + inGroupName + "\"");
312         }
313         else
314         {
315             //File file = createGroupFile(inGroupName);
316
FileSystemGroup group = new FileSystemGroup();
317             group.setName(inGroupName);
318             getGroupNameToGroupMap().put(inGroupName, group);
319             saveGroup(group);
320             return group;
321         }
322     }
323
324     
325     /**
326      * @see com.openedit.users.UserManager#createUser(String, String)
327      */

328     public User createUser(String JavaDoc inUserName, String JavaDoc inPassword)
329         throws UserManagerException
330     {
331         if ( inUserName == null) //when null then we are picking up the next user from the list. May be dup username
332
{
333             int id = getUserIdCounter().incrementCount();
334             inUserName = String.valueOf(id);
335             User user = getUser(inUserName);
336             if ( user != null)
337             {
338                 return user;
339             }
340         }
341         else if (getUserNameToUserMap().containsKey(inUserName))
342         {
343             throw new DuplicateUserException(
344                 "A user already exists with the username \"" + inUserName + "\"");
345         }
346         
347         if (inPassword == null || inPassword.length() <= 0)
348         {
349             Random JavaDoc generator = new Random JavaDoc();
350             inPassword = Integer.toString((int)(100000 + generator.nextDouble() * 899999D));
351         }
352         
353         createUserFile(inUserName, inPassword);
354         User user = getUser(inUserName );
355         fireUserAdded(user);
356         return user;
357     }
358     
359     /**
360      * May be subclassed
361      */

362     protected User loadUser( String JavaDoc inUserName) throws UserManagerException
363     {
364         File JavaDoc userFile = loadUserFile(inUserName);
365         if ( !userFile.exists() )
366         {
367             return null;
368         }
369
370         FileSystemUser user = new FileSystemUser();
371         user.setUserName(inUserName);
372         
373         Element root = getXmlUtil().getXml(userFile, "UTF-8");
374         
375         Element passwordElem = root.element("password");
376         if( passwordElem != null)
377         {
378             user.setPassword(passwordElem.getText());
379         }
380
381         Element creationDateElem = root.element("creation-date");
382         if( creationDateElem != null)
383         {
384             long time = Long.parseLong(creationDateElem.getText() );
385             user.setCreationDate(new Date JavaDoc(time));
386         }
387         MapPropertyContainer container = new MapPropertyContainer();
388         container.loadProperties(root.element("properties"));
389         user.setPropertyContainer(container);
390
391         for (Iterator JavaDoc iter = root.elementIterator("group"); iter.hasNext();)
392         {
393             Element group = (Element) iter.next();
394             Group g = getGroup(group.attributeValue("id"));
395             if( g != null)
396             {
397                 user.addGroup(g);
398             }
399             else
400             {
401                 log.error("Missing group " + group.attributeValue("id") );
402             }
403         }
404         getUserNameToUserMap().put(user.getUserName(), user);
405
406         return user;
407     }
408
409 // //Remove this method
410
// protected User registerUser( File inFile ) throws UserManagerException
411
// {
412
// User user = newUser( inFile );
413
// //We do not call getUserName since that will be slower as it has to load
414
// //up the XML file
415
// String username = inFile.getName();
416
// int index = username.lastIndexOf(".");
417
// username = username.substring(0,index);
418
// //add the groups in there
419
//
420
// return user;
421
// }
422

423     /**
424      * @see com.openedit.users.UserManager#deleteGroup(Group)
425      */

426     public void deleteGroup(Group inGroup) throws UserManagerException
427     {
428         for (Iterator JavaDoc iter = getUsers().getAllHits(); iter.hasNext();)
429         {
430             String JavaDoc username = (String JavaDoc) iter.next();
431             User user = getUser(username);
432             user.removeGroup(inGroup);
433         }
434         File JavaDoc file = loadGroupFile(inGroup.getName());
435         file.delete();
436         getGroupNameToGroupMap().remove(inGroup.getName());
437     }
438
439     /**
440      * @see com.openedit.users.UserManager#deleteUser(User)
441      */

442     public void deleteUser(User inUser) throws UserManagerException
443     {
444         File JavaDoc file = loadUserFile(inUser.getUserName());
445         file.delete();
446         getUserNameToUserMap().remove(inUser.getUserName());
447         fireUserDeleted(inUser);
448     }
449
450     public void deleteGroups(List JavaDoc inGroups) throws UserManagerException
451     {
452         if (inGroups != null)
453         {
454             for (Iterator JavaDoc iter = inGroups.iterator(); iter.hasNext();)
455             {
456                 Group group = (Group) iter.next();
457                 deleteGroup(group);
458             }
459         }
460     }
461
462     public void deleteUsers(List JavaDoc inUsers) throws UserManagerException
463     {
464         if (inUsers != null)
465         {
466             for (Iterator JavaDoc iter = inUsers.iterator(); iter.hasNext();)
467             {
468                 User user = (User) iter.next();
469                 deleteUser(user);
470             }
471         }
472     }
473
474     /**
475      * Retrieve the map from group names to {@link FileSystemGroup}s.
476      *
477      * @return
478      */

479     protected Map JavaDoc getGroupNameToGroupMap()
480     {
481         if (fieldGroupNameToGroupMap == null)
482         {
483             fieldGroupNameToGroupMap = new HashMap JavaDoc();
484         }
485
486         return fieldGroupNameToGroupMap;
487     }
488
489     /**
490      * Returns the map from usernames to {@link FileSystemUser}s.
491      *
492      * @return Map
493      */

494     protected Map JavaDoc getUserNameToUserMap()
495     {
496         if (fieldUserNameToUserMap == null)
497         {
498             fieldUserNameToUserMap = new HashMap JavaDoc();
499         }
500
501         return fieldUserNameToUserMap;
502     }
503
504     protected List JavaDoc listGroupNames()
505     {
506
507 // fieldUserNameToUserMap = new HashMap();
508
//
509
// File[] files = getUserDirectory().listFiles(
510
// new FilenameFilter()
511
// {
512
// public boolean accept(File dir, String name)
513
// {
514
// return name.endsWith(".xml");
515
// }
516
// });
517
//
518
// if (files != null)
519
// {
520
// for (int i = 0; i < files.length; i++)
521
// {
522
// registerUser(files[i]);
523
// }
524
// }
525
List JavaDoc names = new ArrayList JavaDoc();
526             File JavaDoc[] groupfiles = getGroupDirectory().listFiles(
527                 new FilenameFilter JavaDoc()
528                 {
529                     public boolean accept(File JavaDoc inDir, String JavaDoc inName)
530                     {
531                         return inName.endsWith(".xml");
532                     }
533                 });
534
535             if (groupfiles != null)
536             {
537                 for (int i = 0; i < groupfiles.length; i++)
538                 {
539                     //Group group = new FileSystemGroup(this, groupfiles[i]);
540
//this configures the users
541
//getGroupNameToGroupMap().put(group.getName(), group);
542
String JavaDoc name = PathUtilities.extractPageName(groupfiles[i].getName());
543                     names.add(name);
544                 }
545
546             }
547             return names;
548     }
549     /**
550      * Create a file representing the given group on disk.
551      *
552      * @param inGroupName The name of the new group
553      *
554      * @return The new file
555      *
556      * @throws UserManagerException If the new file could not be created
557      */

558     protected File JavaDoc createGroupFile(String JavaDoc inGroupName)
559         throws UserManagerException
560     {
561         try
562         {
563             File JavaDoc newFile = new File JavaDoc(getGroupDirectory(), inGroupName + ".xml");
564             FileWriter JavaDoc writer = new FileWriter JavaDoc(newFile);
565
566             try
567             {
568                 writer.write(
569                     "<?xml version=\"1.0\"?>\n" + "<group>\n" + " <group-name>" + inGroupName +
570                     "</group-name>\n" + "<permissions/>\n" + "</group>\n");
571             }
572             finally
573             {
574                 writer.close();
575             }
576
577             return newFile;
578         }
579         catch (Exception JavaDoc ex)
580         {
581             throw new UserManagerException(ex);
582         }
583     }
584
585     /**
586      * Create a file representing the given user on disk.
587      *
588      * @param inUserName The username of the new user
589      * @param inPassword The password of the new user
590      *
591      * @return The new file
592      *
593      * @throws UserManagerException If the new file could not be created
594      */

595     protected File JavaDoc createUserFile(String JavaDoc inUserName, String JavaDoc inPassword)
596         throws UserManagerException
597     {
598         try
599         {
600             File JavaDoc newFile = loadUserFile( inUserName);
601             newFile.getParentFile().mkdirs();
602
603             FileWriter JavaDoc writer = new FileWriter JavaDoc(newFile);
604
605             try
606             {
607                 writer.write(
608                     "<?xml version=\"1.0\"?>\n" + "<user>\n" + " <user-name>" + inUserName +
609                     "</user-name>\n" + " <password>" + inPassword + "</password>\n" +
610                     "<creation-date>" + new Date JavaDoc().getTime() + "</creation-date>\n" +
611                     "</user>\n");
612             }
613             finally
614             {
615                 writer.close();
616             }
617
618             return newFile;
619         }
620         catch (Exception JavaDoc ex)
621         {
622             throw new UserManagerException(ex);
623         }
624     }
625     protected File JavaDoc loadGroupFile(String JavaDoc inGroupId)
626     {
627         return new File JavaDoc(getGroupDirectory(), inGroupId + ".xml");
628     }
629     
630     protected File JavaDoc loadUserFile(String JavaDoc inUserName)
631     {
632         return new File JavaDoc(getUserDirectory(), inUserName + ".xml");
633     }
634
635     /* (non-Javadoc)
636      * @see com.openedit.users.UserManager#getUserByEmail(java.lang.String)
637      */

638     public User getUserByEmail(String JavaDoc emailaddress) throws UserManagerException
639     {
640         //check the cache first
641
for (Iterator JavaDoc iter = getUserNameToUserMap().values().iterator(); iter.hasNext();)
642         {
643             User element = (User) iter.next();
644             String JavaDoc email = element.getEmail();
645             if ( email != null && email.equalsIgnoreCase(emailaddress))
646             {
647                 return element;
648             }
649         }
650         //TODO: replace with UserSearch object that can be replaced with Lucene or JDBC
651
for (Iterator JavaDoc iter = getUsers().getAllHits(); iter.hasNext();)
652         {
653             String JavaDoc username = (String JavaDoc) iter.next();
654             User element = getUser(username);
655
656             String JavaDoc email = element.getEmail();
657             if ( email != null && email.equalsIgnoreCase(emailaddress))
658             {
659                 return element;
660             }
661         }
662         return null;
663     }
664     public HitTracker findUser(String JavaDoc inQuery) throws UserManagerException
665     {
666         return findUser(inQuery, 1000);
667     }
668     /* Find all users who's first, last name or email start with queury
669      * TODO: Replace with Lucene
670      * @see com.openedit.users.UserManager#findUser(java.lang.String)
671      */

672     public HitTracker findUser(String JavaDoc inQuery, int maxNum) throws UserManagerException
673     {
674         ListHitTracker tracker = new ListHitTracker();
675         
676           if ( inQuery == null || inQuery.equalsIgnoreCase("all") || inQuery.length() == 0)
677           {
678               for (Iterator JavaDoc iter = getUsers().getAllHits(); iter.hasNext() && tracker.getTotal() < maxNum;)
679               {
680                 String JavaDoc username = (String JavaDoc) iter.next();
681                 User user = getUser(username);
682                 tracker.addHit(user);
683               }
684               return tracker;
685           }
686           for (Iterator JavaDoc iter = getUsers().getAllHits(); iter.hasNext();)
687           {
688             String JavaDoc username = (String JavaDoc) iter.next();
689             if( matches(username,inQuery) )
690             {
691                 User user = getUser(username);
692                 tracker.addHit(user);
693             }
694             if ( tracker.getTotal() >= maxNum)
695             {
696                 break;
697             }
698           }
699         return tracker;
700     }
701     protected boolean matches(String JavaDoc inText, String JavaDoc inQuery)
702     {
703         if ( inText != null)
704         {
705             if (inText.toLowerCase().startsWith(inQuery) )
706             {
707                 return true;
708             }
709         }
710         return false;
711     }
712     public boolean isAllUsersLoaded()
713     {
714         return fieldAllUsersLoaded;
715     }
716     public void setAllUsersLoaded(boolean inAllUsersLoaded)
717     {
718         fieldAllUsersLoaded = inAllUsersLoaded;
719     }
720
721     public void saveUser(User user) throws UserManagerException
722     {
723         DocumentFactory factory = DocumentFactory.getInstance();
724         Document doc = factory.createDocument();
725         Element userElem = doc.addElement("user");
726
727         Element userNameElem = userElem.addElement("user-name");
728         userNameElem.setText(user.getUserName());
729
730         if( user.getPassword() != null)
731         {
732             Element passwordElem = userElem.addElement("password");
733             String JavaDoc ps = user.getPassword();
734             ps = encrypt(ps);
735             passwordElem.setText(ps);
736         }
737         Element creationDateElem = userElem.addElement("creation-date");
738         if( user.getCreationDate() != null)
739         {
740             creationDateElem.setText(String.valueOf(user.getCreationDate().getTime()));
741         }
742         else
743         {
744             creationDateElem.setText(String.valueOf(System.currentTimeMillis()));
745         }
746         
747         MapPropertyContainer map = (MapPropertyContainer)user.getPropertyContainer();
748         if( map != null)
749         {
750             Element propertiesElem = map.createPropertiesElement("properties");
751             userElem.add(propertiesElem);
752         }
753         if( user.getGroups() != null)
754         {
755             for (Iterator JavaDoc iter = user.getGroups().iterator(); iter.hasNext();)
756             {
757                 Group group = (Group) iter.next();
758                 Element child = userElem.addElement("group");
759                 child.addAttribute("id",group.getName());
760             }
761         }
762         File JavaDoc file = loadUserFile(user.getUserName());
763         getXmlUtil().saveXml(doc, file);
764         getUserNameToUserMap().put(user.getUserName(), user);
765     }
766     
767     public void saveGroup(Group inGroup) throws UserManagerException
768     {
769         Document doc = DocumentFactory.getInstance().createDocument();
770         Element root = doc.addElement("group");
771         
772         Element groupNameElem = root.addElement("group-name");
773         groupNameElem.setText(inGroup.getName());
774
775         Element permissionsElem = root.addElement("permissions");
776
777         for (Iterator JavaDoc iter = inGroup.getPermissions().iterator(); iter.hasNext();)
778         {
779             String JavaDoc permission = (String JavaDoc) iter.next();
780             Element permissionElem = permissionsElem.addElement("permission");
781             permissionElem.setText(permission);
782         }
783
784         MapPropertyContainer map = (MapPropertyContainer)inGroup.getPropertyContainer();
785         if( map != null)
786         {
787             Element propertiesElem = map.createPropertiesElement("properties");
788             root.add(propertiesElem);
789         }
790         File JavaDoc file = loadGroupFile(inGroup.getName());
791         getXmlUtil().saveXml(doc, file);
792
793         getGroupNameToGroupMap().put( inGroup.getName(), inGroup);
794     }
795
796     public String JavaDoc encrypt(String JavaDoc inPassword) throws UserManagerException
797     {
798         try
799         {
800             if( inPassword.startsWith("DES:"))
801             {
802                 return inPassword;
803             }
804             long encryptionKey = 7939805759879765L; encryptionKey++;
805             StringEncrypter encrypter = new StringEncrypter( StringEncrypter.DES_ENCRYPTION_SCHEME, encryptionKey + "42" + encryptionKey );
806             String JavaDoc encryptedString = encrypter.encrypt( inPassword );
807             return "DES:" + encryptedString;
808         } catch ( OpenEditException ex)
809         {
810             throw new UserManagerException(ex);
811         }
812     }
813
814     
815     public IntCounter getUserIdCounter()
816     {
817         if ( fieldUserIdCounter == null)
818         {
819             fieldUserIdCounter = new IntCounter();
820             fieldUserIdCounter.setCounterFile( new File JavaDoc( getUserDirectory(), "users.properties" ) );
821         }
822         return fieldUserIdCounter;
823     }
824     public HitTracker getUsersInGroup(String JavaDoc inGroup)
825     {
826         Group group = getGroup(inGroup);
827         return getUsersInGroup(group);
828     }
829     
830     //TODO: Replace with smart UserHitTracker that lazy loads
831
public HitTracker getUsersInGroup(Group inGroup)
832     {
833         List JavaDoc all = new ArrayList JavaDoc();
834         for (Iterator JavaDoc iter = getUsers().getAllHits(); iter.hasNext();)
835         {
836             String JavaDoc name = (String JavaDoc)iter.next();
837             User user = getUser(name);
838             if ( user.isInGroup( inGroup ))
839             {
840                 all.add(user);
841             }
842         }
843         Collections.sort(all, new UserComparator() );
844         HitTracker tracker = new ListHitTracker(all);
845         return tracker;
846     }
847
848     public Authenticator getAuthenticator()
849     {
850         return fieldAuthenticator;
851     }
852
853     public void setAuthenticator(Authenticator inAuthenticator)
854     {
855         fieldAuthenticator = inAuthenticator;
856     }
857
858     protected void fireUserAdded( User inUser)
859     {
860         for ( Iterator JavaDoc iter = getUserListeners().keySet().iterator(); iter.hasNext(); )
861         {
862             UserListener listener = (UserListener) iter.next();
863             listener.userAdded( inUser );
864         }
865     }
866     
867     protected void fireUserDeleted( User inUser)
868     {
869         for ( Iterator JavaDoc iter = getUserListeners().keySet().iterator(); iter.hasNext(); )
870         {
871             UserListener listener = (UserListener) iter.next();
872             listener.userDeleted( inUser );
873         }
874     }
875     protected void fireUserLoggedOut( User inUser)
876     {
877         for ( Iterator JavaDoc iter = getUserListeners().keySet().iterator(); iter.hasNext(); )
878         {
879             UserListener listener = (UserListener) iter.next();
880             listener.userLoggedOut( inUser );
881         }
882     }
883     protected void fireUserLoggedIn( User inUser)
884     {
885         for ( Iterator JavaDoc iter = getUserListeners().keySet().iterator(); iter.hasNext(); )
886         {
887             UserListener listener = (UserListener) iter.next();
888             listener.userLoggedIn( inUser );
889         }
890     }
891     
892     public void logout(User inUser )
893     {
894         fireUserLoggedOut(inUser);
895     }
896
897     public Map JavaDoc getUserListeners()
898     {
899         if (fieldUserListeners == null)
900         {
901             fieldUserListeners = new HashMap JavaDoc();
902         }
903         return fieldUserListeners;
904     }
905
906     public void addUserListener( UserListener inListener )
907     {
908         getUserListeners().put( inListener, this );
909     }
910
911     public PermissionsManager getPermissionsManager()
912     {
913         if (fieldPermissionsManager == null)
914         {
915             fieldPermissionsManager = new PermissionsManager();
916             fieldPermissionsManager.setPageManager(getPageManager());
917             
918             fieldPermissionsManager.setRootDir(getGroupDirectory().getParentFile().getParentFile());
919             fieldPermissionsManager.loadPermissions();
920         }
921         return fieldPermissionsManager;
922     }
923
924
925     public void setPermissionsManager(PermissionsManager inPermissionsManager)
926     {
927         fieldPermissionsManager = inPermissionsManager;
928     }
929
930     public XmlUtil getXmlUtil()
931     {
932         if( fieldXmlUtil == null)
933         {
934             fieldXmlUtil = new XmlUtil();
935         }
936         return fieldXmlUtil;
937     }
938
939     public void setXmlUtil(XmlUtil inXmlUtil)
940     {
941         fieldXmlUtil = inXmlUtil;
942     }
943
944     public User createGuestUser(String JavaDoc inAccount, String JavaDoc inPassword, String JavaDoc inGroupname)
945     {
946         FileSystemUser user = new FileSystemUser();
947         user.setCreationDate(new Date JavaDoc());
948         user.setUserName(inAccount);
949         user.setPassword(inPassword);
950         Group group = getGroup(inGroupname);
951         if( group == null)
952         {
953             throw new OpenEditRuntimeException("No such auto login group " + inGroupname );
954         }
955         user.addGroup(group);
956         return user;
957     }
958 }
959
Popular Tags