1 19 20 package za.org.coefficient.modules.user; 21 22 import net.sf.hibernate.Hibernate; 23 import net.sf.hibernate.HibernateException; 24 import net.sf.hibernate.type.Type; 25 26 import za.org.coefficient.authentication.CoefficientUser; 27 import za.org.coefficient.core.Constants; 28 import za.org.coefficient.interfaces.CoefficientContext; 29 import za.org.coefficient.modules.BaseModule; 30 import net.sf.hibernate.util.HibernateUtil; 31 import za.org.coefficient.util.ejb.SecurityUtil; 32 import za.org.coefficient.util.ejb.VelocityScreenUtil; 33 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 37 import org.apache.commons.httpclient.Cookie; 38 39 53 public class Security extends BaseModule { 54 56 public String getMainMethod() { 57 return "loginPrompt"; 58 } 59 60 public String getModuleDescription() { 61 return "This is the module that handles site security"; 62 } 63 64 public String getModuleDisplayName() { 65 return "User Information"; 66 } 67 68 public CoefficientContext login(CoefficientContext ctx) { 69 ctx.invalidateSession(); 71 72 CoefficientUser user = null; 75 76 String password = ctx.getParameter("password"); 77 String username = ctx.getParameter("username"); 78 try { 79 if (!ctx.getParameterAsBoolean("hashedPassword")) { 80 password = new String (SecurityUtil.md5AsHexString(password)); 81 } 82 ArrayList users = 83 new ArrayList (HibernateUtil.find("from " 84 + CoefficientUser.class.getName() 85 + " as pe_user where pe_user.userName = ?" 86 + " and pe_user.password = ? and pe_user.active = ?", 87 new Object [] { username, password, new Boolean (true) }, 88 new Type[] { 89 Hibernate.STRING, Hibernate.STRING, 90 Hibernate.BOOLEAN 91 })); 92 if (users.size() != 1) { 93 ctx.setError("Incorrect username/password"); 94 } else { 95 user = (CoefficientUser) users.get(0); 96 } 97 } catch (HibernateException he) { 98 he.printStackTrace(); 99 } 100 101 ctx.setSessionAttribute(Constants.USER_SESSION_STRING, user); 102 103 if (!ctx.isError()) { 104 String rememberMe = ctx.getParameter("rememberMe"); 105 if (rememberMe != null) { 106 Cookie usernameCookie = new Cookie(ctx.getRequestURL(), 108 "coefficient_username", username); 109 Cookie passwordCookie = new Cookie(ctx.getRequestURL(), 110 "coefficient_password", password); 111 usernameCookie.setExpiryDate(new java.util.Date (System.currentTimeMillis() + Integer.MAX_VALUE)); 112 passwordCookie.setExpiryDate(new java.util.Date (System.currentTimeMillis() + Integer.MAX_VALUE)); 113 ctx.setCookie(usernameCookie); 114 ctx.setCookie(passwordCookie); 115 } 116 if (!ctx.getParameterAsBoolean("hashedPassword")) { 117 ctx.setForward("security", "loginSuccess"); 118 } 119 } 120 return ctx; 121 } 122 123 public CoefficientContext loginPrompt(CoefficientContext ctx) { 124 HashMap map = new HashMap (); 125 map.put("module", this); 126 map.put("curr_module", ctx.getParameter("module")); 127 map.put("curr_op", ctx.getParameter("op")); 128 StringBuffer sb = null; 129 if (ctx.getCurrentUser() == null) { 130 sb = VelocityScreenUtil.getProcessedScreen("loginPrompt.vm", map); 131 } else { 132 map.put("currentUser", ctx.getCurrentUser()); 133 sb = VelocityScreenUtil.getProcessedScreen("loginDisplayInfo.vm", 134 map); 135 } 136 137 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 139 return ctx; 140 } 141 142 public CoefficientContext loginSuccess(CoefficientContext ctx) { 143 ctx.setModuleContent("login successful!", "Login"); 144 return ctx; 145 } 146 147 public CoefficientContext logout(CoefficientContext ctx) { 148 ctx.invalidateSession(); 149 Cookie usernameCookie = new Cookie(ctx.getRequestURL(), "coefficient_username", ""); 150 Cookie passwordCookie = new Cookie(ctx.getRequestURL(), "coefficient_password", ""); 151 usernameCookie.setExpiryDate(new java.util.Date (System.currentTimeMillis())); 152 passwordCookie.setExpiryDate(new java.util.Date (System.currentTimeMillis())); 153 ctx.setCookie(usernameCookie); 154 ctx.setCookie(passwordCookie); 155 if (!ctx.isError()) { 156 ctx.setForward("security", "logoutSuccess"); 157 } 158 return ctx; 159 } 160 161 public CoefficientContext logoutSuccess(CoefficientContext ctx) { 162 ctx.setModuleContent("logout successful!", "Logout"); 163 return ctx; 164 } 165 } 166 | Popular Tags |