1 16 17 package org.apache.jetspeed.services.security; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.ServletConfig ; 25 26 import org.apache.jetspeed.om.security.JetspeedUser; 27 import org.apache.jetspeed.om.security.JetspeedUserFactory; 28 import org.apache.jetspeed.om.security.UserNamePrincipal; 29 import org.apache.jetspeed.portal.Portlet; 30 import org.apache.jetspeed.services.JetspeedPortalAccessController; 31 import org.apache.jetspeed.services.JetspeedSecurity; 32 import org.apache.jetspeed.services.JetspeedUserManagement; 33 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 34 import org.apache.jetspeed.services.logging.JetspeedLogger; 35 import org.apache.jetspeed.services.rundata.JetspeedRunData; 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.resources.ResourceService; 41 42 50 51 public class JetspeedDBSecurityService extends TurbineBaseService 52 implements JetspeedSecurityService 53 { 54 57 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedDBSecurityService.class.getName()); 58 59 private final static String CONFIG_CASEINSENSITIVE_USERNAME = "caseinsensitive.username"; 60 private final static String CONFIG_CASEINSENSITIVE_PASSWORD = "caseinsensitive.password"; 61 private final static String CONFIG_CASEINSENSITIVE_UPPER = "caseinsensitive.upper"; 62 private final static String CONFIG_LOGON_STRIKE_COUNT = "logon.strike.count"; 63 private final static String CONFIG_LOGON_STRIKE_MAX = "logon.strike.max"; 64 private final static String CONFIG_LOGON_STRIKE_INTERVAL = "logon.strike.interval"; 65 private final static String CONFIG_LOGON_AUTO_DISABLE = "logon.auto.disable"; 66 private final static String CONFIG_ACTIONS_ANON_DISABLE = "actions.anon.disable"; 67 private final static String CONFIG_ACTIONS_ALLUSERS_DISABLE = "actions.allusers.disable"; 68 private final static String CONFIG_ACTIONS_ADMIN_ROLES = "admin.roles"; 69 70 private final static String CONFIG_NEWUSER_ROLES = "newuser.roles"; 71 private final static String CONFIG_DEFAULT_PERMISSION_LOGGEDIN = "permission.default.loggedin"; 72 private final static String CONFIG_DEFAULT_PERMISSION_ANONYMOUS = "permission.default.anonymous"; 73 private final static String CONFIG_ANONYMOUS_USER = "user.anonymous"; 74 private final static String [] DEFAULT_PERMISSIONS = {""}; 75 private final static String [] DEFAULT_CONFIG_NEWUSER_ROLES = 76 { "user" }; 77 private final static String [] DEFAULT_ADMIN_ROLES = 78 { "admin" }; 79 80 String roles[] = null; 81 boolean caseInsensitiveUsername = false; 82 boolean caseInsensitivePassword = false; 83 boolean caseInsensitiveUpper = true; 84 boolean actionsAnonDisable = true; 85 boolean actionsAllUsersDisable = false; 86 String anonymousUser = "anon"; 87 String [] adminRoles = null; 88 89 int strikeCount = 3; int strikeMax = 20; long strikeInterval = 300; 93 boolean autoLogonDisable = false; 94 95 private static HashMap users = new HashMap (); 96 97 private static Object sem = new Object (); 98 99 106 public synchronized void init(ServletConfig conf) throws InitializationException 107 { 108 if (getInit()) return; 110 111 super.init(conf); 112 113 ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance()) 115 .getResources(JetspeedSecurityService.SERVICE_NAME); 116 117 try 118 { 119 roles = serviceConf.getStringArray(CONFIG_NEWUSER_ROLES); 120 adminRoles = serviceConf.getStringArray(CONFIG_ACTIONS_ADMIN_ROLES); 121 } 122 catch (Exception e) 123 {} 124 125 if (null == roles || roles.length == 0) 126 { 127 roles = DEFAULT_CONFIG_NEWUSER_ROLES; 128 } 129 130 if (null == adminRoles || adminRoles.length == 0) 131 { 132 adminRoles = DEFAULT_ADMIN_ROLES; 133 } 134 135 caseInsensitiveUsername = serviceConf.getBoolean(CONFIG_CASEINSENSITIVE_USERNAME, caseInsensitiveUsername); 136 caseInsensitivePassword = serviceConf.getBoolean(CONFIG_CASEINSENSITIVE_PASSWORD, caseInsensitivePassword); 137 caseInsensitiveUpper = serviceConf.getBoolean(CONFIG_CASEINSENSITIVE_UPPER, caseInsensitiveUpper); 138 139 strikeCount = serviceConf.getInt(CONFIG_LOGON_STRIKE_COUNT, strikeCount); 140 strikeInterval = serviceConf.getLong(CONFIG_LOGON_STRIKE_INTERVAL, strikeInterval); 141 strikeMax = serviceConf.getInt(CONFIG_LOGON_STRIKE_MAX, strikeMax); 142 143 autoLogonDisable = serviceConf.getBoolean(CONFIG_LOGON_AUTO_DISABLE, autoLogonDisable); 144 actionsAnonDisable = serviceConf.getBoolean(CONFIG_ACTIONS_ANON_DISABLE, actionsAnonDisable); 145 actionsAllUsersDisable = serviceConf.getBoolean(CONFIG_ACTIONS_ALLUSERS_DISABLE, actionsAllUsersDisable); 146 147 anonymousUser = serviceConf.getString(CONFIG_ANONYMOUS_USER, anonymousUser); 148 149 setInit(true); 151 } 152 153 154 162 172 public JetspeedUser getUserInstance() 173 { 174 try 175 { 176 return JetspeedUserFactory.getInstance(); 177 } 178 catch (UserException e) 179 { 180 return null; 181 } 182 } 183 184 192 209 public String convertUserName(String username) 210 { 211 if (caseInsensitiveUsername) 212 { 213 username = (caseInsensitiveUpper) ? username.toUpperCase() : username.toLowerCase(); 214 } 215 return username; 216 } 217 218 235 public String convertPassword(String password) 236 { 237 if (caseInsensitivePassword) 238 { 239 password = (caseInsensitiveUpper) ? password.toUpperCase() : password.toLowerCase(); 240 } 241 return password; 242 } 243 244 271 public boolean checkDisableAccount(String username) 272 { 273 username = convertUserName(username); 274 275 UserLogonStats stat = (UserLogonStats)users.get(username); 277 if (stat == null) 278 { 279 stat = new UserLogonStats(username); 280 synchronized (sem) 281 { 282 users.put(username, stat); 283 } 284 } 285 boolean disabled = stat.failCheck(strikeCount, strikeInterval, strikeMax); 286 287 if (disabled) 288 { 289 try 290 { 291 JetspeedUser user = (JetspeedUser)JetspeedSecurity.getUser(username); 293 if (user != null) 294 { 295 user.setDisabled(true); 296 JetspeedSecurity.saveUser(user); 297 } 298 } 299 catch (Exception e) 300 { 301 logger.error("Could not disable user: " + username, e); 302 } 303 } 304 return disabled; 305 } 306 307 320 public boolean isDisableAccountCheckEnabled() 321 { 322 return autoLogonDisable; 323 } 324 325 326 339 public void resetDisableAccountCheck(String username) 340 { 341 username = convertUserName(username); 343 UserLogonStats stat = (UserLogonStats)users.get(username); 344 if (stat == null) 345 { 346 stat = new UserLogonStats(username); 347 synchronized (sem) 348 { 349 users.put(username, stat); 350 } 351 } 352 stat.reset(); 353 } 354 355 356 362 379 380 public JetspeedUser getUser(String username) 381 throws JetspeedSecurityException 382 { 383 return JetspeedUserManagement.getUser(new UserNamePrincipal(username)); 384 } 385 386 387 399 public boolean checkPermission(JetspeedRunData runData, String action, Portlet portlet) 400 { 401 return JetspeedPortalAccessController.checkPermission(runData.getJetspeedUser(), 402 portlet, 403 action); 404 } 405 406 424 425 433 public boolean areActionsDisabledForAnon() 434 { 435 return actionsAnonDisable; 436 } 437 438 446 public boolean areActionsDisabledForAllUsers() 447 { 448 return actionsAllUsersDisable; 449 } 450 451 458 public String getAnonymousUserName() 459 { 460 return anonymousUser; 461 } 462 463 468 public List getAdminRoles() 469 { 470 List result = new ArrayList (); 471 for (int i = 0; i < adminRoles.length; i++) 472 { 473 result.add(adminRoles[i]); 474 } 475 476 return result; 477 } 478 479 485 public boolean hasAdminRole(User user) 486 { 487 String username = user.getUserName(); 488 try 489 { 490 List adminRoles = getAdminRoles(); 491 for (Iterator it = adminRoles.iterator(); it.hasNext();) 492 { 493 if (JetspeedSecurity.hasRole(username, (String )it.next())) 494 { 495 return true; 496 } 497 } 498 } 499 catch (Exception e) 500 { 501 logger.error(e); 502 } 503 504 return false; 505 } 506 507 } 508 509 | Popular Tags |