1 12 13 package com.openedit.users.filesystem; 14 15 import java.io.File ; 16 import java.io.FileWriter ; 17 import java.io.FilenameFilter ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Random ; 27 import java.util.TreeSet ; 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 66 public class FileSystemUserManager implements UserManager 67 { 68 protected File fieldGroupDirectory; 69 protected File fieldUserDirectory; 70 protected IntCounter fieldUserIdCounter; 71 protected Map fieldGroupNameToGroupMap; 72 protected Map fieldUserNameToUserMap; 73 74 protected long fieldLastEditTime; 75 protected Authenticator fieldAuthenticator; 76 77 protected boolean fieldAllUsersLoaded = false; 78 protected PageManager fieldPageManager; 79 protected Map fieldUserListeners; 80 protected PermissionsManager fieldPermissionsManager; 81 protected XmlUtil fieldXmlUtil; 82 private static final Log log = LogFactory.getLog(FileSystemUserManager.class); 83 84 87 public FileSystemUserManager() 88 { 89 super(); 90 } 91 92 public FileSystemUserManager( File inUserDirectory, File inGroupDirectory ) throws UserManagerException 93 { 94 setUserDirectory( inUserDirectory ); 95 setGroupDirectory( inGroupDirectory ); 96 } 97 98 public List getPermissions() throws UserManagerException 99 { 100 return getPermissionsManager().getSystemPermissions(); 101 } 102 103 104 public List getSystemPermissionGroups() 105 { 106 return getPermissionsManager().getSystemPermissionGroups(); 107 } 108 109 112 public FileSystemUser getFileSystemUser(String inUserName) 113 throws UserManagerException 114 { 115 return (FileSystemUser) getUser(inUserName); 116 } 117 118 121 public Group getGroup(String inGroupName) 122 { 123 Group group = (Group) getGroupNameToGroupMap().get(inGroupName); 124 File 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 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 172 public void setGroupDirectory(File groupDirectory) 173 { 174 fieldGroupDirectory = groupDirectory; 175 } 176 177 182 public File getGroupDirectory() 183 { 184 return fieldGroupDirectory; 185 } 186 187 190 public Collection getGroups() 191 { 192 List names = listGroupNames(); 193 List all = new ArrayList (); 194 for (Iterator iterator = names.iterator(); iterator.hasNext();) 195 { 196 String name = (String ) iterator.next(); 197 all.add( getGroup( name)); 198 } 199 return all; 200 } 201 public Collection getGroupsSorted() 202 { 203 204 TreeSet treeSet = new java.util.TreeSet ( new GroupComparator() ); 205 206 treeSet.addAll( getGroups() ); 207 208 return treeSet; 209 } 210 211 214 public User getUser(String 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 229 public void setUserDirectory(File userDirectory) 230 { 231 fieldUserDirectory = userDirectory; 232 } 233 234 239 public File 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 257 public HitTracker getUsers() 258 { 259 List col = listUserNames(); 260 HitTracker tracker = new ListHitTracker(col); 262 return tracker; 263 } 264 265 protected List listUserNames() 266 { 267 List all = new ArrayList (); 268 File [] files = getUserDirectory().listFiles( 269 new FilenameFilter () 270 { 271 public boolean accept(File dir, String 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 username = PathUtilities.extractPageName(files[i].getName() ); 282 all.add(username); 283 } 284 } 285 return all; 286 } 287 288 291 public boolean authenticate(User inUser, String 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 306 public Group createGroup(String 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 FileSystemGroup group = new FileSystemGroup(); 317 group.setName(inGroupName); 318 getGroupNameToGroupMap().put(inGroupName, group); 319 saveGroup(group); 320 return group; 321 } 322 } 323 324 325 328 public User createUser(String inUserName, String inPassword) 329 throws UserManagerException 330 { 331 if ( inUserName == null) { 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 generator = new Random (); 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 362 protected User loadUser( String inUserName) throws UserManagerException 363 { 364 File 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 (time)); 386 } 387 MapPropertyContainer container = new MapPropertyContainer(); 388 container.loadProperties(root.element("properties")); 389 user.setPropertyContainer(container); 390 391 for (Iterator 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 423 426 public void deleteGroup(Group inGroup) throws UserManagerException 427 { 428 for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) 429 { 430 String username = (String ) iter.next(); 431 User user = getUser(username); 432 user.removeGroup(inGroup); 433 } 434 File file = loadGroupFile(inGroup.getName()); 435 file.delete(); 436 getGroupNameToGroupMap().remove(inGroup.getName()); 437 } 438 439 442 public void deleteUser(User inUser) throws UserManagerException 443 { 444 File file = loadUserFile(inUser.getUserName()); 445 file.delete(); 446 getUserNameToUserMap().remove(inUser.getUserName()); 447 fireUserDeleted(inUser); 448 } 449 450 public void deleteGroups(List inGroups) throws UserManagerException 451 { 452 if (inGroups != null) 453 { 454 for (Iterator iter = inGroups.iterator(); iter.hasNext();) 455 { 456 Group group = (Group) iter.next(); 457 deleteGroup(group); 458 } 459 } 460 } 461 462 public void deleteUsers(List inUsers) throws UserManagerException 463 { 464 if (inUsers != null) 465 { 466 for (Iterator iter = inUsers.iterator(); iter.hasNext();) 467 { 468 User user = (User) iter.next(); 469 deleteUser(user); 470 } 471 } 472 } 473 474 479 protected Map getGroupNameToGroupMap() 480 { 481 if (fieldGroupNameToGroupMap == null) 482 { 483 fieldGroupNameToGroupMap = new HashMap (); 484 } 485 486 return fieldGroupNameToGroupMap; 487 } 488 489 494 protected Map getUserNameToUserMap() 495 { 496 if (fieldUserNameToUserMap == null) 497 { 498 fieldUserNameToUserMap = new HashMap (); 499 } 500 501 return fieldUserNameToUserMap; 502 } 503 504 protected List listGroupNames() 505 { 506 507 List names = new ArrayList (); 526 File [] groupfiles = getGroupDirectory().listFiles( 527 new FilenameFilter () 528 { 529 public boolean accept(File inDir, String 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 String name = PathUtilities.extractPageName(groupfiles[i].getName()); 543 names.add(name); 544 } 545 546 } 547 return names; 548 } 549 558 protected File createGroupFile(String inGroupName) 559 throws UserManagerException 560 { 561 try 562 { 563 File newFile = new File (getGroupDirectory(), inGroupName + ".xml"); 564 FileWriter writer = new FileWriter (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 ex) 580 { 581 throw new UserManagerException(ex); 582 } 583 } 584 585 595 protected File createUserFile(String inUserName, String inPassword) 596 throws UserManagerException 597 { 598 try 599 { 600 File newFile = loadUserFile( inUserName); 601 newFile.getParentFile().mkdirs(); 602 603 FileWriter writer = new FileWriter (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 ().getTime() + "</creation-date>\n" + 611 "</user>\n"); 612 } 613 finally 614 { 615 writer.close(); 616 } 617 618 return newFile; 619 } 620 catch (Exception ex) 621 { 622 throw new UserManagerException(ex); 623 } 624 } 625 protected File loadGroupFile(String inGroupId) 626 { 627 return new File (getGroupDirectory(), inGroupId + ".xml"); 628 } 629 630 protected File loadUserFile(String inUserName) 631 { 632 return new File (getUserDirectory(), inUserName + ".xml"); 633 } 634 635 638 public User getUserByEmail(String emailaddress) throws UserManagerException 639 { 640 for (Iterator iter = getUserNameToUserMap().values().iterator(); iter.hasNext();) 642 { 643 User element = (User) iter.next(); 644 String email = element.getEmail(); 645 if ( email != null && email.equalsIgnoreCase(emailaddress)) 646 { 647 return element; 648 } 649 } 650 for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) 652 { 653 String username = (String ) iter.next(); 654 User element = getUser(username); 655 656 String 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 inQuery) throws UserManagerException 665 { 666 return findUser(inQuery, 1000); 667 } 668 672 public HitTracker findUser(String 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 iter = getUsers().getAllHits(); iter.hasNext() && tracker.getTotal() < maxNum;) 679 { 680 String username = (String ) iter.next(); 681 User user = getUser(username); 682 tracker.addHit(user); 683 } 684 return tracker; 685 } 686 for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) 687 { 688 String username = (String ) 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 inText, String 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 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 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 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 iter = inGroup.getPermissions().iterator(); iter.hasNext();) 778 { 779 String permission = (String ) 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 file = loadGroupFile(inGroup.getName()); 791 getXmlUtil().saveXml(doc, file); 792 793 getGroupNameToGroupMap().put( inGroup.getName(), inGroup); 794 } 795 796 public String encrypt(String 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 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 ( getUserDirectory(), "users.properties" ) ); 821 } 822 return fieldUserIdCounter; 823 } 824 public HitTracker getUsersInGroup(String inGroup) 825 { 826 Group group = getGroup(inGroup); 827 return getUsersInGroup(group); 828 } 829 830 public HitTracker getUsersInGroup(Group inGroup) 832 { 833 List all = new ArrayList (); 834 for (Iterator iter = getUsers().getAllHits(); iter.hasNext();) 835 { 836 String name = (String )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 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 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 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 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 getUserListeners() 898 { 899 if (fieldUserListeners == null) 900 { 901 fieldUserListeners = new HashMap (); 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 inAccount, String inPassword, String inGroupname) 945 { 946 FileSystemUser user = new FileSystemUser(); 947 user.setCreationDate(new Date ()); 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 |