From cbf0df5b42a8fdda8ac1a240b9b6168a8e8b6608 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Fri, 14 Mar 2008 17:23:38 +0000 Subject: [PATCH] SWF-521 Expose current user via a Web Flow API construct as well as via an EL expression --- .../webflow/context/ExternalContext.java | 8 ++++++++ .../context/servlet/ServletExternalContext.java | 5 +++++ .../expression/WebFlowOgnlExpressionParser.java | 9 ++------- .../expression/el/WebFlowELExpressionParser.java | 8 -------- .../webflow/test/MockExternalContext.java | 10 ++++++++++ 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/spring-webflow/src/main/java/org/springframework/webflow/context/ExternalContext.java b/spring-webflow/src/main/java/org/springframework/webflow/context/ExternalContext.java index 6ae3afe5..e60f7e13 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/context/ExternalContext.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/context/ExternalContext.java @@ -16,6 +16,7 @@ package org.springframework.webflow.context; import java.io.Writer; +import java.security.Principal; import org.springframework.webflow.core.collection.AttributeMap; import org.springframework.webflow.core.collection.MutableAttributeMap; @@ -36,6 +37,7 @@ import org.springframework.webflow.core.collection.SharedAttributeMap; * @author Keith Donald * @author Erwin Vervaet * @author Jeremy Grelle + * @author Scott Andrews */ public interface ExternalContext { @@ -99,6 +101,12 @@ public interface ExternalContext { */ public String getFlowExecutionUri(String flowId, String flowExecutionKey); + /** + * Provides access to the user's principal security object. + * @return the user principal + */ + public Principal getCurrentUser(); + /** * Provides access to the context object for the current environment. * @return the environment specific context object diff --git a/spring-webflow/src/main/java/org/springframework/webflow/context/servlet/ServletExternalContext.java b/spring-webflow/src/main/java/org/springframework/webflow/context/servlet/ServletExternalContext.java index c4cb8c7e..90b53d02 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/context/servlet/ServletExternalContext.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/context/servlet/ServletExternalContext.java @@ -17,6 +17,7 @@ package org.springframework.webflow.context.servlet; import java.io.IOException; import java.io.Writer; +import java.security.Principal; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -170,6 +171,10 @@ public class ServletExternalContext implements ExternalContext { return applicationMap; } + public Principal getCurrentUser() { + return request.getUserPrincipal(); + } + public Object getNativeContext() { return context; } diff --git a/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java b/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java index 3816c805..16e6c8b7 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java @@ -29,7 +29,6 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.StaticListableBeanFactory; import org.springframework.binding.collection.MapAdaptable; import org.springframework.binding.expression.ognl.OgnlExpressionParser; -import org.springframework.security.context.SecurityContextHolder; import org.springframework.util.ClassUtils; import org.springframework.webflow.core.collection.MutableAttributeMap; import org.springframework.webflow.engine.AnnotatedAction; @@ -103,15 +102,11 @@ public class WebFlowOgnlExpressionParser extends OgnlExpressionParser { } return requestContext; } - if (securityPresent && property.equals("currentUser")) { + if (property.equals("currentUser")) { if (logger.isDebugEnabled()) { logger.debug("Successfully resolved implicit flow variable '" + property + "'"); } - if (SecurityContextHolder.getContext() != null) { - return SecurityContextHolder.getContext().getAuthentication(); - } else { - return null; - } + return requestContext.getExternalContext().getCurrentUser(); } if (requestContext.getRequestScope().contains(property)) { if (logger.isDebugEnabled()) { diff --git a/spring-webflow/src/main/java/org/springframework/webflow/expression/el/WebFlowELExpressionParser.java b/spring-webflow/src/main/java/org/springframework/webflow/expression/el/WebFlowELExpressionParser.java index 0c695a8a..72a9ee80 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/expression/el/WebFlowELExpressionParser.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/expression/el/WebFlowELExpressionParser.java @@ -27,7 +27,6 @@ import javax.el.VariableMapper; import org.springframework.binding.expression.el.DefaultELResolver; import org.springframework.binding.expression.el.ELContextFactory; import org.springframework.binding.expression.el.ELExpressionParser; -import org.springframework.util.ClassUtils; import org.springframework.webflow.core.collection.MutableAttributeMap; import org.springframework.webflow.execution.RequestContext; @@ -35,13 +34,9 @@ import org.springframework.webflow.execution.RequestContext; * Allows for Unified EL expressions in a FlowDefinition. * * @author Jeremy Grelle - * @author Scott Andrews */ public class WebFlowELExpressionParser extends ELExpressionParser { - private static boolean securityPresent = ClassUtils - .isPresent("org.springframework.security.context.SecurityContextHolder"); - /** * Creates a new Web Flow EL expression parser. * @param expressionFactory the underlying EL expression factory (EL provider specific) @@ -61,9 +56,6 @@ public class WebFlowELExpressionParser extends ELExpressionParser { RequestContext context = (RequestContext) target; List customResolvers = new ArrayList(); customResolvers.add(new RequestContextELResolver(context)); - if (securityPresent) { - customResolvers.add(new SpringSecurityELResolver()); - } customResolvers.add(new ImplicitFlowVariableELResolver(context)); customResolvers.add(new ScopeSearchingELResolver(context)); customResolvers.add(new SpringBeanWebFlowELResolver(context)); diff --git a/spring-webflow/src/main/java/org/springframework/webflow/test/MockExternalContext.java b/spring-webflow/src/main/java/org/springframework/webflow/test/MockExternalContext.java index e94f3168..68365ccd 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/test/MockExternalContext.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/test/MockExternalContext.java @@ -17,9 +17,12 @@ package org.springframework.webflow.test; import java.io.StringWriter; import java.io.Writer; +import java.security.Principal; import java.util.HashMap; import org.springframework.binding.collection.SharedMapDecorator; +import org.springframework.security.context.SecurityContextHolder; +import org.springframework.util.ClassUtils; import org.springframework.webflow.context.ExternalContext; import org.springframework.webflow.core.collection.AttributeMap; import org.springframework.webflow.core.collection.LocalAttributeMap; @@ -110,6 +113,13 @@ public class MockExternalContext implements ExternalContext { return applicationMap; } + public Principal getCurrentUser() { + if (ClassUtils.isPresent("org.springframework.security.context.SecurityContextHolder")) + return SecurityContextHolder.getContext().getAuthentication(); + else + return null; + } + public Object getNativeContext() { return nativeContext; }