01: package org.contineo.web;
02:
03: import org.contineo.core.security.User;
04: import org.contineo.core.security.dao.UserDAO;
05:
06: import org.contineo.util.Context;
07:
08: import org.contineo.web.util.Constants;
09:
10: import java.util.Map;
11:
12: import javax.faces.context.FacesContext;
13:
14: import javax.servlet.http.HttpSession;
15:
16: /**
17: *
18: * @author Michael Scholz
19: */
20: public class SessionManagement {
21: public static boolean isValid(HttpSession session) {
22: boolean result = true;
23: String username = (String) session
24: .getAttribute(Constants.AUTH_USERNAME);
25:
26: if ((username == null) || username.equals("")) {
27: result = false;
28: }
29:
30: if (session.isNew()) {
31: result = false;
32: }
33:
34: return result;
35: }
36:
37: public static boolean isValid() {
38: FacesContext facesContext = FacesContext.getCurrentInstance();
39: HttpSession session = (HttpSession) facesContext
40: .getExternalContext().getSession(false);
41:
42: return isValid(session);
43: }
44:
45: public static String getUsername() {
46: FacesContext facesContext = FacesContext.getCurrentInstance();
47: Map session = facesContext.getExternalContext().getSessionMap();
48: String username = (String) session.get(Constants.AUTH_USERNAME);
49:
50: return username;
51: }
52:
53: public static User getUser() {
54: String username = getUsername();
55: UserDAO dao = (UserDAO) Context.getInstance().getBean(
56: UserDAO.class);
57:
58: return dao.findByPrimaryKey(username);
59: }
60:
61: public static String getLanguage() {
62: FacesContext facesContext = FacesContext.getCurrentInstance();
63: Map session = facesContext.getExternalContext().getSessionMap();
64: String language = (String) session.get(Constants.LANGUAGE);
65:
66: return language;
67: }
68: }
|