// idega 2000 - Tryggvi Larusson - Grimur Jonsson /* * Copyright 2000 idega.is All Rights Reserved. */ package is.idega.idegaweb.golf.block.login.business; import is.idega.idegaweb.golf.access.AccessControl; import is.idega.idegaweb.golf.access.LoginTable; import is.idega.idegaweb.golf.access.LoginTableHome; import is.idega.idegaweb.golf.entity.Member; import is.idega.idegaweb.golf.entity.MemberHome; import java.io.IOException; import java.sql.SQLException; import javax.ejb.CreateException; import javax.ejb.FinderException; import com.idega.core.accesscontrol.business.LoginBusinessBean; import com.idega.data.IDOLookup; import com.idega.event.IWPageEventListener; import com.idega.idegaweb.IWException; import com.idega.presentation.IWContext; import com.idega.user.data.User; /** * Title: LoginBusiness Description: Copyright: Copyright (c) 2000-2001 idega.is * All Rights Reserved Company: idega * * @author <a href="mailto:gimmi@idega.is">Grimur Jonsson </a>, <a * href="mailto:tryggvi@idega.is">Tryggvi Larusson </a> * @version 1.1 */ public class LoginBusiness extends LoginBusinessBean implements IWPageEventListener { public static String UserAttributeParameter = AccessControl.USER_ATTRIBUTE_PARAMETER; public static String UserAccessAttributeParameter = "member_access"; public LoginBusiness() { // default constructor } // this method must be implemented in this class otherwise the static method getControlActionValue // of the super class is used! (inheritance problem of static methods in java) public static boolean isLogOnAction(IWContext iwc) { return "login".equals(getControlActionValue(iwc)); } // this method must be implemented in this class otherwise the static method getControlActionValue // of the super class is used! (inheritance problem of static methods in java) public static boolean isLogOffAction(IWContext iwc) { return "logoff".equals(getControlActionValue(iwc)); } // this method must be implemented in this class otherwise the static method getControlActionValue // of the super class is used! (inheritance problem of static methods in java) protected static boolean isTryAgainAction(IWContext iwc) { return "tryagain".equals(getControlActionValue(iwc)); } // this method uses a different parameter than the super method! private static String getControlActionValue(IWContext iwc) { return iwc.getParameter(GolfLoginBusiness.LoginStateParameterForGolf); } public static boolean isLoggedOn(IWContext modinfo) { if (modinfo.getSessionAttribute(UserAttributeParameter) == null) { return false; } return true; } public static void internalSetState(IWContext modinfo, String state) { modinfo.setSessionAttribute(GolfLoginBusiness.LoginStateParameterForGolf, state); } public static String internalGetStateString(IWContext modinfo) { return (String) modinfo.getSessionAttribute(GolfLoginBusiness.LoginStateParameterForGolf); } public boolean actionPerformed(IWContext modinfo) throws IWException { //System.out.println("LoginBusiness.actionPerformed"); try { if (isLoggedOn(modinfo)) { String controlParameter = modinfo.getParameter(GolfLoginBusiness.LoginStateParameterForGolf); if (controlParameter != null) { if (controlParameter.equals("logoff")) { logOut(modinfo); internalSetState(modinfo, "loggedoff"); } } } else { String controlParameter = modinfo.getParameter(GolfLoginBusiness.LoginStateParameterForGolf); if (controlParameter != null) { if (controlParameter.equals("login")) { boolean canLogin = false; if ((modinfo.getParameter("login") != null) && (modinfo.getParameter("password") != null)) { canLogin = verifyPassword(modinfo, modinfo.getParameter("login"), modinfo.getParameter("password")); if (canLogin) { isLoggedOn(modinfo); internalSetState(modinfo, "loggedon"); } else { internalSetState(modinfo, "loginfailed"); } } } else if (controlParameter.equals("tryagain")) { internalSetState(modinfo, "loggedoff"); } } } } catch (Exception ex) { ex.printStackTrace(System.err); //throw (IdegaWebException)ex.fillInStackTrace(); return false; } return true; } public boolean isAdmin(IWContext modinfo) throws SQLException { return AccessControl.isAdmin(modinfo); } private boolean verifyPassword(IWContext modinfo, String login, String password) throws IOException, SQLException { boolean returner = false; LoginTable[] login_table = (LoginTable[]) ((LoginTable) IDOLookup.instanciateEntity(LoginTable.class)).findAllByColumnEquals("user_login", login); MemberHome mh = ((MemberHome) IDOLookup.getHomeLegacy(Member.class)); for (int i = 0; i < login_table.length; i++) { if (login_table[i].getUserPassword().equals(password)) { try { Member member = mh.findByPrimaryKey(login_table[i].getMemberId()); modinfo.getSession().setAttribute(UserAttributeParameter, member); returner = true; User user = member.getICUser(); if(user!=null) { try { logIn(modinfo,user); } catch (Exception e) { e.printStackTrace(); } } break; } catch (FinderException fe) { throw new SQLException(fe.getMessage()); } } } if(!returner) { //New login boolean newLogin = logInUser(modinfo,login,password); if(newLogin) { try { Member m = mh.findMemberByIWMemberSystemUser(modinfo.getCurrentUser()); modinfo.setSessionAttribute(UserAttributeParameter,m); } catch (FinderException e) { e.printStackTrace(); } } } if (isAdmin(modinfo)) { modinfo.getSession().setAttribute(UserAccessAttributeParameter, "admin"); } return returner; } protected void logOut(IWContext modinfo) throws Exception { //System.out.print("inside logOut"); try { super.logOut(modinfo); } catch (Exception e1) { e1.printStackTrace(); } try { super.logOut(modinfo); } catch (Exception e) { e.printStackTrace(); } modinfo.removeSessionAttribute(UserAttributeParameter); if (modinfo.getSessionAttribute(UserAccessAttributeParameter) != null) { modinfo.removeSessionAttribute(UserAccessAttributeParameter); } modinfo.removeSessionAttribute("member_id"); } public static boolean registerMemberLogin(int member_id, String user_login, String user_pass_one, String user_pass_two) throws SQLException { boolean returner = false; if (user_pass_one.equals(user_pass_two)) { LoginTable[] logTable = (LoginTable[]) ((LoginTable) IDOLookup.instanciateEntity(LoginTable.class)).findAllByColumnEquals("USER_LOGIN", user_login); if (logTable.length == 0) { try { LoginTable logT = ((LoginTableHome) IDOLookup.getHomeLegacy(LoginTable.class)).create(); logT.setMemberId(member_id); logT.setUserLogin(user_login); logT.setUserPassword(user_pass_one); logT.insert(); } catch (CreateException ce) { throw new SQLException(ce.getMessage()); } returner = true; } else if (logTable.length == 1) { if (logTable[0].getMemberId() == member_id) { logTable[0].setMemberId(member_id); logTable[0].setUserLogin(user_login); logTable[0].setUserPassword(user_pass_one); logTable[0].update(); returner = true; } } else { returner = false; } } if (returner) { } return returner; } }