Source Code Cross Referenced for FileSystemUserManager.java in  » Content-Management-System » openedit » com » openedit » users » filesystem » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » openedit » com.openedit.users.filesystem 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:        Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004:        This library is free software; you can redistribute it and/or modify it under the terms
005:        of the GNU Lesser General Public License as published by the Free Software Foundation;
006:        either version 2.1 of the License, or (at your option) any later version.
007:
008:        This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009:        without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010:        See the GNU Lesser General Public License for more details.
011:         */
012:
013:        package com.openedit.users.filesystem;
014:
015:        import java.io.File;
016:        import java.io.FileWriter;
017:        import java.io.FilenameFilter;
018:        import java.util.ArrayList;
019:        import java.util.Collection;
020:        import java.util.Collections;
021:        import java.util.Date;
022:        import java.util.HashMap;
023:        import java.util.Iterator;
024:        import java.util.List;
025:        import java.util.Map;
026:        import java.util.Random;
027:        import java.util.TreeSet;
028:
029:        import org.apache.commons.logging.Log;
030:        import org.apache.commons.logging.LogFactory;
031:        import org.dom4j.Document;
032:        import org.dom4j.DocumentFactory;
033:        import org.dom4j.Element;
034:
035:        import com.openedit.OpenEditException;
036:        import com.openedit.OpenEditRuntimeException;
037:        import com.openedit.hittracker.HitTracker;
038:        import com.openedit.hittracker.ListHitTracker;
039:        import com.openedit.hittracker.SearchQuery;
040:        import com.openedit.page.manage.PageManager;
041:        import com.openedit.users.Authenticator;
042:        import com.openedit.users.DuplicateGroupException;
043:        import com.openedit.users.DuplicateUserException;
044:        import com.openedit.users.Group;
045:        import com.openedit.users.GroupComparator;
046:        import com.openedit.users.User;
047:        import com.openedit.users.UserComparator;
048:        import com.openedit.users.UserListener;
049:        import com.openedit.users.UserManager;
050:        import com.openedit.users.UserManagerException;
051:        import com.openedit.util.IntCounter;
052:        import com.openedit.util.PathUtilities;
053:        import com.openedit.util.StringEncryption;
054:        import com.openedit.util.XmlUtil;
055:
056:        /**
057:         * This user manager stores its users as files in a directory.  Note that clients of this user
058:         * manager must set the user and group directories, then call {@link #load()}.
059:         *
060:         * @author Eric and Matt
061:         *
062:         * @see #setUserDirectory(File)
063:         * @see #setGroupDirectory(File)
064:         * @see #load()
065:         */
066:        public class FileSystemUserManager implements  UserManager {
067:            protected File fieldGroupDirectory;
068:            protected File fieldUserDirectory;
069:            protected IntCounter fieldUserIdCounter;
070:            protected Map fieldGroupNameToGroupMap;
071:            protected Map fieldUserNameToUserMap;
072:
073:            protected long fieldLastEditTime;
074:            protected Authenticator fieldAuthenticator;
075:            protected StringEncryption fieldStringEncryption;
076:
077:            protected boolean fieldAllUsersLoaded = false;
078:            protected PageManager fieldPageManager;
079:            protected Map fieldUserListeners;
080:            protected PermissionsManager fieldPermissionsManager;
081:            protected XmlUtil fieldXmlUtil;
082:            private static final Log log = LogFactory
083:                    .getLog(FileSystemUserManager.class);
084:
085:            /**
086:             * Constructor for FileSystemUserManager.
087:             */
088:            public FileSystemUserManager() {
089:                super ();
090:            }
091:
092:            public FileSystemUserManager(File inUserDirectory,
093:                    File inGroupDirectory) throws UserManagerException {
094:                setUserDirectory(inUserDirectory);
095:                setGroupDirectory(inGroupDirectory);
096:            }
097:
098:            public List getPermissions() throws UserManagerException {
099:                return getPermissionsManager().getSystemPermissions();
100:            }
101:
102:            public List getSystemPermissionGroups() {
103:                return getPermissionsManager().getSystemPermissionGroups();
104:            }
105:
106:            /* (non-Javadoc)
107:             * @see com.openedit.users.filesystem.FileSystemUserLocator#getFileSystemUser(String)
108:             */
109:            public FileSystemUser getFileSystemUser(String inUserName)
110:                    throws UserManagerException {
111:                return (FileSystemUser) getUser(inUserName);
112:            }
113:
114:            /**
115:             * @see com.openedit.users.UserManager#getGroup(String)
116:             */
117:            public Group getGroup(String inGroupName) {
118:                Group group = (Group) getGroupNameToGroupMap().get(inGroupName);
119:                File find = loadGroupFile(inGroupName);
120:                if (group != null) {
121:                    if (group.getLastModified() == find.lastModified()) {
122:                        return group;
123:                    }
124:
125:                }
126:                if (!find.exists()) {
127:                    return null;
128:                }
129:
130:                if (group == null) {
131:                    group = new FileSystemGroup();
132:                }
133:                FileSystemGroup loadgroup = (FileSystemGroup) group;
134:                loadgroup.setName(inGroupName);
135:                group = loadgroup;
136:                loadgroup.setLastModified(find.lastModified());
137:                getGroupNameToGroupMap().put(inGroupName, group);
138:
139:                Element root = getXmlUtil().getXml(find, "UTF-8");
140:                Element perm = root.element("permissions");
141:                if (perm != null) {
142:                    for (Iterator iterator = perm.elementIterator("permission"); iterator
143:                            .hasNext();) {
144:                        Element type = (Element) iterator.next();
145:                        loadgroup.addPermission(type.getTextTrim());
146:                    }
147:                }
148:                MapPropertyContainer properties = new MapPropertyContainer();
149:                Element props = root.element("properties");
150:                properties.loadProperties(props);
151:                loadgroup.setPropertyContainer(properties);
152:
153:                return loadgroup;
154:
155:            }
156:
157:            /**
158:             * Sets the directory in which all the group XML files reside.
159:             *
160:             * @param groupDirectory The new group directory
161:             */
162:            public void setGroupDirectory(File groupDirectory) {
163:                fieldGroupDirectory = groupDirectory;
164:            }
165:
166:            /**
167:             * Returns the directory in which all the group XML files reside.
168:             *
169:             * @return File
170:             */
171:            public File getGroupDirectory() {
172:                return fieldGroupDirectory;
173:            }
174:
175:            /**
176:             * @see com.openedit.users.UserManager#getGroups()
177:             */
178:            public Collection getGroups() {
179:                List names = listGroupNames();
180:                List all = new ArrayList();
181:                for (Iterator iterator = names.iterator(); iterator.hasNext();) {
182:                    String name = (String) iterator.next();
183:                    all.add(getGroup(name));
184:                }
185:                return all;
186:            }
187:
188:            public Collection getGroupsSorted() {
189:
190:                TreeSet treeSet = new java.util.TreeSet(new GroupComparator());
191:
192:                treeSet.addAll(getGroups());
193:
194:                return treeSet;
195:            }
196:
197:            /**
198:             * @see com.openedit.users.UserManager#getUser(String)
199:             */
200:            public User getUser(String inUserName) {
201:                if (inUserName == null) {
202:                    return null;
203:                }
204:                User user = (User) getUserNameToUserMap().get(inUserName);
205:                if (user == null) {
206:                    user = loadUser(inUserName);
207:                }
208:                return user;
209:            }
210:
211:            /**
212:             * Sets the directory in which all the user XML files reside.
213:             *
214:             * @param userDirectory The new user directory
215:             */
216:            public void setUserDirectory(File userDirectory) {
217:                fieldUserDirectory = userDirectory;
218:            }
219:
220:            /**
221:             * Returns the directory in which all the user XML files reside.
222:             *
223:             * @return File
224:             */
225:            public File getUserDirectory() {
226:                return fieldUserDirectory;
227:            }
228:
229:            public void setPageManager(PageManager pageManager) {
230:                fieldPageManager = pageManager;
231:            }
232:
233:            public PageManager getPageManager() {
234:                return fieldPageManager;
235:            }
236:
237:            /**
238:             * @see com.openedit.users.UserManager#getUsers()
239:             */
240:            public HitTracker getUsers() {
241:                List col = listUserNames();
242:                //return getUserNameToUserMap().values();
243:                HitTracker tracker = new ListHitTracker(col);
244:                return tracker;
245:            }
246:
247:            protected List listUserNames() {
248:                List all = new ArrayList();
249:                File[] files = getUserDirectory().listFiles(
250:                        new FilenameFilter() {
251:                            public boolean accept(File dir, String name) {
252:                                return name.endsWith(".xml");
253:                            }
254:                        });
255:
256:                if (files != null) {
257:                    for (int i = 0; i < files.length; i++) {
258:                        String username = PathUtilities
259:                                .extractPageName(files[i].getName());
260:                        all.add(username);
261:                    }
262:                }
263:                return all;
264:            }
265:
266:            /**
267:             * @see com.openedit.users.UserManager#authenticate(User, String)
268:             */
269:            public boolean authenticate(User inUser, String inPassword)
270:                    throws UserManagerException {
271:
272:                boolean success = getAuthenticator().authenticate(inUser,
273:                        inPassword);
274:                if (success) {
275:                    fireUserLoggedIn(inUser);
276:                }
277:                return success;
278:            }
279:
280:            /**
281:             * @see com.openedit.users.UserManager#createGroup(String)
282:             */
283:            public Group createGroup(String inGroupName)
284:                    throws UserManagerException {
285:                if (getGroupNameToGroupMap().containsKey(inGroupName)) {
286:                    throw new DuplicateGroupException(
287:                            "A group already exists with the name \""
288:                                    + inGroupName + "\"");
289:                } else {
290:                    //File file = createGroupFile(inGroupName);
291:                    FileSystemGroup group = new FileSystemGroup();
292:                    group.setName(inGroupName);
293:                    getGroupNameToGroupMap().put(inGroupName, group);
294:                    saveGroup(group);
295:                    return group;
296:                }
297:            }
298:
299:            /**
300:             * @see com.openedit.users.UserManager#createUser(String, String)
301:             */
302:            public User createUser(String inUserName, String inPassword)
303:                    throws UserManagerException {
304:                if (inUserName == null) //when null then we are picking up the next user from the list. May be dup username
305:                {
306:                    int id = getUserIdCounter().incrementCount();
307:                    inUserName = String.valueOf(id);
308:                    User user = getUser(inUserName);
309:                    if (user != null) {
310:                        return user;
311:                    }
312:                } else if (getUser(inUserName) != null) {
313:                    throw new DuplicateUserException(
314:                            "A user already exists with the username \""
315:                                    + inUserName + "\"");
316:                }
317:
318:                if (inPassword == null || inPassword.length() <= 0) {
319:                    Random generator = new Random();
320:                    inPassword = Integer.toString((int) (100000 + generator
321:                            .nextDouble() * 899999D));
322:                }
323:
324:                createUserFile(inUserName, inPassword);
325:                User user = getUser(inUserName);
326:                fireUserAdded(user);
327:                return user;
328:            }
329:
330:            /**
331:             * May be subclassed
332:             */
333:            protected User loadUser(String inUserName)
334:                    throws UserManagerException {
335:                File userFile = loadUserFile(inUserName);
336:                if (!userFile.exists()) {
337:                    return null;
338:                }
339:
340:                FileSystemUser user = new FileSystemUser();
341:                user.setUserName(inUserName);
342:
343:                Element root = getXmlUtil().getXml(userFile, "UTF-8");
344:
345:                Element passwordElem = root.element("password");
346:                if (passwordElem != null) {
347:                    user.setPassword(passwordElem.getText());
348:                }
349:
350:                Element creationDateElem = root.element("creation-date");
351:                if (creationDateElem != null) {
352:                    long time = Long.parseLong(creationDateElem.getText());
353:                    user.setCreationDate(new Date(time));
354:                }
355:                MapPropertyContainer container = new MapPropertyContainer();
356:                container.loadProperties(root.element("properties"));
357:                user.setPropertyContainer(container);
358:
359:                for (Iterator iter = root.elementIterator("group"); iter
360:                        .hasNext();) {
361:                    Element group = (Element) iter.next();
362:                    Group g = getGroup(group.attributeValue("id"));
363:                    if (g != null) {
364:                        user.addGroup(g);
365:                    } else {
366:                        log
367:                                .error("Missing group "
368:                                        + group.attributeValue("id"));
369:                    }
370:                }
371:                getUserNameToUserMap().put(user.getUserName(), user);
372:
373:                return user;
374:            }
375:
376:            //	//Remove this method
377:            //	protected User registerUser( File inFile ) throws UserManagerException
378:            //	{
379:            //		User user = newUser( inFile );
380:            //		//We do not call getUserName since that will be slower as it has to load
381:            //		//up the XML file
382:            //		String username = inFile.getName();
383:            //		int index = username.lastIndexOf(".");
384:            //		username = username.substring(0,index);
385:            //		//add the groups in there
386:            //		
387:            //		return user;
388:            //	}
389:
390:            /**
391:             * @see com.openedit.users.UserManager#deleteGroup(Group)
392:             */
393:            public void deleteGroup(Group inGroup) throws UserManagerException {
394:                for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) {
395:                    String username = (String) iter.next();
396:                    User user = getUser(username);
397:                    user.removeGroup(inGroup);
398:                }
399:                File file = loadGroupFile(inGroup.getName());
400:                file.delete();
401:                getGroupNameToGroupMap().remove(inGroup.getName());
402:            }
403:
404:            /**
405:             * @see com.openedit.users.UserManager#deleteUser(User)
406:             */
407:            public void deleteUser(User inUser) throws UserManagerException {
408:                File file = loadUserFile(inUser.getUserName());
409:                file.delete();
410:                getUserNameToUserMap().remove(inUser.getUserName());
411:                fireUserDeleted(inUser);
412:            }
413:
414:            public void deleteGroups(List inGroups) throws UserManagerException {
415:                if (inGroups != null) {
416:                    for (Iterator iter = inGroups.iterator(); iter.hasNext();) {
417:                        Group group = (Group) iter.next();
418:                        deleteGroup(group);
419:                    }
420:                }
421:            }
422:
423:            public void deleteUsers(List inUsers) throws UserManagerException {
424:                if (inUsers != null) {
425:                    for (Iterator iter = inUsers.iterator(); iter.hasNext();) {
426:                        User user = (User) iter.next();
427:                        deleteUser(user);
428:                    }
429:                }
430:            }
431:
432:            /**
433:             * Retrieve the map from group names to {@link FileSystemGroup}s.
434:             *
435:             * @return
436:             */
437:            protected Map getGroupNameToGroupMap() {
438:                if (fieldGroupNameToGroupMap == null) {
439:                    fieldGroupNameToGroupMap = new HashMap();
440:                }
441:
442:                return fieldGroupNameToGroupMap;
443:            }
444:
445:            /**
446:             * Returns the map from usernames to {@link FileSystemUser}s.
447:             *
448:             * @return Map
449:             */
450:            protected Map getUserNameToUserMap() {
451:                if (fieldUserNameToUserMap == null) {
452:                    fieldUserNameToUserMap = new HashMap();
453:                }
454:
455:                return fieldUserNameToUserMap;
456:            }
457:
458:            protected List listGroupNames() {
459:
460:                //		fieldUserNameToUserMap = new HashMap();
461:                //
462:                //		File[] files = getUserDirectory().listFiles(
463:                //			new FilenameFilter()
464:                //			{
465:                //				public boolean accept(File dir, String name)
466:                //				{
467:                //					return name.endsWith(".xml");
468:                //				}
469:                //			});
470:                //
471:                //			if (files != null)
472:                //			{
473:                //				for (int i = 0; i < files.length; i++)
474:                //				{
475:                //					registerUser(files[i]);
476:                //				}
477:                //			}
478:                List names = new ArrayList();
479:                File[] groupfiles = getGroupDirectory().listFiles(
480:                        new FilenameFilter() {
481:                            public boolean accept(File inDir, String inName) {
482:                                return inName.endsWith(".xml");
483:                            }
484:                        });
485:
486:                if (groupfiles != null) {
487:                    for (int i = 0; i < groupfiles.length; i++) {
488:                        //Group group = new FileSystemGroup(this, groupfiles[i]);
489:                        //this configures the users
490:                        //getGroupNameToGroupMap().put(group.getName(), group);
491:                        String name = PathUtilities
492:                                .extractPageName(groupfiles[i].getName());
493:                        names.add(name);
494:                    }
495:
496:                }
497:                return names;
498:            }
499:
500:            /**
501:             * Create a file representing the given group on disk.
502:             *
503:             * @param inGroupName The name of the new group
504:             *
505:             * @return The new file
506:             *
507:             * @throws UserManagerException If the new file could not be created
508:             */
509:            protected File createGroupFile(String inGroupName)
510:                    throws UserManagerException {
511:                try {
512:                    File newFile = new File(getGroupDirectory(), inGroupName
513:                            + ".xml");
514:                    FileWriter writer = new FileWriter(newFile);
515:
516:                    try {
517:                        writer.write("<?xml version=\"1.0\"?>\n" + "<group>\n"
518:                                + "  <group-name>" + inGroupName
519:                                + "</group-name>\n" + "<permissions/>\n"
520:                                + "</group>\n");
521:                    } finally {
522:                        writer.close();
523:                    }
524:
525:                    return newFile;
526:                } catch (Exception ex) {
527:                    throw new UserManagerException(ex);
528:                }
529:            }
530:
531:            /**
532:             * Create a file representing the given user on disk.
533:             *
534:             * @param inUserName The username of the new user
535:             * @param inPassword The password of the new user
536:             *
537:             * @return The new file
538:             *
539:             * @throws UserManagerException If the new file could not be created
540:             */
541:            protected File createUserFile(String inUserName, String inPassword)
542:                    throws UserManagerException {
543:                try {
544:                    File newFile = loadUserFile(inUserName);
545:                    newFile.getParentFile().mkdirs();
546:
547:                    FileWriter writer = new FileWriter(newFile);
548:
549:                    try {
550:                        writer.write("<?xml version=\"1.0\"?>\n" + "<user>\n"
551:                                + "  <user-name>" + inUserName
552:                                + "</user-name>\n" + "  <password>"
553:                                + inPassword + "</password>\n"
554:                                + "<creation-date>" + new Date().getTime()
555:                                + "</creation-date>\n" + "</user>\n");
556:                    } finally {
557:                        writer.close();
558:                    }
559:
560:                    return newFile;
561:                } catch (Exception ex) {
562:                    throw new UserManagerException(ex);
563:                }
564:            }
565:
566:            protected File loadGroupFile(String inGroupId) {
567:                return new File(getGroupDirectory(), inGroupId + ".xml");
568:            }
569:
570:            protected File loadUserFile(String inUserName) {
571:                return new File(getUserDirectory(), inUserName + ".xml");
572:            }
573:
574:            /* (non-Javadoc)
575:             * @see com.openedit.users.UserManager#getUserByEmail(java.lang.String)
576:             */
577:            public User getUserByEmail(String emailaddress)
578:                    throws UserManagerException {
579:                //check the cache first
580:                for (Iterator iter = getUserNameToUserMap().values().iterator(); iter
581:                        .hasNext();) {
582:                    User element = (User) iter.next();
583:                    String email = element.getEmail();
584:                    if (email != null && email.equalsIgnoreCase(emailaddress)) {
585:                        return element;
586:                    }
587:                }
588:                //TODO: replace with UserSearch object that can be replaced with Lucene or JDBC
589:                for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) {
590:                    String username = (String) iter.next();
591:                    User element = getUser(username);
592:
593:                    String email = element.getEmail();
594:                    if (email != null && email.equalsIgnoreCase(emailaddress)) {
595:                        return element;
596:                    }
597:                }
598:                return null;
599:            }
600:
601:            public HitTracker findUser(String inQuery)
602:                    throws UserManagerException {
603:                return findUser(inQuery, 1000);
604:            }
605:
606:            /* Find all users who's first, last name or email start with queury
607:             * TODO: Replace with Lucene
608:             * @see com.openedit.users.UserManager#findUser(java.lang.String)
609:             */
610:            public HitTracker findUser(String inQuery, int maxNum)
611:                    throws UserManagerException {
612:                ListHitTracker tracker = new ListHitTracker();
613:                SearchQuery q = new SearchQuery();
614:                q.addMatches(inQuery);
615:                tracker.setSearchQuery(q);
616:
617:                if (inQuery == null || inQuery.equalsIgnoreCase("all")
618:                        || inQuery.length() == 0) {
619:                    for (Iterator iter = getUsers().getAllHits(); iter
620:                            .hasNext()
621:                            && tracker.getTotal() < maxNum;) {
622:                        String username = (String) iter.next();
623:                        User user = getUser(username);
624:                        tracker.addHit(user);
625:                    }
626:                    return tracker;
627:                }
628:                inQuery = inQuery.toLowerCase();
629:                for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) {
630:                    String username = (String) iter.next();
631:                    if (matches(username, inQuery)) {
632:                        User user = getUser(username);
633:                        tracker.addHit(user);
634:                    } else if (maxNum < 1001) {
635:                        //check email
636:                        User user = getUser(username);
637:                        for (Iterator iterator = user.getProperties().values()
638:                                .iterator(); iterator.hasNext();) {
639:                            String val = (String) iterator.next();
640:                            if (matches(val, inQuery)) {
641:                                tracker.addHit(user);
642:                            }
643:                        }
644:
645:                    }
646:                    if (tracker.getTotal() >= maxNum) {
647:                        break;
648:                    }
649:                }
650:                return tracker;
651:            }
652:
653:            protected boolean matches(String inText, String inQuery) {
654:                if (inText != null) {
655:                    if (PathUtilities.match(inText, inQuery)) {
656:                        return true;
657:                    }
658:                }
659:                return false;
660:            }
661:
662:            public boolean isAllUsersLoaded() {
663:                return fieldAllUsersLoaded;
664:            }
665:
666:            public void setAllUsersLoaded(boolean inAllUsersLoaded) {
667:                fieldAllUsersLoaded = inAllUsersLoaded;
668:            }
669:
670:            public void saveUser(User user) throws UserManagerException {
671:                if (user.isVirtual()) {
672:                    log
673:                            .error("Cannot save virtual users "
674:                                    + user.getUserName());
675:                    return;
676:                }
677:                DocumentFactory factory = DocumentFactory.getInstance();
678:                Document doc = factory.createDocument();
679:                Element userElem = doc.addElement("user");
680:
681:                Element userNameElem = userElem.addElement("user-name");
682:                userNameElem.setText(user.getUserName());
683:
684:                if (user.getPassword() != null) {
685:                    Element passwordElem = userElem.addElement("password");
686:                    String ps = user.getPassword();
687:                    ps = encrypt(ps);
688:                    passwordElem.setText(ps);
689:                }
690:                Element creationDateElem = userElem.addElement("creation-date");
691:                if (user.getCreationDate() != null) {
692:                    creationDateElem.setText(String.valueOf(user
693:                            .getCreationDate().getTime()));
694:                } else {
695:                    creationDateElem.setText(String.valueOf(System
696:                            .currentTimeMillis()));
697:                }
698:
699:                MapPropertyContainer map = (MapPropertyContainer) user
700:                        .getPropertyContainer();
701:                if (map != null) {
702:                    Element propertiesElem = map
703:                            .createPropertiesElement("properties");
704:                    userElem.add(propertiesElem);
705:                }
706:                if (user.getGroups() != null) {
707:                    for (Iterator iter = user.getGroups().iterator(); iter
708:                            .hasNext();) {
709:                        Group group = (Group) iter.next();
710:                        Element child = userElem.addElement("group");
711:                        child.addAttribute("id", group.getName());
712:                    }
713:                }
714:                File file = loadUserFile(user.getUserName());
715:                getXmlUtil().saveXml(doc, file);
716:                getUserNameToUserMap().put(user.getUserName(), user);
717:            }
718:
719:            public void saveGroup(Group inGroup) throws UserManagerException {
720:                Document doc = DocumentFactory.getInstance().createDocument();
721:                Element root = doc.addElement("group");
722:
723:                Element groupNameElem = root.addElement("group-name");
724:                groupNameElem.setText(inGroup.getName());
725:
726:                Element permissionsElem = root.addElement("permissions");
727:
728:                for (Iterator iter = inGroup.getPermissions().iterator(); iter
729:                        .hasNext();) {
730:                    String permission = (String) iter.next();
731:                    Element permissionElem = permissionsElem
732:                            .addElement("permission");
733:                    permissionElem.setText(permission);
734:                }
735:
736:                MapPropertyContainer map = (MapPropertyContainer) inGroup
737:                        .getPropertyContainer();
738:                if (map != null) {
739:                    Element propertiesElem = map
740:                            .createPropertiesElement("properties");
741:                    root.add(propertiesElem);
742:                }
743:                File file = loadGroupFile(inGroup.getName());
744:                getXmlUtil().saveXml(doc, file);
745:
746:                getGroupNameToGroupMap().put(inGroup.getName(), inGroup);
747:            }
748:
749:            public String encrypt(String inPassword)
750:                    throws UserManagerException {
751:                try {
752:                    if (inPassword.startsWith("DES:")) {
753:                        return inPassword;
754:                    }
755:                    //			long encryptionKey = 7939805759879765L; encryptionKey++;
756:                    //			StringEncryption encrypter = new StringEncryption( StringEncryption.DES_ENCRYPTION_SCHEME, encryptionKey + "42" + encryptionKey );
757:                    String encryptedString = getStringEncryption().encrypt(
758:                            inPassword);
759:                    return encryptedString;
760:                } catch (OpenEditException ex) {
761:                    throw new UserManagerException(ex);
762:                }
763:            }
764:
765:            public IntCounter getUserIdCounter() {
766:                if (fieldUserIdCounter == null) {
767:                    fieldUserIdCounter = new IntCounter();
768:                    fieldUserIdCounter.setCounterFile(new File(
769:                            getUserDirectory(), "users.properties"));
770:                }
771:                return fieldUserIdCounter;
772:            }
773:
774:            public HitTracker getUsersInGroup(String inGroup) {
775:                Group group = getGroup(inGroup);
776:                return getUsersInGroup(group);
777:            }
778:
779:            //TODO: Replace with smart UserHitTracker that lazy loads
780:            public HitTracker getUsersInGroup(Group inGroup) {
781:                List all = new ArrayList();
782:                for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) {
783:                    String name = (String) iter.next();
784:                    User user = getUser(name);
785:                    if (user.isInGroup(inGroup)) {
786:                        all.add(user);
787:                    }
788:                }
789:                Collections.sort(all, new UserComparator());
790:                HitTracker tracker = new ListHitTracker(all);
791:                return tracker;
792:            }
793:
794:            public Authenticator getAuthenticator() {
795:                return fieldAuthenticator;
796:            }
797:
798:            public void setAuthenticator(Authenticator inAuthenticator) {
799:                fieldAuthenticator = inAuthenticator;
800:            }
801:
802:            protected void fireUserAdded(User inUser) {
803:                for (Iterator iter = getUserListeners().keySet().iterator(); iter
804:                        .hasNext();) {
805:                    UserListener listener = (UserListener) iter.next();
806:                    listener.userAdded(inUser);
807:                }
808:            }
809:
810:            protected void fireUserDeleted(User inUser) {
811:                for (Iterator iter = getUserListeners().keySet().iterator(); iter
812:                        .hasNext();) {
813:                    UserListener listener = (UserListener) iter.next();
814:                    listener.userDeleted(inUser);
815:                }
816:            }
817:
818:            protected void fireUserLoggedOut(User inUser) {
819:                for (Iterator iter = getUserListeners().keySet().iterator(); iter
820:                        .hasNext();) {
821:                    UserListener listener = (UserListener) iter.next();
822:                    listener.userLoggedOut(inUser);
823:                }
824:            }
825:
826:            protected void fireUserLoggedIn(User inUser) {
827:                for (Iterator iter = getUserListeners().keySet().iterator(); iter
828:                        .hasNext();) {
829:                    UserListener listener = (UserListener) iter.next();
830:                    listener.userLoggedIn(inUser);
831:                }
832:            }
833:
834:            public void logout(User inUser) {
835:                fireUserLoggedOut(inUser);
836:            }
837:
838:            public Map getUserListeners() {
839:                if (fieldUserListeners == null) {
840:                    fieldUserListeners = new HashMap();
841:                }
842:                return fieldUserListeners;
843:            }
844:
845:            public void addUserListener(UserListener inListener) {
846:                getUserListeners().put(inListener, this );
847:            }
848:
849:            public PermissionsManager getPermissionsManager() {
850:                if (fieldPermissionsManager == null) {
851:                    fieldPermissionsManager = new PermissionsManager();
852:                    fieldPermissionsManager.setPageManager(getPageManager());
853:
854:                    fieldPermissionsManager.setRootDir(getGroupDirectory()
855:                            .getParentFile().getParentFile());
856:                    fieldPermissionsManager.loadPermissions();
857:                }
858:                return fieldPermissionsManager;
859:            }
860:
861:            public void setPermissionsManager(
862:                    PermissionsManager inPermissionsManager) {
863:                fieldPermissionsManager = inPermissionsManager;
864:            }
865:
866:            public XmlUtil getXmlUtil() {
867:                if (fieldXmlUtil == null) {
868:                    fieldXmlUtil = new XmlUtil();
869:                }
870:                return fieldXmlUtil;
871:            }
872:
873:            public void setXmlUtil(XmlUtil inXmlUtil) {
874:                fieldXmlUtil = inXmlUtil;
875:            }
876:
877:            public User createGuestUser(String inAccount, String inPassword,
878:                    String inGroupname) {
879:                FileSystemUser user = new FileSystemUser();
880:                user.setCreationDate(new Date());
881:                user.setUserName(inAccount);
882:                user.setPassword(inPassword);
883:                user.setVirtual(true);
884:
885:                Group group = getGroup(inGroupname);
886:                if (group == null) {
887:                    throw new OpenEditRuntimeException(
888:                            "No such auto login group " + inGroupname);
889:                }
890:                user.addGroup(group);
891:                return user;
892:            }
893:
894:            public StringEncryption getStringEncryption() {
895:                return fieldStringEncryption;
896:            }
897:
898:            public void setStringEncryption(StringEncryption inStringEncryption) {
899:                fieldStringEncryption = inStringEncryption;
900:            }
901:
902:            public String decryptPassword(User inUser) throws OpenEditException {
903:                String pw = inUser.getPassword();
904:                if (pw.startsWith("DES:")) {
905:                    pw = getStringEncryption().decrypt(pw);
906:                }
907:                return pw;
908:            }
909:
910:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.