KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > ITrackerAction


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.net.*;
24 import java.util.*;
25 import javax.ejb.*;
26 import javax.rmi.*;
27 import javax.naming.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import org.apache.commons.beanutils.*;
32 import org.apache.struts.action.*;
33 import org.apache.struts.util.*;
34
35 import cowsultants.itracker.ejb.client.interfaces.*;
36 import cowsultants.itracker.ejb.client.models.*;
37 import cowsultants.itracker.ejb.client.util.*;
38 import cowsultants.itracker.web.util.*;
39
40 public abstract class ITrackerAction extends Action {
41     protected static boolean allowSaveLogin = true;
42
43     static {
44         try {
45             InitialContext ic = new InitialContext();
46             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
47             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
48             SystemConfiguration sc = scHome.create();
49
50             allowSaveLogin = sc.getBooleanProperty("allow_save_login", true);
51         } catch(CreateException ce) {
52         } catch(NamingException ne) {
53         }
54     }
55
56     protected boolean hasPermission(int[] permissionsNeeded, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
57         if(isLoggedIn(request, response)) {
58             HttpSession session = request.getSession(false);
59             HashMap permissions = (session == null ? null : (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY));
60             if(! UserUtilities.hasPermission(permissions, permissionsNeeded)) {
61                 return false;
62             }
63             return true;
64         }
65         return false;
66     }
67
68     protected boolean hasPermission(int permissionNeeded, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
69         if(isLoggedIn(request, response)) {
70             HttpSession session = request.getSession(false);
71             HashMap permissions = (session == null ? null : (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY));
72             if(! UserUtilities.hasPermission(permissions, permissionNeeded)) {
73                 return false;
74             }
75             return true;
76         }
77         return false;
78     }
79
80     protected boolean isLoggedIn(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
81         HttpSession session = request.getSession(false);
82         UserModel user = (session == null ? null : (UserModel) session.getAttribute(Constants.USER_KEY));
83         String JavaDoc login = (user == null ? null : user.getLogin());
84
85         if(login == null || "".equals(login)) {
86             LoginUtilities.checkAutoLogin(request, allowSaveLogin);
87             return false;
88         }
89         return true;
90     }
91
92     public String JavaDoc getBaseURL(HttpServletRequest request) {
93         return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
94     }
95
96 }
97
Popular Tags