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

Quick Search    Search Deep

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


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/BelongsToTag.java,v 1.13 2003/09/30 15:13:16 joe Exp $
2    * $Revision: 1.13 $
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.bean.User;
33  import com.RuntimeCollective.webapps.bean.UserGroup;
34  import com.RuntimeCollective.webapps.bean.UserGroupType;
35  import com.RuntimeCollective.webapps.RuntimeParameters;
36  import com.RuntimeCollective.webapps.bean.LoginCookie;
37  import com.RuntimeCollective.webapps.bean.SearchResult;
38  import com.RuntimeCollective.webapps.IndexedEntityBeanStore;
39  
40  import java.io.IOException;
41  import java.lang.reflect.Method;
42  import java.util.StringTokenizer;
43  import java.util.Enumeration;
44  import java.util.Iterator;
45  import java.util.Set;
46  import javax.servlet.http.HttpSession;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.jsp.JspException;
49  import javax.servlet.jsp.JspWriter;
50  import javax.servlet.jsp.PageContext;
51  import javax.servlet.jsp.tagext.TagSupport;
52  import javax.servlet.ServletContext;
53  
54  import org.apache.struts.action.Action;
55  import org.apache.struts.action.ActionError;
56  import org.apache.struts.action.ActionErrors;
57  import org.apache.struts.util.MessageResources;
58  import org.apache.struts.util.RequestUtils;
59  
60  /**
61   * Evaluate the body of this tag only if the user specified by name, property and scope (or the currently logged-on user, if name is not specified) is a member (or not!) of the given group, or is a member of any group with the given group type.
62   * <p>This tag takes the following optional parameters.  Either <code>group</code> or <code>groupType</code> must be specified:
63   * <ul>
64   * <li><code>name</code> - specifies the attribute name of the bean whose property is accessed to retrieve the value specified by <code>property</code> (if specified). If <code>property</code> is not specified, the value of this bean itself will specify the user.  If <code>name</code> is not specified, the currently logged-on user will be used.
65   * <li><code>property</code> - specifies the name of the property to be accessed on the bean specified by <code>name</code>. This value may be a simple, indexed, or nested property reference expression. If not specified, the bean identified by <code>name</code> will itself specify the user. If the specified property returns null, an Exception will be thrown.
66   * <li><code>scope</code> - specifies the variable scope searched to retrieve the bean specified by <code>name</code>.  If not specified, the default rules applied by <code>PageContext.findAttribute()</code> are applied.
67   * <li><code>group</code> - if set then the tag will only admit users who are members of the UserGroup with this name.
68   * <li><code>groupType</code> - if set then the tag will only admit users who are members of any group which has this UserGroupType.
69   * <li><code>notIn</code> - if you want to check that the user is NOT in the group / group type. Defaults to false.
70   * </ul>
71   *
72   * <p>An example of how to use the <code>groups</code> parameter to allow only members of the Administrators or Editors group:</p>
73   * <p><code>&lt;rs:belongsTo groups='&lt;%= new String[]{&quot;Administrators&quot;, &quot;Editors&quot;} %&gt;'&gt;</code></p>
74   *
75   * @see com.RuntimeCollective.webapps.bean.UserGroup
76   * @see com.RuntimeCollective.webapps.bean.UserGroupType
77   * @version $Id: BelongsToTag.java,v 1.13 2003/09/30 15:13:16 joe Exp $
78   */
79  public class BelongsToTag extends TagSupport {
80  
81      // ------------------------------------------------------------- Properties
82  
83      /** The name of the group that the user must be a member of [Optional] */
84      private String iGroup; 
85  
86      /** The names of the groups, at least one of which the user must be a member of [Optional] */
87      private String[] iGroups; 
88  
89      /** The user must be a member of at least one group that has this group type [Optional] */
90      private String iGroupType; 
91  
92      /** Whether the user should be in or not [Optional] */
93      private boolean iNotIn = false; 
94  
95      /** Get the name of the group that the user must be a member of [Optional] */
96      public String getGroup() { return iGroup; }
97  
98      /** Set the name of the group that the user must be a member of [Optional] */
99      public void setGroup(String group) { iGroup = group; }
100 
101     /** Get the names of the groups, at least one of which the user user must be a member of [Optional] */
102     public String[] getGroups() { return iGroups; }
103 
104     /** Set the names of the groups, at least one of which the user user must be a member of [Optional] */
105     public void setGroups(String[] groups) { iGroups = groups; }
106 
107     /** Get the name of the group type containing a group that the user must be a member of [Optional] */
108     public String getGroupType() { return iGroupType; }
109 
110     /** Set the name of the group type containing a group that the user must be a member of [Optional] */
111     public void setGroupType(String groupType) { iGroupType = groupType; }
112 
113     /** Get whether the user should be in or not [Optional] */
114     public boolean getNotIn() { return iNotIn; }
115 
116     /** Set whether the user should be in or not [Optional] */
117     public void setNotIn(boolean notIn) { iNotIn = notIn; }
118 
119    /**
120      * Name of the bean that contains the user to check groups for.
121      */
122     protected String name = null;
123 
124     public String getName() {
125         return (this.name);
126     }
127 
128     public void setName(String name) {
129         this.name = name;
130     }
131 
132     /**
133      * Name of the property to be accessed on the specified bean.
134      */
135     protected String property = null;
136 
137     public String getProperty() {
138         return (this.property);
139     }
140 
141     public void setProperty(String property) {
142         this.property = property;
143     }
144 
145     /**
146      * The scope to be searched to retrieve the specified bean.
147      */
148     protected String scope = null;
149 
150     public String getScope() {
151         return (this.scope);
152     }
153 
154     public void setScope(String scope) {
155         this.scope = scope;
156     }
157 
158 
159     // --------------------------------------------------------- Public Methods
160 
161 
162     /**
163      * Check if the user specified by name, property and scope (or the session user,
164      * if name is not specified) belongs to the appropriate group/group type,
165      * and evaluate the body of this tag if they do.
166      *
167      * @exception JspException if a JSP exception occurs
168      */
169     public int doStartTag() throws JspException {
170 
171   // Check if name has been set.  If so, we'll try and get a user from a bean.
172   // If not, we'll use the session user.
173   User user;
174   if (name != null) {
175       // Get user from bean.
176       Object value = RequestUtils.lookup(pageContext, name, property, scope);
177       if (value == null) {
178     throw new JspException("No user found using name="+name+", property="+property+" in scope "+scope);
179       } else {
180     user = (User) value;
181       }
182   } else {
183       // Use session user.
184       RuntimeParameters.logDebug(this, "BelongsTo: getting the user from the session");
185       user = CheckLogonTag.getLoggedOnUser((HttpServletRequest)pageContext.getRequest(), pageContext.getSession());
186   }
187   
188         if ((iNotIn && !checkBelongs(user)) || (!iNotIn && checkBelongs(user))) {
189             return (EVAL_BODY_INCLUDE);
190         } else {
191             return (SKIP_BODY);
192   }
193     }
194 
195 
196     /**
197      * Evaluate the remainder of the current page normally.
198      *
199      * @exception JspException if a JSP exception occurs
200      */
201     public int doEndTag() throws JspException {
202         return (EVAL_PAGE);
203     }
204 
205     /**
206      * Release all allocated resources.
207      */
208     public void release() {
209         super.release();
210         iGroup = null;
211         iGroups = null;
212   iGroupType = null;
213   iNotIn = false;
214   name = null;
215   property = null;
216   scope = null;
217     }
218 
219 
220     // --------------------------------------------------------- Private Methods
221     
222     /**
223      * If the group attribute is set, check the user is in the specified group.
224      * <p>Otherwise, if the groupType attribute is set, check the user is in at least one
225      * group of the given type.
226      */
227      private boolean checkBelongs(User user) throws JspException {
228   // Is the user a member of the appropriate group?
229   boolean valid = false;
230 
231   // Do we have a user?
232   if (user != null) {
233       // check either the group or group type
234       if (getGroup()!=null) {
235             UserGroup group = RuntimeParameters.getUserGroups().getForName( getGroup() );
236             valid = group.contains( user );
237       } else if (getGroups() != null) {
238             for (int i=0; i<getGroups().length; i++) {
239                 UserGroup group = RuntimeParameters.getUserGroups().getForName( getGroups()[i] );
240                 valid = valid || ((group != null) && (group.contains(user)));
241             }
242       } else if (getGroupType()!=null) {
243             UserGroupType type = UserGroupType.getForName( getGroupType() );
244             if (type != null) {
245                 Iterator allGroups = RuntimeParameters.getUserGroups().getAllGroups( type ).iterator();
246                 UserGroup group;
247                 SearchResult result;
248                 while (valid == false && allGroups.hasNext()) {
249                     result = (SearchResult) allGroups.next();
250                     group = (UserGroup) RuntimeParameters.getStore().get("com.RuntimeCollective.webapps.bean.UserGroup", result.getId());
251                     valid = group.contains( user );
252                 }
253             }
254       } 
255   }
256   return valid;
257      }
258 
259 }
260