1 package org.apache.turbine.services.security; 2 3 18 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.servlet.ServletConfig ; 23 24 import org.apache.commons.configuration.Configuration; 25 26 import org.apache.commons.lang.StringUtils; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.apache.torque.util.Criteria; 32 33 import org.apache.turbine.om.security.Group; 34 import org.apache.turbine.om.security.Permission; 35 import org.apache.turbine.om.security.Role; 36 import org.apache.turbine.om.security.User; 37 import org.apache.turbine.services.InitializationException; 38 import org.apache.turbine.services.TurbineBaseService; 39 import org.apache.turbine.services.TurbineServices; 40 import org.apache.turbine.services.crypto.CryptoAlgorithm; 41 import org.apache.turbine.services.crypto.CryptoService; 42 import org.apache.turbine.services.crypto.TurbineCrypto; 43 import org.apache.turbine.services.factory.FactoryService; 44 import org.apache.turbine.util.security.AccessControlList; 45 import org.apache.turbine.util.security.DataBackendException; 46 import org.apache.turbine.util.security.EntityExistsException; 47 import org.apache.turbine.util.security.GroupSet; 48 import org.apache.turbine.util.security.PasswordMismatchException; 49 import org.apache.turbine.util.security.PermissionSet; 50 import org.apache.turbine.util.security.RoleSet; 51 import org.apache.turbine.util.security.UnknownEntityException; 52 53 74 public abstract class BaseSecurityService 75 extends TurbineBaseService 76 implements SecurityService 77 { 78 79 private int readerCount = 0; 80 81 82 private UserManager userManager = null; 83 84 85 private Class userClass = null; 86 87 88 private Class groupClass = null; 89 90 91 private Class permissionClass = null; 92 93 94 private Class roleClass = null; 95 96 97 private Class aclClass = null; 98 99 100 private FactoryService aclFactoryService = null; 101 102 105 private static Group globalGroup = null; 106 107 108 private static Log log = LogFactory.getLog(BaseSecurityService.class); 109 110 122 public String encryptPassword(String password) 123 { 124 return encryptPassword(password, null); 125 } 126 127 144 145 public String encryptPassword(String password, String salt) 146 { 147 if (password == null) 148 { 149 return null; 150 } 151 String secure = getConfiguration().getString( 152 SecurityService.SECURE_PASSWORDS_KEY, 153 SecurityService.SECURE_PASSWORDS_DEFAULT).toLowerCase(); 154 155 String algorithm = getConfiguration().getString( 156 SecurityService.SECURE_PASSWORDS_ALGORITHM_KEY, 157 SecurityService.SECURE_PASSWORDS_ALGORITHM_DEFAULT); 158 159 CryptoService cs = TurbineCrypto.getService(); 160 161 if (cs != null && (secure.equals("true") || secure.equals("yes"))) 162 { 163 try 164 { 165 CryptoAlgorithm ca = cs.getCryptoAlgorithm(algorithm); 166 167 ca.setSeed(salt); 168 169 String result = ca.encrypt(password); 170 171 return result; 172 } 173 catch (Exception e) 174 { 175 log.error("Unable to encrypt password: ", e); 176 177 return null; 178 } 179 } 180 else 181 { 182 return password; 183 } 184 } 185 186 195 196 public boolean checkPassword(String checkpw, String encpw) 197 { 198 String result = encryptPassword(checkpw, encpw); 199 200 return (result == null) ? false : result.equals(encpw); 201 } 202 203 210 public void init() 211 throws InitializationException 212 { 213 Configuration conf = getConfiguration(); 214 215 String userManagerClassName = conf.getString( 216 SecurityService.USER_MANAGER_KEY, 217 SecurityService.USER_MANAGER_DEFAULT); 218 219 String userClassName = conf.getString( 220 SecurityService.USER_CLASS_KEY, 221 SecurityService.USER_CLASS_DEFAULT); 222 223 String groupClassName = conf.getString( 224 SecurityService.GROUP_CLASS_KEY, 225 SecurityService.GROUP_CLASS_DEFAULT); 226 227 String permissionClassName = conf.getString( 228 SecurityService.PERMISSION_CLASS_KEY, 229 SecurityService.PERMISSION_CLASS_DEFAULT); 230 231 String roleClassName = conf.getString( 232 SecurityService.ROLE_CLASS_KEY, 233 SecurityService.ROLE_CLASS_DEFAULT); 234 235 String aclClassName = conf.getString( 236 SecurityService.ACL_CLASS_KEY, 237 SecurityService.ACL_CLASS_DEFAULT); 238 239 try 240 { 241 userClass = Class.forName(userClassName); 242 groupClass = Class.forName(groupClassName); 243 permissionClass = Class.forName(permissionClassName); 244 roleClass = Class.forName(roleClassName); 245 aclClass = Class.forName(aclClassName); 246 } 247 catch (Exception e) 248 { 249 if (userClass == null) 250 { 251 throw new InitializationException( 252 "Failed to create a Class object for User implementation", e); 253 } 254 if (groupClass == null) 255 { 256 throw new InitializationException( 257 "Failed to create a Class object for Group implementation", e); 258 } 259 if (permissionClass == null) 260 { 261 throw new InitializationException( 262 "Failed to create a Class object for Permission implementation", e); 263 } 264 if (roleClass == null) 265 { 266 throw new InitializationException( 267 "Failed to create a Class object for Role implementation", e); 268 } 269 if (aclClass == null) 270 { 271 throw new InitializationException( 272 "Failed to create a Class object for ACL implementation", e); 273 } 274 } 275 276 try 277 { 278 UserManager userManager = 279 (UserManager) Class.forName(userManagerClassName).newInstance(); 280 281 userManager.init(conf); 282 283 setUserManager(userManager); 284 } 285 catch (Exception e) 286 { 287 throw new InitializationException("Failed to instantiate UserManager", e); 288 } 289 290 try 291 { 292 aclFactoryService = (FactoryService) TurbineServices.getInstance(). 293 getService(FactoryService.SERVICE_NAME); 294 } 295 catch (Exception e) 296 { 297 throw new InitializationException( 298 "BaseSecurityService.init: Failed to get the Factory Service object", e); 299 } 300 301 setInit(true); 302 } 303 304 311 public void init(ServletConfig config) throws InitializationException 312 { 313 init(); 314 } 315 316 324 public Class getUserClass() 325 throws UnknownEntityException 326 { 327 if (userClass == null) 328 { 329 throw new UnknownEntityException( 330 "Failed to create a Class object for User implementation"); 331 } 332 return userClass; 333 } 334 335 344 public User getUserInstance() 345 throws UnknownEntityException 346 { 347 User user; 348 try 349 { 350 user = (User) getUserClass().newInstance(); 351 } 352 catch (Exception e) 353 { 354 throw new UnknownEntityException( 355 "Failed instantiate an User implementation object", e); 356 } 357 return user; 358 } 359 360 372 public User getUserInstance(String userName) 373 throws UnknownEntityException 374 { 375 User user = getUserInstance(); 376 user.setName(userName); 377 return user; 378 } 379 380 388 public Class getGroupClass() 389 throws UnknownEntityException 390 { 391 if (groupClass == null) 392 { 393 throw new UnknownEntityException( 394 "Failed to create a Class object for Group implementation"); 395 } 396 return groupClass; 397 } 398 399 408 public Group getGroupInstance() 409 throws UnknownEntityException 410 { 411 Group group; 412 try 413 { 414 group = (Group) getGroupClass().newInstance(); 415 } 416 catch (Exception e) 417 { 418 throw new UnknownEntityException("Failed to instantiate a Group implementation object", e); 419 } 420 return group; 421 } 422 423 435 public Group getGroupInstance(String groupName) 436 throws UnknownEntityException 437 { 438 Group group = getGroupInstance(); 439 group.setName(groupName); 440 return group; 441 } 442 443 451 public Class getPermissionClass() 452 throws UnknownEntityException 453 { 454 if (permissionClass == null) 455 { 456 throw new UnknownEntityException( 457 "Failed to create a Class object for Permission implementation"); 458 } 459 return permissionClass; 460 } 461 462 471 public Permission getPermissionInstance() 472 throws UnknownEntityException 473 { 474 Permission permission; 475 try 476 { 477 permission = (Permission) getPermissionClass().newInstance(); 478 } 479 catch (Exception e) 480 { 481 throw new UnknownEntityException("Failed to instantiate a Permission implementation object", e); 482 } 483 return permission; 484 } 485 486 497 public Permission getPermissionInstance(String permName) 498 throws UnknownEntityException 499 { 500 Permission perm = getPermissionInstance(); 501 perm.setName(permName); 502 return perm; 503 } 504 505 513 public Class getRoleClass() 514 throws UnknownEntityException 515 { 516 if (roleClass == null) 517 { 518 throw new UnknownEntityException( 519 "Failed to create a Class object for Role implementation"); 520 } 521 return roleClass; 522 } 523 524 533 public Role getRoleInstance() 534 throws UnknownEntityException 535 { 536 Role role; 537 538 try 539 { 540 role = (Role) getRoleClass().newInstance(); 541 } 542 catch (Exception e) 543 { 544 throw new UnknownEntityException("Failed to instantiate a Role implementation object", e); 545 } 546 return role; 547 } 548 549 561 public Role getRoleInstance(String roleName) 562 throws UnknownEntityException 563 { 564 Role role = getRoleInstance(); 565 role.setName(roleName); 566 return role; 567 } 568 569 577 public Class getAclClass() 578 throws UnknownEntityException 579 { 580 if (aclClass == null) 581 { 582 throw new UnknownEntityException( 583 "Failed to create a Class object for ACL implementation"); 584 } 585 return aclClass; 586 } 587 588 600 public AccessControlList getAclInstance(Map roles, Map permissions) 601 throws UnknownEntityException 602 { 603 Object [] objects = {roles, permissions}; 604 String [] signatures = {Map .class.getName(), Map .class.getName()}; 605 AccessControlList accessControlList; 606 607 try 608 { 609 accessControlList = 610 (AccessControlList) aclFactoryService.getInstance(aclClass.getName(), 611 objects, 612 signatures); 613 } 614 catch (Exception e) 615 { 616 throw new UnknownEntityException( 617 "Failed to instantiate an ACL implementation object", e); 618 } 619 620 return accessControlList; 621 } 622 623 628 public UserManager getUserManager() 629 { 630 return userManager; 631 } 632 633 638 public void setUserManager(UserManager userManager) 639 { 640 this.userManager = userManager; 641 } 642 643 653 public boolean accountExists(User user) 654 throws DataBackendException 655 { 656 return getUserManager().accountExists(user); 657 } 658 659 669 public boolean accountExists(String userName) 670 throws DataBackendException 671 { 672 return getUserManager().accountExists(userName); 673 } 674 675 687 public User getAuthenticatedUser(String username, String password) 688 throws DataBackendException, UnknownEntityException, 689 PasswordMismatchException 690 { 691 return getUserManager().retrieve(username, password); 692 } 693 694 703 public User getUser(String username) 704 throws DataBackendException, UnknownEntityException 705 { 706 return getUserManager().retrieve(username); 707 } 708 709 724 public User[] getUsers(Criteria criteria) 725 throws DataBackendException 726 { 727 return (User []) getUserList(criteria).toArray(new User[0]); 728 } 729 730 744 public List getUserList(Criteria criteria) 745 throws DataBackendException 746 { 747 return getUserManager().retrieveList(criteria); 748 } 749 750 758 public User getAnonymousUser() 759 throws UnknownEntityException 760 { 761 User user = getUserInstance(); 762 user.setName(""); 763 return user; 764 } 765 766 775 public boolean isAnonymousUser(User user) 776 { 777 return (user == null) || StringUtils.isEmpty(user.getName()); 779 } 780 781 790 public void saveUser(User user) 791 throws UnknownEntityException, DataBackendException 792 { 793 getUserManager().store(user); 794 } 795 796 808 public void saveOnSessionUnbind(User user) 809 throws UnknownEntityException, DataBackendException 810 { 811 userManager.saveOnSessionUnbind(user); 812 } 813 814 824 public void addUser(User user, String password) 825 throws DataBackendException, EntityExistsException 826 { 827 getUserManager().createAccount(user, password); 828 } 829 830 838 public void removeUser(User user) 839 throws DataBackendException, UnknownEntityException 840 { 841 revokeAll(user); 843 844 getUserManager().removeAccount(user); 845 } 846 847 858 public void changePassword(User user, String oldPassword, 859 String newPassword) 860 throws PasswordMismatchException, UnknownEntityException, 861 DataBackendException 862 { 863 getUserManager().changePassword(user, oldPassword, newPassword); 864 } 865 866 880 public void forcePassword(User user, String password) 881 throws UnknownEntityException, DataBackendException 882 { 883 getUserManager().forcePassword(user, password); 884 } 885 886 892 protected synchronized void lockShared() 893 { 894 readerCount++; 895 } 896 897 903 protected synchronized void unlockShared() 904 { 905 readerCount--; 906 this.notify(); 907 } 908 909 916 protected void lockExclusive() 917 { 918 while (readerCount > 0) 919 { 920 try 921 { 922 this.wait(); 923 } 924 catch (InterruptedException e) 925 { 926 } 927 } 928 } 929 930 937 protected void unlockExclusive() 938 { 939 } 941 942 948 public Group getGlobalGroup() 949 { 950 if (globalGroup == null) 951 { 952 synchronized (BaseSecurityService.class) 953 { 954 if (globalGroup == null) 955 { 956 try 957 { 958 globalGroup = getAllGroups() 959 .getGroupByName(Group.GLOBAL_GROUP_NAME); 960 } 961 catch (DataBackendException e) 962 { 963 log.error("Failed to retrieve global group object: ", e); 964 } 965 } 966 } 967 } 968 return globalGroup; 969 } 970 971 981 public Group getGroup(String name) 982 throws DataBackendException, UnknownEntityException 983 { 984 return getGroupByName(name); 985 } 986 987 996 public Group getGroupByName(String name) 997 throws DataBackendException, UnknownEntityException 998 { 999 Group group = getAllGroups().getGroupByName(name); 1000 if (group == null) 1001 { 1002 throw new UnknownEntityException( 1003 "The specified group does not exist"); 1004 } 1005 return group; 1006 } 1007 1008 1018 public Group getGroupById(int id) 1019 throws DataBackendException, UnknownEntityException 1020 { 1021 Group group = getAllGroups().getGroupById(id); 1022 if (group == null) 1023 { 1024 throw new UnknownEntityException( 1025 "The specified group does not exist"); 1026 } 1027 return group; 1028 } 1029 1030 1040 public Role getRole(String name) 1041 throws DataBackendException, UnknownEntityException 1042 { 1043 return getRoleByName(name); 1044 } 1045 1046 1055 public Role getRoleByName(String name) 1056 throws DataBackendException, UnknownEntityException 1057 { 1058 Role role = getAllRoles().getRoleByName(name); 1059 if (role == null) 1060 { 1061 throw new UnknownEntityException( 1062 "The specified role does not exist"); 1063 } 1064 role.setPermissions(getPermissions(role)); 1065 return role; 1066 } 1067 1068 1077 public Role getRoleById(int id) 1078 throws DataBackendException, 1079 UnknownEntityException 1080 { 1081 Role role = getAllRoles().getRoleById(id); 1082 if (role == null) 1083 { 1084 throw new UnknownEntityException( 1085 "The specified role does not exist"); 1086 } 1087 role.setPermissions(getPermissions(role)); 1088 return role; 1089 } 1090 1091 1101 public Permission getPermission(String name) 1102 throws DataBackendException, UnknownEntityException 1103 { 1104 return getPermissionByName(name); 1105 } 1106 1107 1116 public Permission getPermissionByName(String name) 1117 throws DataBackendException, UnknownEntityException 1118 { 1119 Permission permission = getAllPermissions().getPermissionByName(name); 1120 if (permission == null) 1121 { 1122 throw new UnknownEntityException( 1123 "The specified permission does not exist"); 1124 } 1125 return permission; 1126 } 1127 1128 1138 public Permission getPermissionById(int id) 1139 throws DataBackendException, 1140 UnknownEntityException 1141 { 1142 Permission permission = getAllPermissions().getPermissionById(id); 1143 if (permission == null) 1144 { 1145 throw new UnknownEntityException( 1146 "The specified permission does not exist"); 1147 } 1148 return permission; 1149 } 1150 1151 1158 public GroupSet getAllGroups() 1159 throws DataBackendException 1160 { 1161 return getGroups(new Criteria()); 1162 } 1163 1164 1171 public RoleSet getAllRoles() 1172 throws DataBackendException 1173 { 1174 return getRoles(new Criteria()); 1175 } 1176 1177 1184 public PermissionSet getAllPermissions() 1185 throws DataBackendException 1186 { 1187 return getPermissions(new Criteria()); 1188 } 1189 1190 1193 public Group getNewGroup(String groupName) 1194 { 1195 try 1196 { 1197 return getGroupInstance(groupName); 1198 } 1199 catch (UnknownEntityException uee) 1200 { 1201 uee.printStackTrace(); 1202 return null; 1203 } 1204 } 1205 1206 1209 public Role getNewRole(String roleName) 1210 { 1211 try 1212 { 1213 return getRoleInstance(roleName); 1214 } 1215 catch (UnknownEntityException uee) 1216 { 1217 return null; 1218 } 1219 } 1220 1221 1224 public Permission getNewPermission(String permissionName) 1225 { 1226 try 1227 { 1228 return getPermissionInstance(permissionName); 1229 } 1230 catch (UnknownEntityException uee) 1231 { 1232 return null; 1233 } 1234 } 1235} 1236 | Popular Tags |