Source Code Cross Referenced for LabelBasedAclVoter.java in  » Security » acegi-security » org » acegisecurity » vote » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » acegi security » org.acegisecurity.vote 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002:         *
003:         * Licensed under the Apache License, Version 2.0 (the "License");
004:         * you may not use this file except in compliance with the License.
005:         * You may obtain a copy of the License at
006:         *
007:         *     http://www.apache.org/licenses/LICENSE-2.0
008:         *
009:         * Unless required by applicable law or agreed to in writing, software
010:         * distributed under the License is distributed on an "AS IS" BASIS,
011:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:         * See the License for the specific language governing permissions and
013:         * limitations under the License.
014:         */
015:        package org.acegisecurity.vote;
016:
017:        import org.acegisecurity.Authentication;
018:        import org.acegisecurity.ConfigAttribute;
019:        import org.acegisecurity.ConfigAttributeDefinition;
020:
021:        import org.aopalliance.intercept.MethodInvocation;
022:
023:        import org.apache.commons.logging.Log;
024:        import org.apache.commons.logging.LogFactory;
025:
026:        import org.springframework.util.Assert;
027:
028:        import java.util.Iterator;
029:        import java.util.List;
030:        import java.util.Vector;
031:        import java.util.Map;
032:
033:        /**
034:         * <p>This Acl voter will evaluate methods based on labels applied to incoming arguments. It will only check
035:         * methods that have been properly tagged in the MethodSecurityInterceptor with the value stored in
036:         * <tt>attributeIndicatingLabeledOperation</tt>. If a method has been tagged, then it examines each argument, and if the
037:         * argument implements {@link LabeledData}, then it will asses if the user's list of granted authorities matches.
038:         * </p>
039:         *
040:         * <p>By default, if none of the arguments are labeled, then the access will be granted. This can be overridden by
041:         * setting <tt>allowAccessIfNoAttributesAreLabeled</tt> to false in the Spring context file.</p>
042:         *
043:         * <p>In many situations, different values are linked together to define a common label, it is necessary to
044:         * define a map in the application context that links user-assigned label access to domain object labels. This is done
045:         * by setting up the <tt>labelMap</tt> in the application context.</p>
046:         *
047:         * @author Greg Turnquist
048:         * @version $Id: LabelBasedAclVoter.java 1962 2007-08-27 17:21:16Z luke_t $
049:         *
050:         * @see org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor
051:         */
052:        public class LabelBasedAclVoter extends AbstractAclVoter {
053:            //~ Static fields/initializers =====================================================================================
054:
055:            private static final Log logger = LogFactory
056:                    .getLog(LabelBasedAclVoter.class);
057:
058:            //~ Instance fields ================================================================================================
059:
060:            private Map labelMap = null;
061:            private String attributeIndicatingLabeledOperation = null;
062:            private boolean allowAccessIfNoAttributesAreLabeled = true;
063:
064:            //~ Methods ========================================================================================================
065:
066:            /**
067:             * Set whether or not to allow the user to run methods in which none of the incoming arguments are labeled.
068:             *
069:             * <p>Default value: <b>true, users can run such methods.</b></p>
070:             *
071:             * @param allowAccessIfNoAttributesAreLabeled boolean
072:             */
073:            public void setAllowAccessIfNoAttributesAreLabeled(
074:                    boolean allowAccessIfNoAttributesAreLabeled) {
075:                this .allowAccessIfNoAttributesAreLabeled = allowAccessIfNoAttributesAreLabeled;
076:            }
077:
078:            /**
079:             * Each method intended for evaluation by this voter must include this tag name in the definition of the
080:             * MethodSecurityInterceptor, indicating if this voter should evaluate the arguments and compare them against the
081:             * label map.
082:             *
083:             * @param attributeIndicatingLabeledOperation string
084:             */
085:            public void setAttributeIndicatingLabeledOperation(
086:                    String attributeIndicatingLabeledOperation) {
087:                this .attributeIndicatingLabeledOperation = attributeIndicatingLabeledOperation;
088:            }
089:
090:            /**
091:             * Set the map that correlate a user's assigned label against domain object values that are considered data
092:             * labels. An example application context configuration of a <tt>labelMap</tt>:
093:             *
094:             * <pre>
095:             * &lt;bean id="accessDecisionManager" class="org.acegisecurity.vote.UnanimousBased"&gt;
096:             *     &lt;property name="allowIfAllAbstainDecisions"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
097:             *     &lt;property name="decisionVoters"&gt;
098:             *       &lt;list&gt;
099:             *         &lt;bean class="org.acegisecurity.vote.RoleVoter"/&gt;
100:             *         &lt;bean class="org.acegisecurity.vote.LabelBasedAclVoter"&gt;
101:             *           &lt;property name="attributeIndicatingLabeledOperation"&gt;
102:             *             &lt;value&gt;LABELED_OPERATION&lt;/value&gt;
103:             *           &lt;/property&gt;
104:             *           &lt;property name="labelMap"&gt;
105:             *             &lt;map&gt;
106:             *               &lt;entry key="DATA_LABEL_BLUE"&gt;
107:             *                 &lt;list&gt;
108:             *                   &lt;value&gt;blue&lt;/value&gt;
109:             *                   &lt;value&gt;indigo&lt;/value&gt;
110:             *                   &lt;value&gt;purple&lt;/value&gt;
111:             *                 &lt;/list&gt;
112:             *               &lt;/entry&gt;
113:             *               &lt;entry key="LABEL_ORANGE"&gt;
114:             *                 &lt;list&gt;
115:             *                   &lt;value&gt;orange&lt;/value&gt;
116:             *                   &lt;value&gt;sunshine&lt;/value&gt;
117:             *                   &lt;value&gt;amber&lt;/value&gt;
118:             *                 &lt;/list&gt;
119:             *               &lt;/entry&gt;
120:             *               &lt;entry key="LABEL_ADMIN"&gt;
121:             *                 &lt;list&gt;
122:             *                   &lt;value&gt;blue&lt;/value&gt;
123:             *                   &lt;value&gt;indigo&lt;/value&gt;
124:             *                   &lt;value&gt;purple&lt;/value&gt;
125:             *                   &lt;value&gt;orange&lt;/value&gt;
126:             *                   &lt;value&gt;sunshine&lt;/value&gt;
127:             *                   &lt;value&gt;amber&lt;/value&gt;
128:             *                 &lt;/list&gt;
129:             *               &lt;/entry&gt;
130:             *             &lt;/map&gt;
131:             *           &lt;/property&gt;
132:             *         &lt;/bean&gt;
133:             *       &lt;/list&gt;
134:             *     &lt;/property&gt;
135:             *   &lt;/bean&gt;
136:             * </pre>
137:             *
138:             * @param labelMap a map structured as in the above example.
139:             *
140:             */
141:            public void setLabelMap(Map labelMap) {
142:                this .labelMap = labelMap;
143:            }
144:
145:            /**
146:             * This acl voter will only evaluate labeled methods if they are marked in the security interceptor's
147:             * configuration with the attribute stored in attributeIndicatingLabeledOperation.
148:             *
149:             * @param attribute DOCUMENT ME!
150:             *
151:             * @return DOCUMENT ME!
152:             *
153:             * @see org.acegisecurity.vote.AbstractAclVoter
154:             * @see org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor
155:             */
156:            public boolean supports(ConfigAttribute attribute) {
157:                if (attribute.getAttribute().equals(
158:                        attributeIndicatingLabeledOperation)) {
159:                    logger.debug(attribute + " is supported.");
160:
161:                    return true;
162:                }
163:
164:                if (logger.isDebugEnabled()) {
165:                    logger.debug(attribute + " is unsupported.");
166:                }
167:
168:                return false;
169:            }
170:
171:            /**
172:             * Vote on whether or not the user has all the labels necessary to match the method argument's labeled
173:             * data.
174:             *
175:             * @param authentication DOCUMENT ME!
176:             * @param object DOCUMENT ME!
177:             * @param config DOCUMENT ME!
178:             *
179:             * @return ACCESS_ABSTAIN, ACCESS_GRANTED, or ACCESS_DENIED.
180:             */
181:            public int vote(Authentication authentication, Object object,
182:                    ConfigAttributeDefinition config) {
183:                int result = ACCESS_ABSTAIN;
184:
185:                if (logger.isDebugEnabled()) {
186:                    logger
187:                            .debug("==========================================================");
188:                }
189:
190:                if (this .supports((ConfigAttribute) config
191:                        .getConfigAttributes().next())) {
192:                    result = ACCESS_DENIED;
193:
194:                    /* Parse out the user's labels by examining the security context, and checking
195:                     * for matches against the label map.
196:                     */
197:                    List userLabels = new Vector();
198:
199:                    for (int i = 0; i < authentication.getAuthorities().length; i++) {
200:                        if (labelMap.containsKey(authentication
201:                                .getAuthorities()[i].getAuthority())) {
202:                            String userLabel = authentication.getAuthorities()[i]
203:                                    .getAuthority();
204:                            userLabels.add(userLabel);
205:                            logger.debug("Adding " + userLabel + " to <<<"
206:                                    + authentication.getName()
207:                                    + "'s>>> authorized label list");
208:                        }
209:                    }
210:
211:                    MethodInvocation invocation = (MethodInvocation) object;
212:
213:                    int matches = 0;
214:                    int misses = 0;
215:                    int labeledArguments = 0;
216:
217:                    for (int j = 0; j < invocation.getArguments().length; j++) {
218:                        if (invocation.getArguments()[j] instanceof  LabeledData) {
219:                            labeledArguments++;
220:
221:                            boolean matched = false;
222:
223:                            String argumentDataLabel = ((LabeledData) invocation
224:                                    .getArguments()[j]).getLabel();
225:                            logger.debug("Argument["
226:                                    + j
227:                                    + "/"
228:                                    + invocation.getArguments()[j].getClass()
229:                                            .getName()
230:                                    + "] has a data label of "
231:                                    + argumentDataLabel);
232:
233:                            List validDataLabels = new Vector();
234:
235:                            for (int i = 0; i < userLabels.size(); i++) {
236:                                validDataLabels.addAll((List) labelMap
237:                                        .get(userLabels.get(i)));
238:                            }
239:
240:                            logger.debug("The valid labels for user label "
241:                                    + userLabels + " are " + validDataLabels);
242:
243:                            Iterator dataLabelIter = validDataLabels.iterator();
244:
245:                            while (dataLabelIter.hasNext()) {
246:                                String validDataLabel = (String) dataLabelIter
247:                                        .next();
248:
249:                                if (argumentDataLabel.equals(validDataLabel)) {
250:                                    logger.debug(userLabels + " maps to "
251:                                            + validDataLabel
252:                                            + " which matches the argument");
253:                                    matched = true;
254:                                }
255:                            }
256:
257:                            if (matched) {
258:                                logger.debug("We have a match!");
259:                                matches++;
260:                            } else {
261:                                logger.debug("We have a miss!");
262:                                misses++;
263:                            }
264:                        }
265:                    }
266:                    Assert.isTrue((matches + misses) == labeledArguments,
267:                            "The matches (" + matches + ") and misses ("
268:                                    + misses + " ) don't add up ("
269:                                    + labeledArguments + ")");
270:
271:                    logger.debug("We have " + matches + " matches and "
272:                            + misses + " misses and " + labeledArguments
273:                            + " labeled arguments.");
274:
275:                    /* The result has already been set to ACCESS_DENIED. Only if there is a proper match of
276:                     * labels will this be overturned. However, if none of the attributes are actually labeled,
277:                     * the result is dependent on allowAccessIfNoAttributesAreLabeled.
278:                     */
279:                    if ((matches > 0) && (misses == 0)) {
280:                        result = ACCESS_GRANTED;
281:                    } else if (labeledArguments == 0) {
282:                        if (allowAccessIfNoAttributesAreLabeled) {
283:                            result = ACCESS_GRANTED;
284:                        } else {
285:                            result = ACCESS_DENIED;
286:                        }
287:                    }
288:                }
289:
290:                if (logger.isDebugEnabled()) {
291:                    switch (result) {
292:                    case ACCESS_GRANTED:
293:
294:                        if (logger.isDebugEnabled()) {
295:                            logger.debug("===== Access is granted =====");
296:                        }
297:
298:                        break;
299:
300:                    case ACCESS_DENIED:
301:
302:                        if (logger.isDebugEnabled()) {
303:                            logger.debug("===== Access is denied =====");
304:                        }
305:
306:                        break;
307:
308:                    case ACCESS_ABSTAIN:
309:
310:                        if (logger.isDebugEnabled()) {
311:                            logger.debug("===== Abstaining =====");
312:                        }
313:
314:                        break;
315:                    }
316:                }
317:
318:                return result;
319:            }
320:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.