KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > interceptor > LoginAdminInterceptor


1 package org.javabb.interceptor;
2
3 import org.javabb.infra.UserContext;
4 import org.javabb.transaction.UserTransaction;
5 import org.javabb.vo.User;
6
7 import com.opensymphony.xwork.Action;
8 import com.opensymphony.xwork.ActionContext;
9 import com.opensymphony.xwork.ActionInvocation;
10 import com.opensymphony.xwork.interceptor.AroundInterceptor;
11
12 /*
13  * Copyright 2004 JavaFree.org
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */

27
28 /**
29  * $Id: LoginAdminInterceptor.java,v 1.8.8.1.2.2 2006/04/17 17:47:08 daltoncamargo Exp $
30  *
31  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org
32  * </a> <br>
33  * @author Ronald Tetsuo Miura
34  */

35 public class LoginAdminInterceptor extends AroundInterceptor {
36
37     private UserTransaction userTransaction;
38
39     /**
40      * @param userTransaction
41      * the new userTransaction value
42      */

43     public void setUserTransaction(UserTransaction userTransaction) {
44         this.userTransaction = userTransaction;
45     }
46
47     /**
48      * @param invocation
49      * @throws Exception
50      * @see com.opensymphony.xwork.interceptor.AroundInterceptor#before(com.opensymphony.xwork.ActionInvocation)
51      */

52     protected void before(ActionInvocation invocation) throws Exception JavaDoc {
53
54     }
55
56     /**
57      * @param invocation
58      * @param result
59      * @throws Exception
60      * @see com.opensymphony.xwork.interceptor.AroundInterceptor#after(com.opensymphony.xwork.ActionInvocation,
61      * java.lang.String)
62      */

63     protected void after(ActionInvocation invocation, String JavaDoc result)
64             throws Exception JavaDoc {
65     }
66
67     /**
68      * @param invocation
69      * @return result
70      * @throws Exception
71      * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation)
72      */

73     public String JavaDoc intercept(ActionInvocation invocation) throws Exception JavaDoc {
74         boolean loggedIn = false;
75         User user = null;
76         ActionContext ctx = ActionContext.getContext();
77         user = UserContext.getContext().getUser();
78         if ((user != null) && (user.getUser() != null)
79                 && (user.getAdmin() != null)
80                 && (user.getAdmin().intValue() == 1)) {
81             // Check if the user is the same of there in database.
82
user = userTransaction.getUser(user.getId());
83             if ((user.getAdmin() != null) && (user.getAdmin().intValue() == 1)) {
84                 loggedIn = true;
85             } else {
86                 loggedIn = false;
87             }
88         } else {
89             loggedIn = false;
90         }
91
92         if (loggedIn == false) {
93             // Logging
94
if (UserContext.getContext().isAuthenticated()) {
95                 log.debug("Action name: " + ctx.getName());
96                 log.debug("Admin Denied: "
97                         + UserContext.getContext().getUser().getUser());
98             }
99
100             // Envia o usuário para a tela de login
101
return Action.LOGIN;
102         }
103
104         // Usuário logado, deixa ele acessar o link desejado
105
String JavaDoc result = invocation.invoke();
106         after(invocation, result);
107
108         return result;
109     }
110 }
Popular Tags