KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > IfMemberOfGroup


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  * IfMemberOfGroup.java
67  *
68  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.ext.taglib;
71
72 import com.jcorporate.expresso.core.misc.CurrentLogin;
73 import com.jcorporate.expresso.core.security.User;
74 import com.jcorporate.expresso.services.dbobj.GroupMembers;
75
76 import javax.servlet.http.HttpServletRequest JavaDoc;
77 import javax.servlet.http.HttpServletResponse JavaDoc;
78 import javax.servlet.jsp.JspTagException JavaDoc;
79 import java.util.StringTokenizer JavaDoc;
80
81
82 /**
83  * This tag checks the currently logged in user for membership into
84  * a specified security group.
85  *
86  * @author Shash Chatterjee
87  */

88 public class IfMemberOfGroup
89         extends ExpressoTagSupport {
90     private boolean inverse = false;
91     private String JavaDoc groupname = "";
92     private String JavaDoc delimiter = "";
93
94     /**
95      * The public constructor.
96      */

97     public IfMemberOfGroup() {
98         super();
99     }
100
101     /**
102      * No special handling at end of tag
103      *
104      * @return int as defined by the JSP taglib spec
105      * @throws JspTagException as per defined by the spec signature
106      */

107     public int doEndTag()
108             throws javax.servlet.jsp.JspTagException JavaDoc {
109         return EVAL_PAGE;
110     }
111
112     /**
113      * The tag implementation is right here...
114      *
115      * @return int as defined by the JSP taglib spec
116      * @throws JspTagException as per defined by the spec signature
117      */

118     public int doStartTag()
119             throws javax.servlet.jsp.JspTagException JavaDoc {
120
121         boolean isMember = false;
122         String JavaDoc userName = "";
123
124         try {
125             // Get the current user from the servlet session
126
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
127             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
128 // CheckLogin chk = CheckLogin.getInstance();
129
// chk.checkLogin(request, response);
130
CurrentLogin myLogin = (CurrentLogin) pageContext.getSession().getAttribute(CurrentLogin.LOGIN_KEY);
131             if (myLogin != null) {
132                 // Extract relevant parameters needed to lookup group membership
133
int uid = myLogin.getUid();
134                 userName = myLogin.getUserName();
135
136                 String JavaDoc currentDB = myLogin.getDBName();
137
138                 // Check group membership
139
// NOTE: User:Admin is blindly given membership in every group
140
if (User.isAdmin(userName)) {
141                     isMember = true;
142                 } else if (!"".equals(delimiter)) {
143                     StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(groupname, delimiter);
144                     while (stok.hasMoreTokens()) {
145                         String JavaDoc oneGroup = stok.nextToken();
146                         GroupMembers gm = new GroupMembers(User.getAdminId(currentDB));
147                         gm.setDataContext(currentDB);
148                         gm.setField("ExpUid", uid);
149                         gm.setField("GroupName", oneGroup);
150
151                         if (gm.find()) {
152                             isMember = true;
153                             break;
154                         }
155                     }
156                 } else {
157                     GroupMembers gm = new GroupMembers(User.getAdminId(currentDB));
158                     gm.setDataContext(currentDB);
159                     gm.setField("ExpUid", uid);
160                     gm.setField("GroupName", groupname);
161
162                     if (gm.find()) {
163                         isMember = true;
164                     }
165                 }
166             }
167         } catch (Exception JavaDoc e) {
168             throw new JspTagException JavaDoc("Exception:" +
169                     e.getMessage());
170         }
171
172         // If not logged in....
173
if (userName.equals("") || User.isUnknownUser(userName)) {
174             isMember = false;
175         }
176
177         // Check if reverse logic was specified (i.e. NOT memberOf)
178
if (inverse) {
179             isMember = !isMember;
180         }
181
182         // Process rest of tag if user is a member of group, skip it otherwise.
183
if (isMember) {
184             return EVAL_BODY_INCLUDE;
185         } else {
186             return SKIP_BODY;
187         }
188     }
189
190     /**
191      * Getter for inverse member
192      *
193      * @return boolean
194      */

195     public boolean getInverse() {
196         return inverse;
197     }
198
199     /**
200      * Setter for inverse.
201      *
202      * @param newInverse If set, a reverse logic lookup is performed.
203      */

204     public void setInverse(boolean newInverse) {
205         inverse = newInverse;
206     }
207
208     /**
209      * Getter for groupname member
210      *
211      * @return String
212      */

213     public String JavaDoc getGroupname() {
214         return groupname;
215     }
216
217     /**
218      * Setter for groupname
219      *
220      * @param name The group name to check membership for
221      */

222     public void setGroupname(String JavaDoc name) {
223         groupname = name;
224     }
225
226     /**
227      * Getter for delimiter
228      *
229      * @return Returns the delimiter.
230      */

231     public String JavaDoc getDelimiter() {
232         return delimiter;
233     }
234
235     /**
236      * Setter for delimiter
237      *
238      * @param seperator The delimiter to set.
239      */

240     public void setDelimiter(String JavaDoc delimiter) {
241         this.delimiter = delimiter;
242     }
243 } /* IfLoggedIn */
244
Popular Tags