From 834f69168dc9a1beaf3ef6b0465c123009a8148c Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Fri, 4 Feb 2005 22:38:07 +0000 Subject: [PATCH] Support getUserPrincipal(). --- .../ContextHolderAwareRequestWrapper.java | 20 ++++++++++++++++++- ...ContextHolderAwareRequestWrapperTests.java | 7 ++++++- doc/xdocs/changes.xml | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapper.java b/core/src/main/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapper.java index 8d81849a8e..ac578b31d3 100644 --- a/core/src/main/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapper.java +++ b/core/src/main/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapper.java @@ -1,4 +1,4 @@ -/* Copyright 2004 Acegi Technology Pty Limited +/* Copyright 2004, 2005 Acegi Technology Pty Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,8 @@ import net.sf.acegisecurity.UserDetails; import net.sf.acegisecurity.context.ContextHolder; import net.sf.acegisecurity.context.SecureContext; +import java.security.Principal; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; @@ -88,6 +90,22 @@ public class ContextHolderAwareRequestWrapper extends HttpServletRequestWrapper return isGranted(role); } + /** + * Returns the Authentication (which is a subclass of + * Principal), or null if unavailable. + * + * @return the Authentication, or null + */ + public Principal getUserPrincipal() { + Authentication auth = getAuthentication(); + + if ((auth == null) || (auth.getPrincipal() == null)) { + return null; + } + + return auth; + } + private Authentication getAuthentication() { if ((ContextHolder.getContext() != null) && ContextHolder.getContext() instanceof SecureContext) { diff --git a/core/src/test/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapperTests.java b/core/src/test/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapperTests.java index 59b2011be8..12a54342a9 100644 --- a/core/src/test/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapperTests.java +++ b/core/src/test/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapperTests.java @@ -1,4 +1,4 @@ -/* Copyright 2004 Acegi Technology Pty Limited +/* Copyright 2004, 2005 Acegi Technology Pty Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,6 +70,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase { assertEquals("marissa", wrapper.getRemoteUser()); assertTrue(wrapper.isUserInRole("ROLE_FOO")); assertFalse(wrapper.isUserInRole("ROLE_NOT_GRANTED")); + assertEquals(auth, wrapper.getUserPrincipal()); ContextHolder.setContext(null); } @@ -93,6 +94,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase { assertFalse(wrapper.isUserInRole("ROLE_NOT_GRANTED")); assertTrue(wrapper.isUserInRole("ROLE_FOOBAR")); assertTrue(wrapper.isUserInRole("ROLE_HELLO")); + assertEquals(auth, wrapper.getUserPrincipal()); ContextHolder.setContext(null); } @@ -106,6 +108,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase { "/")); assertNull(wrapper.getRemoteUser()); assertFalse(wrapper.isUserInRole("ROLE_ANY")); + assertNull(wrapper.getUserPrincipal()); ContextHolder.setContext(null); } @@ -117,6 +120,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase { "/")); assertNull(wrapper.getRemoteUser()); assertFalse(wrapper.isUserInRole("ROLE_ANY")); + assertNull(wrapper.getUserPrincipal()); } public void testNullPrincipalHandling() throws Exception { @@ -133,6 +137,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase { assertNull(wrapper.getRemoteUser()); assertFalse(wrapper.isUserInRole("ROLE_HELLO")); // principal is null, so reject assertFalse(wrapper.isUserInRole("ROLE_FOOBAR")); // principal is null, so reject + assertNull(wrapper.getUserPrincipal()); ContextHolder.setContext(null); } diff --git a/doc/xdocs/changes.xml b/doc/xdocs/changes.xml index 28230ab8ed..fcc3541a9e 100644 --- a/doc/xdocs/changes.xml +++ b/doc/xdocs/changes.xml @@ -31,6 +31,7 @@ FilterToBeanProxy now searches hierarchical bean factories Improved Tapestry support in AbstractProcessingFilter User now accepted blank passwords (null passwords still rejected) + ContextHolderAwareRequestWrapper now provides a getUserPrincipal() method Contacts sample web.xml no longer expect Log4j to be in classpath JaasAuthenticatinProvider now uses System.property "java.security.auth.login.config" JaasAuthenticationCallbackHandler Authentication is passed to handle method setAuthenticatoin removed