KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > logic > IsUserInRoleTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.logic;
17
18 import com.blandware.atleap.webapp.util.core.WebappUtil;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.struts.taglib.TagUtils;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
28 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collection JavaDoc;
32
33 /**
34  * <p>Body of this tag will be rendered if and only if user is in specified role
35  * or in a collection of roles.
36  * </p>
37  * <p>You can specify comma-separated list of roles in <b>roles</b> attribute just as
38  * you can provide instance of <code>java.util.Collection</code> and <code>java.lang.Object[]</code>
39  * as collection of roles or beans which have the property with name of the role.
40  * All of these cases will be correctly handled.
41  * </p>
42  * <p>
43  * If <code>var</code> attribute specified, tag body is ignored, and only
44  * boolean result of check is assigned to given variable.
45  * </p>
46  * <p>
47  * This tag has two modes: if <b>all</b> attribute is <code>false</code> (or not
48  * specified), then user passes check if he/she has at least one role from
49  * <b>roles</b>. If <b>all</b> attribute is <code>true</code>, then user must
50  * have all roles from <b>roles</b> attribute to pass check.
51  * </p>
52  * <p>
53  * Allowed attributes are:
54  * <ul>
55  * <li>
56  * <b>roles</b> - required - defines a collection of roles that are allowed to
57  * access body of this tag (see above)
58  * </li>
59  * <li>
60  * <b>property</b> - name of property of beans in collection (if there are beans)
61  * that contains role name
62  * </li>
63  * <li>
64  * <b>all</b> - whether the user need to have all the roles from supplied
65  * collection to pass check, or not. This can be "true" or "false", default is
66  * "false".
67  * </li>
68  * <li>
69  * <b>var</b> - name of scope variable to which result of check (allowed or not)
70  * will be saved. If specified, tag body is ignored.
71  * </li>
72  * <li>
73  * <b>scope</b> - scope of variable to which the result will be saved. If <b>var</b>
74  * is omitted, this attribute is ignored.
75  * </li>
76  * </ul>
77  * </p>
78  * <p><a HREF="IsUserInRoleTag.java.htm"><i>View Source</i></a></p>
79  *
80  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
81  * @version $Revision: 1.1 $ $Date: 2005/09/27 10:16:59 $
82  * @jsp.tag name="isUserInRole"
83  * body-content="scriptless"
84  */

