001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SecurityResolver.java 2057 2007-11-21 15:35:32Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.helper.bean;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.ow2.easybeans.asm.Type;
030: import org.ow2.easybeans.deployment.annotations.JClassInterceptor;
031: import org.ow2.easybeans.deployment.annotations.JMethod;
032: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
033: import org.ow2.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
034: import org.ow2.easybeans.security.interceptors.AccessInterceptor;
035: import org.ow2.easybeans.security.interceptors.DenyAllInterceptor;
036: import org.ow2.easybeans.security.interceptors.RunAsAccessInterceptor;
037:
038: /**
039: * This class adds the interceptor for the security (if required) on a given method.
040: * @author Florent Benoit
041: */
042: public final class SecurityResolver {
043:
044: /**
045: * Signature of EasyBeans interceptors.
046: */
047: private static final JMethod EASYBEANS_INTERCEPTOR = new JMethod(
048: 0,
049: "intercept",
050: "(Lorg/ow2/easybeans/api/EasyBeansInvocationContext;)Ljava/lang/Object;",
051: null, new String[] { "java/lang/Exception" });
052:
053: /**
054: * DenyAll interceptor.
055: */
056: private static final String DENYALL_INTERCEPTOR = Type
057: .getInternalName(DenyAllInterceptor.class);
058:
059: /**
060: * RunAs interceptor.
061: */
062: private static final String RUNAS_INTERCEPTOR = Type
063: .getInternalName(RunAsAccessInterceptor.class);
064:
065: /**
066: * Role based interceptor.
067: */
068: private static final String ROLEBASED_INTERCEPTOR = Type
069: .getInternalName(AccessInterceptor.class);
070:
071: /**
072: * Helper class, no public constructor.
073: */
074: private SecurityResolver() {
075: }
076:
077: /**
078: * Adds the right transaction interceptor depending of the transactional
079: * attribute set by the user.
080: * @param bean the given bean on which set the transactional interceptor.
081: */
082: public static void resolve(final ClassAnnotationMetadata bean) {
083:
084: // Class values
085: boolean beanPermitAll = bean.hasPermitAll();
086: List<String> beanRolesAllowed = bean.getRolesAllowed();
087:
088: String runAs = bean.getRunAs();
089: String super ClassName = bean.getSuperName();
090: // Search in super class
091: while (runAs == null
092: && !super ClassName.equals(Type
093: .getInternalName(Object.class))) {
094: ClassAnnotationMetadata super Metadata = bean
095: .getEjbJarAnnotationMetadata()
096: .getClassAnnotationMetadata(super ClassName);
097: if (super Metadata != null) {
098: runAs = super Metadata.getRunAs();
099: super ClassName = super Metadata.getSuperName();
100: // Set with the super class value
101: if (runAs != null) {
102: bean.setRunAs(runAs);
103: }
104: }
105: }
106:
107: // Inheritance for DeclaredRoles
108: List<String> declaredRoles = bean.getDeclareRoles();
109: super ClassName = bean.getSuperName();
110: // if null, search on super classes.
111: while (declaredRoles == null
112: && !super ClassName.equals(Type
113: .getInternalName(Object.class))) {
114: ClassAnnotationMetadata super Metadata = bean
115: .getEjbJarAnnotationMetadata()
116: .getClassAnnotationMetadata(super ClassName);
117: if (super Metadata != null) {
118: declaredRoles = super Metadata.getDeclareRoles();
119: super ClassName = super Metadata.getSuperName();
120: // Set with the super class value
121: if (declaredRoles != null) {
122: bean.setDeclareRoles(declaredRoles);
123: }
124: }
125: }
126:
127: for (MethodAnnotationMetadata method : bean
128: .getMethodAnnotationMetadataCollection()) {
129: List<JClassInterceptor> interceptors = method
130: .getInterceptors();
131: if (interceptors == null) {
132: interceptors = new ArrayList<JClassInterceptor>();
133: }
134:
135: // DenyAll ?
136: boolean denyAll = method.hasDenyAll();
137:
138: // PermitAll ?
139: boolean permitAll = method.hasPermitAll();
140: // not defined on the method, check inheritance or bean's value
141: if (!permitAll) {
142: if (method.isInherited()) {
143: permitAll = method
144: .getOriginalClassAnnotationMetadata()
145: .hasPermitAll();
146: method.setPermitAll(permitAll);
147: } else {
148: permitAll = beanPermitAll;
149: }
150: }
151:
152: // roles allowed.
153: List<String> rolesAllowed = method.getRolesAllowed();
154: if (rolesAllowed == null) {
155: if (method.isInherited()) {
156: rolesAllowed = method
157: .getOriginalClassAnnotationMetadata()
158: .getRolesAllowed();
159: method.setRolesAllowed(rolesAllowed);
160: } else {
161: // Method roles are Bean's roles.
162: rolesAllowed = beanRolesAllowed;
163: // Update the method value
164: method.setRolesAllowed(beanRolesAllowed);
165: }
166: }
167:
168: // runAs ?
169: if (runAs != null) {
170: interceptors.add(new JClassInterceptor(
171: RUNAS_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
172: }
173:
174: if (denyAll) {
175: interceptors.add(new JClassInterceptor(
176: DENYALL_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
177: } else if (!permitAll && rolesAllowed != null) {
178: // only if permitAll is not set as no interceptor is added in this case
179: interceptors.add(new JClassInterceptor(
180: ROLEBASED_INTERCEPTOR, EASYBEANS_INTERCEPTOR));
181: }
182: method.setInterceptors(interceptors);
183: }
184: }
185: }
|