1 64 65 68 package com.jcorporate.expresso.services.controller; 69 70 import com.jcorporate.expresso.core.controller.Controller; 71 import com.jcorporate.expresso.core.controller.ControllerException; 72 import com.jcorporate.expresso.core.controller.ControllerRequest; 73 import com.jcorporate.expresso.core.controller.ControllerResponse; 74 import com.jcorporate.expresso.core.controller.DBController; 75 import com.jcorporate.expresso.core.controller.ErrorCollection; 76 import com.jcorporate.expresso.core.controller.NonHandleableException; 77 import com.jcorporate.expresso.core.controller.Output; 78 import com.jcorporate.expresso.core.controller.ServletControllerRequest; 79 import com.jcorporate.expresso.core.controller.session.PersistentSession; 80 import com.jcorporate.expresso.core.db.DBException; 81 import com.jcorporate.expresso.core.dbobj.ValidValue; 82 import com.jcorporate.expresso.core.misc.ConfigManager; 83 import com.jcorporate.expresso.core.misc.CookieUtil; 84 import com.jcorporate.expresso.core.misc.CurrentLogin; 85 import com.jcorporate.expresso.core.misc.StringUtil; 86 import com.jcorporate.expresso.core.security.DelayThread; 87 import com.jcorporate.expresso.core.security.User; 88 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 89 import org.apache.log4j.Logger; 90 91 import javax.servlet.http.Cookie ; 92 import javax.servlet.http.HttpServletRequest ; 93 import javax.servlet.http.HttpServletResponse ; 94 import java.util.Enumeration ; 95 import java.util.Vector ; 96 97 107 public abstract class LoginController 108 extends DBController { 109 110 public static final String LOGINNAME_COOKIE = "UserName"; 111 public static final String PASSWORD_COOKIE = "Password"; 112 public static final String DBNAME_COOKIE = "db"; 113 public static final String CLASS_HANDLER_NAME = "login"; 114 public static final String DEFAULT_CLASS_NAME = com.jcorporate.expresso.services.controller.SimpleLoginController.class.getName(); 115 116 private static Logger log = Logger.getLogger(LoginController.class); 117 118 122 public LoginController() { 123 super(); 124 } 126 127 134 protected void delayLogin() { 135 DelayThread.delay(); 136 } 137 138 158 protected int attemptLogin(ControllerRequest request, 159 ControllerResponse response, 160 ErrorCollection errors, 161 HttpServletRequest hreq, 162 HttpServletResponse hres, 163 PersistentSession session) 164 throws ControllerException, NonHandleableException, DBException { 165 try { 166 int uid = 0; 167 User myUser = new User(); 168 myUser.setDataContext(request.getDataContext()); 169 String loginName = StringUtil.notNull(request.getParameter("LoginName")); 170 171 if (loginName.equals("")) { 172 errors.addError("error.nologinname"); return uid; 174 } 175 176 myUser.setLoginName(loginName); 177 178 if ("NONE".equalsIgnoreCase(loginName) || !myUser.find()) { 179 errors.addError("error.invalidusername"); 180 181 186 logInvalidLoginAttempt(response.getString("error.invalidusername"), request); 189 delayLogin(); 190 191 return uid; 192 } 193 if (!myUser.passwordEquals(StringUtil.notNull(request.getParameter("Password")))) { 195 196 errors.addError("error.invalidusername"); 200 logInvalidLoginAttempt(response.getString("error.invalidusername"), request); 203 delayLogin(); 204 205 return uid; 206 } 207 208 uid = myUser.getUid(); 209 210 String currStatus = myUser.getAccountStatus(); 211 212 if (!currStatus.equals(User.ACTIVE_ACCOUNT_STATUS)) { 214 ValidValue vv = null; 215 String currDescrip = "Unknown Status '" + currStatus + "'"; 216 Vector v = myUser.getValidValues("AccountStatus"); 217 218 for (Enumeration ev = v.elements(); ev.hasMoreElements();) { 219 vv = (ValidValue) ev.nextElement(); 220 221 if (vv.getValue().equals(currStatus)) { 222 currDescrip = vv.getDescription(); 223 } 224 } 225 226 this.logInvalidLoginAttempt(response.getString("error.login.invalidlogin", loginName, currDescrip), 227 request); 228 229 errors.addError("error.login.invalidlogin", loginName, currDescrip); delayLogin(); 232 return uid; 233 } 234 235 239 try { 243 User user = new User(); 244 user.setDataContext(request.getDataContext()); 245 user.setUid(uid); 246 if (!user.find()) { 247 log.error( 248 "runDoLoginState unexpectedly cannot find user (after successful login!) for uid: " + uid); 249 } else { 250 loginName = user.getLoginName(); 251 } 252 } catch (DBException e) { 253 log.error("unexpectedly cannot find user" + e); 254 } 255 256 if (log.isInfoEnabled()) { 257 log.info("Successful login for user: " + loginName); 258 } 259 260 261 265 if (StringUtil.notNull(request.getParameter("Remember")).equalsIgnoreCase("Y")) { 266 setCookie(loginName, request.getParameter("Password"), hres, false, request.getDataContext()); 267 response.add(new Output("remembered", response.getString("Login_Remembered"))); 268 } else { 269 setCookie(User.UNKNOWN_USER, "NONE", hres, true, request.getDataContext()); 270 response.add(new Output("remembered", 271 response.getString("Login_Not_Remembered"))); 272 } 273 274 275 response.setUser(loginName); 276 request.setUser(loginName); 277 request.setUid(uid); 278 setPersistentLoginAttributes(request, loginName); 280 281 282 return uid; 283 } catch (Throwable t) { 284 log.error("Exception caught attempting login", t); 285 throw new ControllerException("Error while attempting login processing", t); 286 } 287 } 288 289 304 public static void setPersistentLoginAttributes(ControllerRequest request, String loginName) throws ControllerException { 305 PersistentSession session = request.getSession(); 306 ServletControllerRequest sHreq = (ServletControllerRequest) request; 307 HttpServletRequest hreq = sHreq.getHttpServletRequest(); 308 309 session.setPersistentAttribute("UserName", loginName); 310 session.setPersistentAttribute("Password", request.getParameter("Password")); 311 session.setPersistentAttribute("db", request.getDataContext()); 312 session.removePersistentAttribute(CurrentLogin.LOGIN_KEY); 313 314 CurrentLogin myLogin = CurrentLogin.newInstance(loginName, 315 hreq.getRemoteAddr(), 316 request.getDataContext(), 317 request.getUid()); 318 319 session.setPersistentAttribute(CurrentLogin.LOGIN_KEY, myLogin); 320 321 } 322 323 324 341 public static void setCookie(String userName, 342 String password, 343 HttpServletResponse res, boolean clear, String dbname) 344 throws ControllerException { 345 try { 346 final int THIRTY_DAYS_IN_SECS = 2592000; 347 Cookie c1; 348 349 if ((userName != null) && (userName.length() > 0)) { 350 c1 = new Cookie (LOGINNAME_COOKIE, CookieUtil.cookieEncode(userName)); 351 } else { 352 c1 = new Cookie (LOGINNAME_COOKIE, User.UNKNOWN_USER); 353 } 354 if (clear == true) { 355 c1.setMaxAge(10); 356 } else { 357 c1.setMaxAge(THIRTY_DAYS_IN_SECS); 358 } 359 360 c1.setPath("/"); 361 res.addCookie(c1); 362 363 Cookie c2; 364 365 if ((password != null) && (password.length() > 0)) { 366 c2 = new Cookie (PASSWORD_COOKIE, CookieUtil.cookieEncode(password)); 367 } else { 368 c2 = new Cookie (PASSWORD_COOKIE, "NONE"); 369 } 370 if (clear == true) { 371 c2.setMaxAge(10); } else { 373 c2.setMaxAge(THIRTY_DAYS_IN_SECS); 374 } 375 376 c2.setPath("/"); res.addCookie(c2); 378 379 Cookie c3 = null; 380 381 if (clear == true) { 382 c3 = new Cookie (DBNAME_COOKIE, "NONE"); 383 c3.setMaxAge(10); 384 } else { 385 c3 = new Cookie (DBNAME_COOKIE, 386 CookieUtil.cookieEncode(dbname)); 387 c3.setMaxAge(THIRTY_DAYS_IN_SECS); 388 } 389 390 c3.setPath("/"); 391 res.addCookie(c3); 392 } catch (Exception ce) { 393 throw new ControllerException(ce); 394 } 395 } 396 397 406 407 public Controller getDefaultRegistrationController() throws ControllerException { 408 return this.getSchemaInstance().getRegistrationController(); 409 } 410 411 412 419 public static Controller getLoginController() 420 throws ControllerException { 421 String className = ConfigManager.getClassHandler(CLASS_HANDLER_NAME); 422 if (className == null || className.length() == 0) { 423 className = DEFAULT_CLASS_NAME; 424 } 425 426 return ConfigManager.getControllerFactory().getController(className); 427 } 428 429 438 439 public Controller getDefaultLoginController() throws ControllerException { 440 return this.getSchemaInstance().getLoginController(); 441 } 442 443 444 451 public void logInvalidLoginAttempt(String msg, ControllerRequest request) { 452 String remoteIP = ""; 453 try { 454 remoteIP = ((ServletControllerRequest) request).getServletRequest().getRemoteAddr(); 455 } catch (ClassCastException cce) { 456 } 459 FastStringBuffer fsb = new FastStringBuffer(msg.length() + 32); 460 fsb.append(msg); 461 if (remoteIP.length() > 0) { 462 fsb.append("\n Client IP Address: "); 463 fsb.append(remoteIP); 464 } 465 466 log.warn(fsb.toString()); 467 } 468 469 476 public void postLoginProcessing(ControllerRequest request, ControllerResponse response) 477 throws ControllerException { 478 return; 479 } 480 481 490 public boolean stateAllowed(String newState, 491 ControllerRequest params) 492 throws ControllerException { 493 if (newState.equals("promptChangePW") || 494 newState.equals("processChangePW") || 495 newState.equals("promptLogout")) { 496 return super.stateAllowed(newState, params); 497 } 498 499 return true; 500 } 501 502 503 } | Popular Tags |