85 public class IsUserInRoleTag extends SimpleTagSupport JavaDoc {
86
87     protected transient final Log log = LogFactory.getLog(IsUserInRoleTag.class);
88
89     /**
90      * Collection of roles. Body of tag will be rendered if and only if
91      * user belongs to at least one role in specified collection, except of
92      * cases when <code>all</code> attribute is set to <em>true</em>.
93      */

94     protected Object JavaDoc roles;
95
96     /**
97      * If role collection is specified and this attribute is set to true,
98      * body of tag will be rendered if user belongs to all roles in collection.
99      * If roles is omitted, setting this attribute will have no effect.
100      */

101     protected Boolean JavaDoc all;
102
103     /**
104      * Name of property of beans in collection (if there are beans) that contains role name.
105      */

106     protected String JavaDoc property;
107
108     /**
109      * Name of variable to export result of check (<code>Boolean.TRUE</code> or <code>Booolean.FALSE</code>)
110      */

111     protected String JavaDoc var;
112
113     /**
114      * Scope to export variable to. By default variable (if <code>var</code> attribute
115      * is specified) will be exported to page scope.
116      */

117     protected String JavaDoc scope;
118
119     /**
120      * Returns roles that are allowed to view tag content
121      *
122      * @return roles
123      * @see #roles
124      * @jsp.attribute required="true"
125      * rtexprvalue="true"
126      * type="java.lang.Object"
127      * description="Collection of roles"
128      */

129     public Object JavaDoc getRoles() {
130         return roles;
131     }
132
133     /**
134      * Sets roles that are allowed to view tag content
135      *
136      * @param roles roles to set
137      * @see #roles
138      */

139     public void setRoles(Object JavaDoc roles) {
140         this.roles = roles;
141     }
142
143     /**
144      * Returns whether user must have all roles to gain access
145      *
146      * @return whether user must have all roles to gain access
147      * @see #all
148      * @jsp.attribute required="false"
149      * rtexprvalue="true"
150      * type="java.lang.Boolean"
151      * description="Whether or not user must belong to all roles in collection"
152      */

153     public Boolean JavaDoc getAll() {
154         return all;
155     }
156
157     /**
158      * Sets whether user must have all roles to gain access
159      *
160      * @param all whether user must have all roles to gain access
161      * @see #all
162      */

163     public void setAll(Boolean JavaDoc all) {
164         this.all = all;
165     }
166
167     /**
168      * Returns property name
169      *
170      * @return property name
171      * @see #property
172      * @jsp.attribute required="false"
173      * rtexprvalue="true"
174      * type="java.lang.String"
175      * description="Property of beans in collection that contains role name"
176      */

177     public String JavaDoc getProperty() {
178         return property;
179     }
180
181     /**
182      * Sets property name
183      *
184      * @param property property name to set
185      * @see #property
186      *
187      */

188     public void setProperty(String JavaDoc property) {
189         this.property = property;
190     }
191
192     /**
193      * Returns name of variable which will accept result of check
194      *
195      * @return name of variable
196      * @see #var
197      * @jsp.attribute required="false"
198      * rtexprvalue="true"
199      * type="java.lang.String"
200      * description="Name of variable to export result of check"
201      */

202     public String JavaDoc getVar() {
203         return var;
204     }
205
206     /**
207      * Sets name of variable which will accept result of check
208      *
209      * @param var name of variable to set
210      * @see #var
211      */

212     public void setVar(String JavaDoc var) {
213         this.var = var;
214     }
215
216     /**
217      * Returns variable scope
218      *
219      * @return variable scope
220      * @see #scope
221      * @jsp.attribute required="false"
222      * rtexprvalue="true"
223      * type="java.lang.String"
224      * description="Scope to export variable to"
225      */

226     public String JavaDoc getScope() {
227         return scope;
228     }
229
230     /**
231      * Sets variable scope
232      *
233      * @param scope variable scope to set
234      * @see #scope
235      */

236     public void setScope(String JavaDoc scope) {
237         this.scope = scope;
238     }
239
240     /**
241      * Processes the tag
242      *
243      * @throws JspException
244      * @throws IOException
245      */

246     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
247
248         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
249
250         if ( all == null ) {
251             all = Boolean.FALSE;
252         }
253
254         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
255
256         Collection JavaDoc rolesCollection = null;
257         if ( roles instanceof String JavaDoc ) {
258             rolesCollection = Arrays.asList(((String JavaDoc) roles).split(","));
259         } else if ( roles instanceof Collection JavaDoc ) {
260             rolesCollection = ((Collection JavaDoc) roles);
261         } else if ( roles instanceof Object JavaDoc[] ) {
262             rolesCollection = Arrays.asList((Object JavaDoc[]) roles);
263         } else {
264             String JavaDoc errorMessage = "Only instances of 'java.util.Collection', " +
265                     "'java.lang.Object[]' and 'java.lang.String' are supported in 'roles' attribute";
266             JspTagException JavaDoc e = new JspTagException JavaDoc(errorMessage);
267             throw e;
268         }
269
270         boolean isUserInRole = false;
271         try {
272             isUserInRole = WebappUtil.isUserInRole(rolesCollection, all.booleanValue(), property, request);
273         } catch ( Exception JavaDoc e ) {
274             JspTagException JavaDoc ex = new JspTagException JavaDoc(e);
275             throw ex;
276         }
277
278         if ( var != null ) {
279             int varScope = PageContext.PAGE_SCOPE;
280             if ( scope != null ) {
281                 varScope = TagUtils.getInstance().getScope(scope);
282             }
283             pageContext.setAttribute(var, Boolean.valueOf(isUserInRole), varScope);
284         } else if ( isUserInRole ) {
285             JspFragment JavaDoc body = getJspBody();
286             if ( body != null ) {
287                 body.invoke(null);
288             }
289         }
290     }
291 }
292
Popular Tags