1 25 package org.ofbiz.webapp.control; 26 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import javax.servlet.ServletRequest ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpSession ; 34 import javax.servlet.jsp.PageContext ; 35 import javax.transaction.Transaction ; 36 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.UtilFormatOut; 39 import org.ofbiz.base.util.UtilMisc; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.transaction.GenericTransactionException; 44 import org.ofbiz.entity.transaction.TransactionUtil; 45 46 53 public class LoginWorker { 54 55 public final static String module = LoginWorker.class.getName(); 56 57 public static final String EXTERNAL_LOGIN_KEY_ATTR = "externalLoginKey"; 58 59 60 public static Map externalLoginKeys = new HashMap (); 61 62 public static String makeLoginUrl(PageContext pageContext) { 63 return makeLoginUrl(pageContext, "checkLogin"); 64 } 65 66 public static String makeLoginUrl(ServletRequest request) { 67 return makeLoginUrl(request, "checkLogin"); 68 } 69 70 public static String makeLoginUrl(PageContext pageContext, String requestName) { 71 return makeLoginUrl(pageContext.getRequest(), requestName); 72 } 73 public static String makeLoginUrl(ServletRequest request, String requestName) { 74 String queryString = null; 75 76 Enumeration parameterNames = request.getParameterNames(); 77 78 while (parameterNames != null && parameterNames.hasMoreElements()) { 79 String paramName = (String ) parameterNames.nextElement(); 80 81 if (paramName != null) { 82 if (queryString == null) queryString = paramName + "=" + request.getParameter(paramName); 83 else queryString = queryString + "&" + paramName + "=" + request.getParameter(paramName); 84 } 85 } 86 87 String loginUrl = "/" + requestName + "/" + UtilFormatOut.checkNull((String ) request.getAttribute("_CURRENT_VIEW_")); 88 89 if (queryString != null) loginUrl = loginUrl + "?" + UtilFormatOut.checkNull(queryString); 90 91 return loginUrl; 92 } 93 94 97 public static String getExternalLoginKey(HttpServletRequest request) { 98 GenericValue userLogin = (GenericValue) request.getAttribute("userLogin"); 100 101 String externalKey = (String ) request.getAttribute(EXTERNAL_LOGIN_KEY_ATTR); 102 if (externalKey != null) return externalKey; 103 104 HttpSession session = request.getSession(); 105 synchronized (session) { 106 String sesExtKey = (String ) session.getAttribute(EXTERNAL_LOGIN_KEY_ATTR); 108 if (sesExtKey != null) { 109 externalLoginKeys.remove(sesExtKey); 110 } 111 112 if (userLogin == null) return ""; 114 115 while (externalKey == null || externalLoginKeys.containsKey(externalKey)) { 117 externalKey = "EL" + Long.toString(Math.round(Math.random() * 1000000)) + Long.toString(Math.round(Math.random() * 1000000)); 118 } 119 120 request.setAttribute(EXTERNAL_LOGIN_KEY_ATTR, externalKey); 121 session.setAttribute(EXTERNAL_LOGIN_KEY_ATTR, externalKey); 122 externalLoginKeys.put(externalKey, userLogin); 123 return externalKey; 124 } 125 } 126 127 public static void cleanupExternalLoginKey(HttpSession session) { 128 String sesExtKey = (String ) session.getAttribute(EXTERNAL_LOGIN_KEY_ATTR); 129 if (sesExtKey != null) { 130 externalLoginKeys.remove(sesExtKey); 131 } 132 } 133 134 public static void setLoggedOut(String userLoginId, GenericDelegator delegator) { 135 Transaction parentTx = null; 136 boolean beganTransaction = false; 137 138 try { 139 try { 140 parentTx = TransactionUtil.suspend(); 141 } catch (GenericTransactionException e) { 142 Debug.logError(e, "Cannot suspend current transaction: " + e.getMessage(), module); 143 } 144 145 try { 146 beganTransaction = TransactionUtil.begin(); 147 148 GenericValue userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userLoginId)); 149 userLogin.set("hasLoggedOut", "Y"); 150 userLogin.store(); 151 } catch (GenericEntityException e) { 152 String errMsg = "Unable to set logged out flag on UserLogin"; 153 Debug.logError(e, errMsg, module); 154 try { 155 TransactionUtil.rollback(beganTransaction, errMsg, e); 156 } catch (GenericTransactionException e2) { 157 Debug.logError(e2, "Could not rollback nested transaction: " + e.getMessage(), module); 158 } 159 } finally { 160 try { 161 TransactionUtil.commit(beganTransaction); 162 } catch (GenericTransactionException e) { 163 Debug.logError(e, "Could not commit nested transaction: " + e.getMessage(), module); 164 } 165 } 166 } finally { 167 if (parentTx != null) { 169 try { 170 TransactionUtil.resume(parentTx); 171 Debug.logVerbose("Resumed the parent transaction.", module); 172 } catch (GenericTransactionException ite) { 173 Debug.logError(ite, "Cannot resume transaction: " + ite.getMessage(), module); 174 } 175 } 176 } 177 } 178 } 179 | Popular Tags |