KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > jee > taglib > HasPrincipal


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.jee.taglib;
29
30
31
32
33 import java.security.Principal JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Set JavaDoc;
39
40 import javax.security.auth.Subject JavaDoc;
41 import javax.servlet.jsp.JspException JavaDoc;
42 import javax.servlet.jsp.JspTagException JavaDoc;
43 import javax.servlet.jsp.jstl.core.ConditionalTagSupport;
44
45 import net.sf.jguard.core.CoreConstants;
46 import net.sf.jguard.core.principals.RolePrincipal;
47 import net.sf.jguard.ext.principals.PrincipalUtils;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
52
53
54 /**
55  * display the jsp fragment if the Subject has got this Principal/role.
56  * principals are divided by ';' character, which are divided with ',' to include
57  * multiple strings to build the principal.
58  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
59  *
60  */

61 public class HasPrincipal extends ConditionalTagSupport {
62
63     /** Logger for this class */
64     private static final Log logger = LogFactory.getLog(HasPrincipal.class);
65      /**
66      * serial version id.
67      */

68     private static final long serialVersionUID = 3257284721280235318L;
69     private String JavaDoc principals;
70     private List JavaDoc principalsArray;
71     private static final String JavaDoc ALL ="ALL";
72     private static final String JavaDoc ANY ="ANY";
73     private static final String JavaDoc NONE ="NONE";
74
75     //default setting is ANY
76
private String JavaDoc operator = ANY;
77     private Class JavaDoc defaultClassName = RolePrincipal.class;
78     private Class JavaDoc clazz = defaultClassName;
79     private Class JavaDoc[] defaultParameterTypes = new Class JavaDoc[]{String JavaDoc.class,String JavaDoc.class};
80     private Class JavaDoc[] parameterTypes = defaultParameterTypes;
81     private String JavaDoc applicationName;
82
83
84     /**
85      * @param strUri
86      */

87     public void setPrincipals(String JavaDoc strUri) {
88               principals = strUri;
89               principalsArray = Arrays.asList(principals.split(","));
90     }
91
92
93     /**
94      * allow or not to display jsp content;depends on user's principalsArray (Principals).
95      * @return true if tag displays content when user has got the specified role(principal); false otherwise
96      * @see javax.servlet.jsp.jstl.core.ConditionalTagSupport#condition()
97      */

98     protected boolean condition() throws JspTagException JavaDoc {
99
100         try {
101             this.principals=(String JavaDoc)ExpressionEvaluatorManager.evaluate ("principalsArray", this.principals, String JavaDoc.class, this, pageContext);
102             principalsArray = Arrays.asList(principals.split(","));
103         } catch (JspException JavaDoc e1) {
104             logger.error("condition()", e1);
105             throw new JspTagException JavaDoc(e1.getMessage());
106         }
107         if(logger.isDebugEnabled()){
108             logger.debug("<jguard:authorized> tag uri="+principals);
109             logger.debug("<jguard:authorized> tag operator="+operator);
110         }
111
112         Subject JavaDoc subject = TagUtils.getSubject(this.pageContext);
113         if(subject == null){
114             return false;
115         }
116
117         Set JavaDoc principals = subject.getPrincipals();
118         for(int j=0;j<principalsArray.size();j++){
119             Principal JavaDoc ppal = null;
120             if(clazz.getName().equals(RolePrincipal.class.getName())){
121                 List JavaDoc args = new ArrayList JavaDoc();
122                 args.add(principalsArray.get(j));
123                 
124                 //we always add the applicationName for RolePrincipal
125
if(applicationName == null){
126                     applicationName = (String JavaDoc)pageContext.getServletContext().getAttribute(CoreConstants.APPLICATION_NAME);
127                 }
128                 args.add(applicationName);
129                  ppal = PrincipalUtils.getPrincipal(this.clazz,this.parameterTypes,args.toArray());
130             }else{
131                  ppal = PrincipalUtils.getPrincipal(this.clazz,this.parameterTypes,new Object JavaDoc[]{((String JavaDoc)this.principalsArray.get(j)).split(";")});
132             }
133
134             if (ppal == null){
135                 logger.warn(" wrong arguments in the HasPrincipal tag \n class="+clazz.getName()+"\n parameterTypes="+parameterTypes+"\n principalsArray="+principalsArray);
136                 return false;
137             }
138
139             if(!principals.contains(ppal)){
140                 if(operator.equals(ALL)){
141                   return false;
142                 }
143             }else{
144                 Iterator JavaDoc it = principals.iterator();
145                 boolean active = false;
146                 while(it.hasNext()){
147                     Principal JavaDoc principal = (Principal JavaDoc)it.next();
148                     if(ppal.equals(principal)&& ((RolePrincipal)principal).isActive()){
149                         active = true;
150                         break;
151                     }
152                 }
153                 if(active==false){
154                     return false;
155                 }
156                 //principals contains principalsArray[j]
157
if(operator.equals(ANY)){
158                     return true;
159                 }else if(operator.equals(NONE)){
160                     return false;
161                 }
162                 return false;
163             }
164         }
165
166
167         if(operator.equals(ALL) ||operator.equals(NONE) ){
168             return true;
169         }else if (operator.equals(ANY)){
170             return false;
171         }
172
173         return false;
174
175     }
176
177
178     /**
179  * @return Returns the principals.
180  */

181 public String JavaDoc getPrincipals() {
182     return principals;
183 }
184 /**
185  * @return Returns the operator.
186  */

187 public String JavaDoc getOperator() {
188     return operator;
189 }
190 /**
191  * @param operator The operator to set.
192  */

193 public void setOperator(String JavaDoc operator) {
194     String JavaDoc upper = operator.toUpperCase();
195       if(upper.equals(ALL)||upper.equals(ANY)||upper.equals(NONE)){
196       this.operator = upper;
197     }
198 }
199
200
201 public final Class JavaDoc getClazz() {
202     return clazz;
203 }
204
205
206 public final void setClassName(String JavaDoc className) {
207     try {
208         this.clazz = Class.forName(className);
209     } catch (ClassNotFoundException JavaDoc e) {
210         logger.info(" 'className' attribute does not map to an existing or reachable class ");
211     }
212 }
213
214
215 public final Class JavaDoc[] getParameterTypes() {
216     return parameterTypes;
217 }
218
219
220 public String JavaDoc getApplicationName() {
221     return applicationName;
222 }
223
224
225 public void setApplicationName(String JavaDoc applicationName) {
226     this.applicationName = applicationName;
227 }
228
229 }
230
Popular Tags