KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > IsUserInRoleTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
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
17 package org.apache.taglibs.request;
18
19 import java.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>isuserinrole</b>, used to determine if HttpServletRequest
27  * is for an authenticated user in a role.
28  * <p>
29  * Includes the body of the tag if an authenticated user in a role.
30  * <p>
31  * Requires that the attribute <b>role</b> be set.
32  * <p>
33  * You can set the optional tag attribute <b>value</b> to <i>true</i> or
34  * <i>false</i>. The body of the tag is included if isuserinrole matches
35  * the value.
36  * <p>
37  * JSP Tag Lib Descriptor
38  * <p><pre>
39  * &lt;name&gt;isuserinrole&lt;/name&gt;
40  * &lt;tagclass&gt;org.apache.taglibs.request.IsUserInRoleTag&lt;/tagclass&gt;
41  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
42  * &lt;info&gt;Test whether the remote user is in a role.&lt;/info&gt;
43  * &lt;attribute&gt;
44  * &lt;name&gt;role&lt;/name&gt;
45  * &lt;required&gt;yes&lt;/required&gt;
46  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
47  * &lt;/attribute&gt;
48  * &lt;attribute&gt;
49  * &lt;name&gt;value&lt;/name&gt;
50  * &lt;required&gt;false&lt;/required&gt;
51  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
52  * &lt;/attribute&gt;
53  * </pre>
54  *
55  * @author Glenn Nielsen
56  */

57
58 public class IsUserInRoleTag extends TagSupport
59 {
60     private boolean value = true;
61     private String JavaDoc role = null;
62
63     /**
64      * Determines whether remote user is in a role.
65      *
66      * @return SKIP_BODY if isuserinrole doesn't match value, EVAL_BODY_include if isuserinrole matches value
67      */

68     public final int doStartTag() throws JspException
69     {
70     boolean result = ((HttpServletRequest)pageContext.getRequest()).isUserInRole(role);
71
72     if( value == result )
73         return EVAL_BODY_INCLUDE;
74
75     return SKIP_BODY;
76     }
77
78     /**
79      * Set the optional tag attribute <b>value</b> to true or false.
80      *
81      * @param boolean true or false
82      */

83     public final void setValue(boolean value)
84     {
85     this.value = value;
86     }
87
88     /**
89      * Set the required tag attribute <b>role</b> to the name
90      * of the role you wish to test remote user for.
91      *
92      * @param String role name
93      */

94     public final void setRole(String JavaDoc str)
95     {
96     role = str;
97     }
98 }
99
Popular Tags