1 package org.apache.fulcrum.security; 2 3 56 57 import java.security.MessageDigest ; 58 import java.util.Map ; 59 60 import org.apache.commons.codec.binary.Base64; 61 import org.apache.fulcrum.BaseService; 62 import org.apache.fulcrum.InitializationException; 63 import org.apache.fulcrum.TurbineServices; 64 import org.apache.fulcrum.factory.FactoryService; 65 import org.apache.fulcrum.security.entity.Group; 66 import org.apache.fulcrum.security.entity.Permission; 67 import org.apache.fulcrum.security.entity.Role; 68 import org.apache.fulcrum.security.entity.User; 69 import org.apache.fulcrum.security.util.AccessControlList; 70 import org.apache.fulcrum.security.util.DataBackendException; 71 import org.apache.fulcrum.security.util.EntityExistsException; 72 import org.apache.fulcrum.security.util.GroupSet; 73 import org.apache.fulcrum.security.util.PasswordMismatchException; 74 import org.apache.fulcrum.security.util.PermissionSet; 75 import org.apache.fulcrum.security.util.RoleSet; 76 import org.apache.fulcrum.security.util.UnknownEntityException; 77 import org.apache.torque.util.Criteria; 78 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 79 80 100 public abstract class BaseSecurityService 101 extends BaseService 102 implements SecurityService 103 { 104 106 107 private GroupSet allGroups = null; 108 109 110 private RoleSet allRoles = null; 111 112 113 private PermissionSet allPermissions = null; 114 115 116 private int readerCount = 0; 117 118 119 protected UserManager userManager = null; 120 121 122 private Class userClass = null; 123 124 125 private Class groupClass = null; 126 127 128 private Class permissionClass = null; 129 130 131 private Class roleClass = null; 132 133 134 private Class aclClass = null; 135 136 137 private FactoryService aclFactoryService = null; 138 139 142 private static Group globalGroup = null; 143 144 156 public String encryptPassword(String password) 157 { 158 if (password == null) 159 { 160 return null; 161 } 162 163 String secure = getConfiguration().getString( 164 SecurityService.SECURE_PASSWORDS_KEY, 165 SecurityService.SECURE_PASSWORDS_DEFAULT).toLowerCase(); 166 167 String algorithm = getConfiguration().getString( 168 SecurityService.SECURE_PASSWORDS_ALGORITHM_KEY, 169 SecurityService.SECURE_PASSWORDS_ALGORITHM_DEFAULT); 170 171 if (secure.equals("true") || secure.equals("yes")) 172 { 173 try 174 { 175 MessageDigest md = MessageDigest.getInstance(algorithm); 176 byte[] digest = md.digest(password.getBytes("UTF-8")); 179 180 Base64 base64 = new Base64(); 184 byte[] encodedDigest = base64.encodeBase64(digest); 185 return (encodedDigest == null ? null : 186 new String (encodedDigest)); 187 } 188 catch (Exception e) 189 { 190 getCategory().error("Unable to encrypt password"); 191 getCategory().error(e); 192 193 return null; 194 } 195 } 196 else 197 { 198 return password; 199 } 200 } 201 202 207 public void init() 208 throws InitializationException 209 { 210 String userManagerClassName = getConfiguration().getString( 211 SecurityService.USER_MANAGER_KEY, 212 SecurityService.USER_MANAGER_DEFAULT); 213 214 String userClassName = getConfiguration().getString( 215 SecurityService.USER_CLASS_KEY, 216 SecurityService.USER_CLASS_DEFAULT); 217 218 String groupClassName = getConfiguration().getString( 219 SecurityService.GROUP_CLASS_KEY, 220 SecurityService.GROUP_CLASS_DEFAULT); 221 222 String permissionClassName = getConfiguration().getString( 223 SecurityService.PERMISSION_CLASS_KEY, 224 SecurityService.PERMISSION_CLASS_DEFAULT); 225 226 String roleClassName = getConfiguration().getString( 227 SecurityService.ROLE_CLASS_KEY, 228 SecurityService.ROLE_CLASS_DEFAULT); 229 230 String aclClassName = getConfiguration().getString( 231 SecurityService.ACL_CLASS_KEY, 232 SecurityService.ACL_CLASS_DEFAULT); 233 234 try 235 { 236 userClass = Class.forName(userClassName); 237 groupClass = Class.forName(groupClassName); 238 permissionClass = Class.forName(permissionClassName); 239 roleClass = Class.forName(roleClassName); 240 aclClass = Class.forName(aclClassName); 241 } 242 catch (Exception e) 243 { 244 if (userClass == null) 245 { 246 throw new InitializationException( 247 "Failed to create a Class object for User implementation", e); 248 } 249 if (groupClass == null) 250 { 251 throw new InitializationException( 252 "Failed to create a Class object for Group implementation", e); 253 } 254 if (permissionClass == null) 255 { 256 throw new InitializationException( 257 "Failed to create a Class object for Permission implementation", e); 258 } 259 if (roleClass == null) 260 { 261 throw new InitializationException( 262 "Failed to create a Class object for Role implementation", e); 263 } 264 if (aclClass == null) 265 { 266 throw new InitializationException( 267 "Failed to create a Class object for ACL implementation", e); 268 } 269 } 270 271 try 272 { 273 userManager = (UserManager) Class. 274 forName(userManagerClassName).newInstance(); 275 } 276 catch (Exception e) 277 { 278 throw new InitializationException( 279 "BaseSecurityService.init: Failed to instantiate UserManager", e); 280 } 281 282 try 283 { 284 aclFactoryService = getFactoryService(); 285 } 286 catch (Exception e) 287 { 288 throw new InitializationException( 289 "BaseSecurityService.init: Failed to get the Factory Service object", e); 290 } 291 292 293 setInit(true); 294 } 295 296 304 public Class getUserClass() 305 throws UnknownEntityException 306 { 307 if (userClass == null) 308 { 309 throw new UnknownEntityException( 310 "Failed to create a Class object for User implementation"); 311 } 312 return userClass; 313 } 314 315 324 public User getUserInstance() 325 throws UnknownEntityException 326 { 327 User user; 328 try 329 { 330 user = (User) getUserClass().newInstance(); 331 } 332 catch (Exception e) 333 { 334 throw new UnknownEntityException("Failed instantiate an User implementation object", e); 335 } 336 return user; 337 } 338 339 351 public User getUserInstance(String userName) 352 throws UnknownEntityException 353 { 354 User user = getUserInstance(); 355 user.setName(userName); 356 return user; 357 } 358 359 367 public Class getGroupClass() 368 throws UnknownEntityException 369 { 370 if (groupClass == null) 371 { 372 throw new UnknownEntityException( 373 "Failed to create a Class object for Group implementation"); 374 } 375 return groupClass; 376 } 377 378 387 public Group getGroupInstance() 388 throws UnknownEntityException 389 { 390 Group group; 391 try 392 { 393 group = (Group) getGroupClass().newInstance(); 394 } 395 catch (Exception e) 396 { 397 throw new UnknownEntityException("Failed instantiate an Group implementation object", e); 398 } 399 return group; 400 } 401 402 414 public Group getGroupInstance(String groupName) 415 throws UnknownEntityException 416 { 417 Group group = getGroupInstance(); 418 group.setName(groupName); 419 return group; 420 } 421 422 430 public Class getPermissionClass() 431 throws UnknownEntityException 432 { 433 if (permissionClass == null) 434 { 435 throw new UnknownEntityException( 436 "Failed to create a Class object for Permission implementation"); 437 } 438 return permissionClass; 439 } 440 441 450 public Permission getPermissionInstance() 451 throws UnknownEntityException 452 { 453 Permission permission; 454 try 455 { 456 permission = (Permission) getPermissionClass().newInstance(); 457 } 458 catch (Exception e) 459 { 460 throw new UnknownEntityException("Failed instantiate an Permission implementation object", e); 461 } 462 return permission; 463 } 464 465 476 public Permission getPermissionInstance(String permName) 477 throws UnknownEntityException 478 { 479 Permission perm = getPermissionInstance(); 480 perm.setName(permName); 481 return perm; 482 } 483 484 492 public Class getRoleClass() 493 throws UnknownEntityException 494 { 495 if (roleClass == null) 496 { 497 throw new UnknownEntityException( 498 "Failed to create a Class object for Role implementation"); 499 } 500 return roleClass; 501 } 502 503 512 public Role getRoleInstance() 513 throws UnknownEntityException 514 { 515 Role role; 516 try 517 { 518 role = (Role) getRoleClass().newInstance(); 519 } 520 catch (Exception e) 521 { 522 throw new UnknownEntityException("Failed instantiate an Role implementation object", e); 523 } 524 return role; 525 } 526 527 539 public Role getRoleInstance(String roleName) 540 throws UnknownEntityException 541 { 542 Role role = getRoleInstance(); 543 role.setName(roleName); 544 return role; 545 } 546 547 555 public Class getAclClass() 556 throws UnknownEntityException 557 { 558 if (aclClass == null) 559 { 560 throw new UnknownEntityException( 561 "Failed to create a Class object for ACL implementation"); 562 } 563 return aclClass; 564 } 565 566 578 public AccessControlList getAclInstance(Map roles, Map permissions) 579 throws UnknownEntityException 580 { 581 Object [] objects = { roles, permissions }; 582 String [] signatures = {"java.util.Map", "java.util.Map"}; 583 AccessControlList accessControlList; 584 585 try 586 { 587 accessControlList = 588 (AccessControlList) aclFactoryService.getInstance(aclClass.getName(), 589 objects, 590 signatures); 591 } 592 catch (Exception e) 593 { 594 throw new UnknownEntityException( 595 "Failed instantiate an ACL implementation object", e); 596 } 597 598 return accessControlList; 599 } 600 601 610 public boolean accountExists(User user) 611 throws DataBackendException 612 { 613 return userManager.accountExists(user); 614 } 615 616 627 public boolean accountExists(String userName) 628 throws DataBackendException 629 { 630 return userManager.accountExists(userName); 631 } 632 633 647 public User getAuthenticatedUser(String username, String password) 648 throws DataBackendException, UnknownEntityException, 649 PasswordMismatchException 650 { 651 return userManager.retrieve(username, password); 652 } 653 654 664 public User getUser(String username) 665 throws DataBackendException, UnknownEntityException 666 { 667 return userManager.retrieve(username); 668 } 669 670 684 public User[] getUsers(Criteria criteria) 685 throws DataBackendException 686 { 687 return userManager.retrieve(criteria); 688 } 689 690 697 public User getAnonymousUser() 698 throws UnknownEntityException 699 { 700 User user = getUserInstance(); 701 user.setUserName(""); 702 return user; 703 } 704 705 716 public void saveUser(User user) 717 throws UnknownEntityException, DataBackendException 718 { 719 userManager.store(user); 720 } 721 722 731 public void addUser(User user, String password) 732 throws DataBackendException, EntityExistsException 733 { 734 userManager.createAccount(user, password); 735 } 736 737 744 public void removeUser(User user) 745 throws DataBackendException, UnknownEntityException 746 { 747 revokeAll(user); 749 750 userManager.removeAccount(user); 751 } 752 753 766 public void changePassword(User user, String oldPassword, String newPassword) 767 throws PasswordMismatchException, UnknownEntityException, 768 DataBackendException 769 { 770 userManager.changePassword(user, oldPassword, newPassword); 771 } 772 773 788 public void forcePassword(User user, String password) 789 throws UnknownEntityException, DataBackendException 790 { 791 userManager.forcePassword(user, password); 792 } 793 794 800 protected synchronized void lockShared() 801 { 802 readerCount++; 803 } 804 805 811 protected synchronized void unlockShared() 812 { 813 readerCount--; 814 this.notify(); 815 } 816 817 824 protected void lockExclusive() 825 { 826 while (readerCount > 0) 827 { 828 try 829 { 830 this.wait(); 831 } 832 catch (InterruptedException e) 833 { 834 } 835 } 836 } 837 838 845 protected void unlockExclusive() 846 { 847 } 849 850 856 public Group getGlobalGroup() 857 { 858 if (globalGroup == null) 859 { 860 synchronized (BaseSecurityService.class) 861 { 862 if (globalGroup == null) 863 { 864 try 865 { 866 globalGroup = getAllGroups() 867 .getGroup(Group.GLOBAL_GROUP_NAME); 868 } 869 catch (DataBackendException e) 870 { 871 getCategory().error("Failed to retrieve global group object"); 872 getCategory().error(e); 873 } 874 } 875 } 876 } 877 return globalGroup; 878 } 879 880 891 public Group getGroup(String name) 892 throws DataBackendException, UnknownEntityException 893 { 894 GroupSet groups = getAllGroups(); 895 Group group = groups.getGroup(name); 896 if (group != null) 897 { 898 return group; 899 } 900 else 901 { 902 throw new UnknownEntityException("The specified group does not exist"); 903 } 904 } 905 906 917 public Role getRole(String name) 918 throws DataBackendException, UnknownEntityException 919 { 920 RoleSet roles = getAllRoles(); 921 Role role = roles.getRole(name); 922 if (role != null) 923 { 924 role.setPermissions(getPermissions(role)); 925 return role; 926 } 927 else 928 { 929 throw new UnknownEntityException("The specified role does not exist"); 930 } 931 } 932 933 944 public Permission getPermission(String name) 945 throws DataBackendException, UnknownEntityException 946 { 947 PermissionSet permissions = getAllPermissions(); 948 Permission permission = permissions.getPermission(name); 949 if (permission != null) 950 { 951 return permission; 952 } 953 else 954 { 955 throw new UnknownEntityException("The specified permission does not exist"); 956 } 957 } 958 959 965 public GroupSet getAllGroups() 966 throws DataBackendException 967 { 968 return getGroups(new Criteria()); 969 } 970 971 977 public RoleSet getAllRoles() 978 throws DataBackendException 979 { 980 return getRoles(new Criteria()); 981 } 982 983 989 public PermissionSet getAllPermissions() 990 throws DataBackendException 991 { 992 return getPermissions(new Criteria()); 993 } 994 995 1000 protected static FactoryService getFactoryService() { 1001 try { 1002 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 1003 YaafiComponentService.SERVICE_NAME); 1004 return (FactoryService) yaafi.lookup(FactoryService.class.getName()); 1005 } catch (Exception e) { 1006 throw new RuntimeException ("Problem looking up Factory service", e); 1007 } 1008 } 1009} 1010 | Popular Tags |