1 3 150 package com.ivata.groupware.admin.security.struts; 151 152 import java.lang.reflect.InvocationTargetException ; 153 import java.util.Properties ; 154 155 import javax.servlet.http.HttpServletRequest ; 156 import javax.servlet.http.HttpServletResponse ; 157 import javax.servlet.http.HttpSession ; 158 159 import org.apache.commons.beanutils.PropertyUtils; 160 import org.apache.log4j.Logger; 161 import org.apache.struts.action.ActionErrors; 162 import org.apache.struts.action.ActionForm; 163 import org.apache.struts.action.ActionForward; 164 import org.apache.struts.action.ActionMapping; 165 import org.apache.struts.action.ActionMessage; 166 import org.apache.struts.action.ActionMessages; 167 import org.apache.struts.util.RequestUtils; 168 import org.picocontainer.PicoContainer; 169 170 import com.ivata.groupware.admin.security.Security; 171 import com.ivata.groupware.admin.security.server.SecuritySession; 172 import com.ivata.groupware.admin.security.user.UserDO; 173 import com.ivata.groupware.admin.setting.SettingNullException; 174 import com.ivata.groupware.admin.setting.Settings; 175 import com.ivata.groupware.admin.setting.SettingsDataTypeException; 176 import com.ivata.groupware.container.PicoContainerFactory; 177 import com.ivata.groupware.container.persistence.hibernate.HibernateSetupConstants; 178 import com.ivata.mask.MaskFactory; 179 import com.ivata.mask.persistence.FinderException; 180 import com.ivata.mask.util.StringHandling; 181 import com.ivata.mask.util.SystemException; 182 import com.ivata.mask.web.browser.Browser; 183 import com.ivata.mask.web.browser.BrowserConstants; 184 import com.ivata.mask.web.struts.MaskAction; 185 import com.ivata.mask.web.struts.MaskAuthenticator; 186 import com.ivata.mask.web.tag.theme.ThemeConstants; 187 188 197 public class LoginAction extends MaskAction { 198 private Security security; 199 private Settings settings; 200 205 private Logger log = Logger.getLogger(LoginAction.class); 206 207 216 public LoginAction(Security security, Settings settings, 217 MaskFactory maskFactory, MaskAuthenticator authenticator) { 218 super(maskFactory, authenticator); 219 this.security = security; 220 this.settings = settings; 221 } 222 223 224 240 public ActionForward execute(final ActionMapping mapping, 241 final ActionForm form, 242 final HttpServletRequest request, 243 final HttpServletResponse response) throws Exception { 244 setLogin(true); 246 return super.execute(mapping, form, request, response); 247 } 248 249 272 public String execute(final ActionMapping mapping, 273 final ActionErrors errors, 274 final ActionForm formParam, 275 final HttpServletRequest request, 276 final HttpServletResponse response, 277 final HttpSession session) throws SystemException { 278 ActionForm form = formParam; 279 String forward = null; 280 String help = null; 281 String javaScriptVersion = null; 282 String jndiPrefix = null; 283 String login = null; 284 String password = null; 285 286 String userName = null; 287 SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession"); 288 289 String errorForward = "login"; 291 if ((form == null) 292 || (securitySession == null)) { 293 return "loginGuestAction"; 294 } 295 296 PicoContainerFactory factory = PicoContainerFactory.getInstance(); 300 assert (factory != null); 301 PicoContainer container = factory.getGlobalContainer(); 302 assert (container != null); 303 Properties hibernateProperties = (Properties ) container 304 .getComponentInstance("hibernateProperties"); 305 assert (hibernateProperties != null); 306 Boolean setupConfirmed = (Boolean ) servlet.getServletContext() 307 .getAttribute(HibernateSetupConstants.CONFIRM_ATTRIBUTE); 308 String uRL = hibernateProperties.getProperty( 309 HibernateSetupConstants 310 .HIBERNATE_PROPERTY_DATABASE_URL); 311 assert (uRL != null); 312 if (HibernateSetupConstants.AUTOMATIC_DATABASE_MEMORY_URL 313 .equals(uRL) 314 && !Boolean.TRUE.equals(setupConfirmed)) { 315 return "setupAction"; 316 } 317 318 try { 319 forward = (String ) PropertyUtils.getSimpleProperty(form, "forward"); 320 help = (String ) PropertyUtils.getSimpleProperty(form, "help"); 321 javaScriptVersion = (String ) PropertyUtils.getSimpleProperty(form, "javaScriptVersion"); 322 jndiPrefix = (String ) PropertyUtils.getSimpleProperty(form, "jndiPrefix"); 323 login = (String ) PropertyUtils.getSimpleProperty(form, "login"); 324 password = (String ) PropertyUtils.getSimpleProperty(form, "password"); 325 userName = ((String ) PropertyUtils.getSimpleProperty(form, "userName")).toLowerCase(); 326 } catch (InvocationTargetException e) { 327 throw new SystemException(e); 328 } catch (IllegalAccessException e) { 329 throw new SystemException(e); 330 } catch (NoSuchMethodException e) { 331 form = RequestUtils.createActionForm(request, mapping, mapping.getModuleConfig(), servlet); 333 } 334 if (securitySession == null) { 336 return "loginGuestAction"; 337 } 338 339 String defaultForward = ""; 340 UserDO user = null; 342 if (!StringHandling.isNullOrEmpty(userName)) { 343 try { 344 if (log.isDebugEnabled()) { 345 log.debug("Looking for entity for user name '" + userName + "'"); 346 } 347 user = security.findUserByName(securitySession, userName); 348 defaultForward = settings.getStringSetting(securitySession, 349 "siteDefaultForward", 350 user); 351 } catch (SettingsDataTypeException e) { 352 throw new SystemException(e); 353 } catch (SettingNullException e) { 354 throw new SystemException(e); 355 } catch (FinderException e) { 356 log.warn("No user found called '" + userName + "'", e); 357 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("login.error.login")); 358 login = null; 360 } 361 } 362 String siteTheme = request.getParameter("theme"); 364 boolean changeTheme = true; 366 if ("last".equals(siteTheme)) { 367 changeTheme = false; 368 siteTheme = settings.getStringSetting(securitySession, 369 "siteTheme", user); 370 } 371 session.setAttribute(ThemeConstants.ATTRIBUTE_THEME_NAME, siteTheme); 372 if(StringHandling.isNullOrEmpty(login)) { 375 return "login"; 376 } 377 378 if (StringHandling.isNullOrEmpty(forward)) { 380 forward = defaultForward; 381 } 382 383 try { 384 PropertyUtils.setSimpleProperty(form, "forward", forward); 385 } catch (NoSuchMethodException e) { 386 form = RequestUtils.createActionForm(request, mapping, mapping.getModuleConfig(), servlet); 388 try { 389 PropertyUtils.setSimpleProperty(form, "forward", forward); 390 } catch (IllegalAccessException e1) { 391 e1.printStackTrace(); 392 } catch (InvocationTargetException e1) { 393 e1.printStackTrace(); 394 } catch (NoSuchMethodException e1) { 395 e1.printStackTrace(); 396 } 397 return null; 398 } catch (InvocationTargetException e) { 399 throw new SystemException(e); 400 } catch (IllegalAccessException e) { 401 throw new SystemException(e); 402 } 403 Browser browser = null; 404 405 if ((javaScriptVersion != null) && 407 javaScriptVersion.trim().equals("")) { 408 javaScriptVersion = null; 409 } 410 browser = new Browser(request.getHeader("User-Agent"), javaScriptVersion); 411 session.setAttribute("browser", browser); 412 try { 414 if (log.isInfoEnabled()) { 415 log.info("Logging in user '" + userName + "' for real (not guest)."); 416 } 417 session.setAttribute("securitySession", securitySession = 418 security.login(user, password)); 419 } catch (Exception e) { 420 String logPassword = "**********"; 422 try { 423 securitySession = security.loginGuest(); 424 Boolean debugPassword = settings.getBooleanSetting( 425 securitySession, 426 "siteLoginDebugPassword", null); 427 if ((debugPassword != null) 428 && debugPassword.booleanValue()) { 429 logPassword = password; 430 } 431 } catch (Exception e1) { 432 log.error("Failed to access setting 'siteLoginDebugPassword'.", 434 e1); 435 } 436 log.warn ("Failed to login user '" 437 + userName 438 + "', password '" 439 + logPassword 440 + "'", e); 441 errors.add(ActionMessages.GLOBAL_MESSAGE, 442 new ActionMessage("login.error.login")); 443 } 444 if (javaScriptVersion == null) { 448 if (log.isInfoEnabled()) { 449 log.info("Javascript is unavailable on user's browser."); 450 } 451 errorForward = "loginJavaScript"; 454 Integer browserType = browser.getType(); 455 String errorKey; 456 457 if (BrowserConstants.TYPE_INTERNET_EXPLORER.equals(browserType)) { 458 if (browser.getVersion().compareTo("5") >= 0) { 459 if (log.isDebugEnabled()) { 460 log.debug("Identified IE5 browser."); 461 } 462 errorKey = "login.error.javascript.iexplore5"; 464 } else { 465 if (log.isDebugEnabled()) { 466 log.debug("Identified IE browser."); 467 } 468 errorKey = "login.error.javascript.iexplore"; 469 } 470 } else if (BrowserConstants.TYPE_MOZILLA.equals(browserType) || 471 BrowserConstants.TYPE_NETSCAPE.equals(browserType)) { 472 if (log.isDebugEnabled()) { 473 log.debug("Identified Netscape/Mozilla browser."); 474 } 475 errorKey = "login.error.javascript.netscape"; 476 } else if (BrowserConstants.TYPE_OPERA.equals(browserType)) { 477 if (browser.getVersion().compareTo("6") >= 0) { 478 errorKey = "login.error.javascript.opera6"; 480 if (log.isDebugEnabled()) { 481 log.debug("Identified Opera 6 browser."); 482 } 483 } else { 484 errorKey = "login.error.javascript.opera"; 485 if (log.isDebugEnabled()) { 486 log.debug("Identified Opera browser."); 487 } 488 } 489 } else { 490 errorKey = "login.error.javascript.unknown"; 491 if (log.isDebugEnabled()) { 492 log.debug("Could not identify browser."); 493 } 494 } 495 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(errorKey)); 496 } 497 if (!errors.isEmpty()) { 499 if (log.isInfoEnabled()) { 500 log.info("Errors found for user '" + userName + "'."); 501 } 502 return errorForward; 503 } 504 if (log.isInfoEnabled()) { 505 log.info("Login successful for user '" + userName + "'."); 506 } 507 session.setAttribute("userName", userName); 509 request.setAttribute("loginForward", forward); 510 511 if (changeTheme) { 514 settings.amendSetting(securitySession, "siteTheme", siteTheme, 515 user); 516 } 517 return forward; 518 } 519 } 520 | Popular Tags |