KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > interceptors > SecurityInterceptor


1 /*
2  * Copyright (C) 2003 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.interceptors;
21
22 import com.opensymphony.xwork.*;
23 import com.opensymphony.xwork.interceptor.Interceptor;
24 import com.opensymphony.xwork.interceptor.component.ComponentManager;
25
26 import org.efs.openreports.ORStatics;
27 import org.efs.openreports.objects.ReportUser;
28 import org.efs.openreports.util.LocalStrings;
29
30 public class SecurityInterceptor implements Interceptor
31 {
32     private boolean loggedIn;
33     private ReportUser user;
34     
35     public String JavaDoc intercept(ActionInvocation actionInvocation) throws Exception JavaDoc
36     {
37         ComponentManager container =
38             (ComponentManager) ActionContext.getContext().get(
39                 "com.opensymphony.xwork.interceptor.component.ComponentManager");
40
41         if (container != null)
42         {
43             container.initializeObject(this);
44         }
45
46         user = (ReportUser) actionInvocation.getInvocationContext().getSession().get(
47                 "user");
48         
49         if (!isAuthenticated(user))
50         {
51             ActionSupport action = (ActionSupport) actionInvocation.getAction();
52             action.addActionError(LocalStrings.getString(LocalStrings.ERROR_NOTLOGGEDIN));
53
54             return Action.LOGIN;
55         }
56         
57         if (!isAuthorized(user))
58         {
59             ActionSupport action = (ActionSupport) actionInvocation.getAction();
60             action.addActionError(LocalStrings.getString(LocalStrings.ERROR_NOTAUTHORIZED));
61             
62             return ORStatics.NOT_AUTHORIZED;
63         }
64
65         ActionContext.getContext().getValueStack().push(this);
66
67         return actionInvocation.invoke();
68     }
69
70     protected boolean isAuthenticated(ReportUser user)
71     {
72         if (user == null)
73         {
74             loggedIn = false;
75             return false;
76         }
77
78         loggedIn = true;
79         return true;
80     }
81
82     protected boolean isAuthorized(ReportUser user)
83     {
84         return true;
85     }
86     
87     public void destroy()
88     {
89     }
90
91     public void init()
92     {
93     }
94
95     public boolean isLoggedIn()
96     {
97         return loggedIn;
98     }
99     
100     public ReportUser getUser()
101     {
102         return user;
103     }
104 }
105
Popular Tags