001: package org.contineo.web;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005:
006: import org.contineo.core.FileBean;
007: import org.contineo.core.security.User;
008: import org.contineo.core.security.dao.UserDAO;
009:
010: import org.contineo.util.Context;
011: import org.contineo.util.config.SettingsConfig;
012:
013: import org.contineo.web.i18n.Messages;
014: import org.contineo.web.navigation.NavigationBean;
015: import org.contineo.web.navigation.PageContentBean;
016: import org.contineo.web.util.Constants;
017:
018: import java.util.Enumeration;
019: import java.util.Locale;
020: import java.util.Map;
021:
022: import javax.faces.context.FacesContext;
023:
024: import javax.servlet.http.HttpSession;
025:
026: /**
027: * Executes user login
028: *
029: * @author Marco Meschieri
030: * @version $Id: LoginForm.java,v 1.9 2007/10/16 16:10:34 marco Exp $
031: * @since 3.0.0.0
032: */
033: public class LoginForm {
034: protected static Log logger = LogFactory.getLog(LoginForm.class);
035: private String j_password;
036: private String language;
037: private String j_username;
038: private NavigationBean navigation;
039:
040: /**
041: * Login handler
042: *
043: * @return "success" if the user was succesfully authenticated
044: */
045: public String login() {
046: UserDAO userDao = (UserDAO) Context.getInstance().getBean(
047: UserDAO.class);
048:
049: if (userDao.validateUser(j_username, j_password)) {
050: logger.info("User " + j_username + " logged in.");
051:
052: FacesContext facesContext = FacesContext
053: .getCurrentInstance();
054: Map session = facesContext.getExternalContext()
055: .getSessionMap();
056:
057: session.put(Constants.AUTH_USERNAME, j_username);
058:
059: // Gets language option pressed
060: if (language.equals("default")) {
061: User user = userDao.findByPrimaryKey(j_username);
062: language = user.getLanguage();
063: }
064:
065: session.put(Constants.LANGUAGE, language);
066:
067: Locale locale = new Locale(language, "");
068: session.put(Constants.LOCALE, locale);
069: facesContext.getViewRoot().setLocale(locale);
070:
071: // Show the home page
072: PageContentBean content = new PageContentBean("home",
073: "home");
074: content.setContentTitle("Home");
075: content.setDisplayText("Home");
076: content.setIcon(StyleBean.getImagePath("home.png"));
077: navigation.setSelectedPanel(content);
078:
079: return "loginSuccess";
080: } else {
081: logger.warn("User " + j_username + " is not valid.");
082: //Messages.addError(Messages.getString("errors.action.password.mismatch"));
083: Messages.addError("Invalid username or password");
084:
085: return "loginFailure";
086: }
087: }
088:
089: /**
090: * Logout handler
091: *
092: * @return "login" if the user was succesfully logged off
093: */
094: public String logout() {
095: FacesContext facesContext = FacesContext.getCurrentInstance();
096: HttpSession session = (HttpSession) facesContext
097: .getExternalContext().getSession(false);
098:
099: try {
100: String authUsername = SessionManagement.getUsername();
101: SettingsConfig conf = (SettingsConfig) Context
102: .getInstance().getBean(SettingsConfig.class);
103: FileBean.deleteDir(conf.getValue("userdir") + "/"
104: + authUsername + "/temp");
105:
106: logger.info("User " + authUsername + " logged out.");
107:
108: Enumeration enumeration = session.getAttributeNames();
109:
110: while (enumeration.hasMoreElements()) {
111: session.removeAttribute((String) enumeration
112: .nextElement());
113: }
114:
115: // session.invalidate();
116: } catch (Exception e) {
117: logger.error(e.getMessage(), e);
118: }
119:
120: return "login";
121: }
122:
123: /**
124: * @return the j_password
125: */
126: public String getJ_password() {
127: return j_password;
128: }
129:
130: /**
131: * @param j_password the j_password to set
132: */
133: public void setJ_password(String j_password) {
134: this .j_password = j_password;
135: }
136:
137: /**
138: * @return the language
139: */
140: public String getLanguage() {
141: return language;
142: }
143:
144: /**
145: * @param language the language to set
146: */
147: public void setLanguage(String language) {
148: this .language = language;
149: }
150:
151: /**
152: * @return the j_username
153: */
154: public String getJ_username() {
155: return j_username;
156: }
157:
158: /**
159: * @param j_username the j_username to set
160: */
161: public void setJ_username(String j_username) {
162: this .j_username = j_username;
163: }
164:
165: public void setNavigation(NavigationBean navigation) {
166: this.navigation = navigation;
167: }
168: }
|