Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/tag/CheckEditTag.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/CheckEditTag.java,v 1.7 2003/09/30 15:13:16 joe Exp $
2    * $Revision: 1.7 $
3    * $Date: 2003/09/30 15:13:16 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.tag;
31  
32  import com.RuntimeCollective.webapps.RuntimeParameters;
33  import com.RuntimeCollective.webapps.bean.User;
34  import com.RuntimeCollective.webapps.bean.PermissionBean;
35  import com.RuntimeCollective.webapps.bean.EntityBean;
36  
37  import java.sql.SQLException;
38  import java.io.IOException;
39  import javax.servlet.http.HttpSession;
40  import javax.servlet.jsp.JspException;
41  import javax.servlet.jsp.JspWriter;
42  import javax.servlet.jsp.PageContext;
43  import javax.servlet.jsp.tagext.TagSupport;
44  import org.apache.struts.action.Action;
45  import org.apache.struts.action.ActionError;
46  import org.apache.struts.action.ActionErrors;
47  import org.apache.struts.util.MessageResources;
48  
49  /**
50   *
51   * Check if a user can edit the named session-scoped bean.  This bean must implement the PermissionBean interface.
52   *
53   * If they do not have permission to edit this bean, control will be forwarded to a page that will display errors (defaults to /logon.jsp).
54   *
55   * <p> By default, the User on the session under RuntimeParameters.get("logonUserKey") will be used.  A different User bean on the session
56   * can be used by specifying the "user" parameter.
57   * <p>
58   * This tag assumes the user is logged on; this tag should be used after checkLogon (unless "user" is specified).
59   * <p>
60   * Attributes:
61   * <ul>
62   * <li> name - The name of the bean to check edit permissions for</li>
63   * <li> user - [optional] The name of the user bean to check permissions with - defaults to  RuntimeParameters.get("logonUserKey")</li>
64   * <li> page - the page to go to if the user is not logged in (defaults to /logon.jsp)</li>
65   * </ul>
66   * <p>
67   * For example, inserting
68   * <br><code>&lt;%@ taglib uri="/WEB-INF/runtime-struts.tld" prefix="rs" %&gt;
69   * <br>&lt;rs:checkEdit name="com.RuntimeCollective.school.bean.Course"/&gt;
70   * <br></code>
71   * <br> into a jsp page will check that the user in the session under <code>RuntimeParameters.get("logonUserKey")</code> can edit the <code>com.RuntimeCollective.school.bean.Course</code> bean.
72   *
73   * <p> The following errors are returned
74   * <ul>
75   * <li><code>error.permission.editDenied</code>
76   * <li><code>error.permission.nullValues</code>
77   * <li><code>error.db.connection</code>
78   * <li><code>error.permission.nullSession</code>
79   * </ul>
80   * @author Joe Holmberg
81   * @version $Id: CheckEditTag.java,v 1.7 2003/09/30 15:13:16 joe Exp $
82   */
83  
84  public final class CheckEditTag extends TagSupport {
85  
86  
87      /** The key of the session-scope bean we check permissions for. */
88      private String name = "";
89  
90      /** The page to which we should forward for the user to log on. Defaults to "/logon.jsp"*/
91      private String page = "/logon.jsp";
92  
93      /** The key of the user to check permissions for. Defaults to <code>RuntimeParameters.get("logonUserKey")</code> */
94      private String user = RuntimeParameters.get("logonUserKey");
95  
96      /** Return the bean name. */
97      public String getName() {
98    return (this.name);
99      }
100 
101     /** Set the bean name.
102      * @param name The new bean name
103      */
104     public void setName(String name) {
105   this.name = name;
106     }
107 
108     /** Return the forward page. */
109     public String getPage() {
110   return (this.page);
111     }
112 
113     /** Set the forward page.
114      * @param page The new forward page
115      */
116     public void setPage(String page) {
117   this.page = page;
118     }
119 
120     /** Return the user. */
121     public String getUser() { return this.user; }
122 
123     /** Set the user. */
124     public void setUser(String user) { this.user = user; }
125 
126 
127     /** Defer our checking until the end of this tag is encountered.
128      * @exception JspException if a JSP exception has occurred
129      */
130     public int doStartTag() throws JspException {
131   return (SKIP_BODY);
132     }
133 
134     /**
135 
136      * Perform a permissions check by calling the specified bean's 
137      * <code>canEdit</code> method, with the current user.
138      * If either of these beans are blank, or there is no session,
139      * or the user does not have permissions to access that bean,
140      * control will be forwarded to the specified error page
141      * with an appropriate error message set.
142      * @exception JspException if a JSP exception has occurred
143      */
144     public int doEndTag() throws JspException {
145 
146   ActionErrors errors = new ActionErrors();
147 
148   // Does the user have permission?
149   HttpSession session = pageContext.getSession();
150   if (session!=null) {
151       User userBean = (User) session.getAttribute(user);
152       if (userBean != null) {
153     userBean = (User)RuntimeParameters.getStore().get(User.class.getName(), userBean.getId());
154       }
155       PermissionBean perm = (PermissionBean) session.getAttribute(name);
156       try {
157     if (userBean != null && perm != null) {
158         RuntimeParameters.logDebug( this, "user id="+userBean.getId()+" and bean id="+( (EntityBean) perm).getId() );
159         if ( !perm.canEdit(userBean) ) errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.permission.editDenied"));
160     } else {
161         RuntimeParameters.logWarn(this,"Unable to find user and permission bean for name="+name);
162         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.permission.nullValues"));
163     }
164       } catch ( SQLException e ) {
165     RuntimeParameters.logError(this,"problem connecting to db",e);
166     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.db.connection"));
167       }
168   } else {
169       errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.permission.nullSession"));
170   }
171 
172   // Forward control based on results
173   if (errors.size() == 0)
174       return (EVAL_PAGE);
175   else {
176       // Put the errors on the request
177       pageContext.getRequest().setAttribute(Action.ERROR_KEY, errors);
178 
179       try {
180     pageContext.forward(page);
181       } catch (Exception e) {
182     throw new JspException(e.toString());
183       }
184       return (SKIP_PAGE);
185   }
186     }
187 
188 
189     /** Release any acquired resources. */
190     public void release() {
191         super.release();
192         this.name = "";
193         this.page = "/logon.jsp";
194   this.user = RuntimeParameters.get("logonUserKey");
195     }
196 }
197 
198