SWF-521 Expose current user via a Web Flow API construct as well as via an EL expression

This commit is contained in:
Scott Andrews
2008-03-14 17:23:38 +00:00
parent adf7578de9
commit cbf0df5b42
5 changed files with 25 additions and 15 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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()) {

View File

@@ -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));

View File

@@ -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;
}