From 4768e4b13c07aecd9dae80c6a9296f347955c5fa Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Tue, 9 Jun 2009 01:42:37 +0000 Subject: [PATCH] Removed methods relating to current context from AuthorityUtils, making it a simple factory for GrantedAuthority lists etc. --- .../core/authority/AuthorityUtils.java | 47 +++---------------- .../core/authority/AuthorityUtilsTests.java | 31 ++++++++++++ 2 files changed, 38 insertions(+), 40 deletions(-) create mode 100644 core/src/test/java/org/springframework/security/core/authority/AuthorityUtilsTests.java diff --git a/core/src/main/java/org/springframework/security/core/authority/AuthorityUtils.java b/core/src/main/java/org/springframework/security/core/authority/AuthorityUtils.java index 1b86c7eff4..cc5371d296 100644 --- a/core/src/main/java/org/springframework/security/core/authority/AuthorityUtils.java +++ b/core/src/main/java/org/springframework/security/core/authority/AuthorityUtils.java @@ -1,58 +1,25 @@ package org.springframework.security.core.authority; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.util.StringUtils; - import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.util.StringUtils; + /** + * Utility method for manipulating GrantedAuthority collections etc. + *

+ * Mainly intended for internal use. + * * @author Luke Taylor * @version $Id$ */ public abstract class AuthorityUtils { public static final List NO_AUTHORITIES = Collections.emptyList(); - /** - * Returns true if the current user has the specified authority. - * - * @param authority the authority to test for (e.g. "ROLE_A"). - * @return true if a GrantedAuthority object with the same string representation as the supplied authority - * name exists in the current user's list of authorities. False otherwise, or if the user in not authenticated. - */ - public static boolean userHasAuthority(String authority) { - List authorities = getUserAuthorities(); - - for (GrantedAuthority grantedAuthority : authorities) { - if (authority.equals(grantedAuthority.getAuthority())) { - return true; - } - } - - return false; - } - - /** - * Returns the authorities of the current user. - * - * @return an array containing the current user's authorities (or an empty array if not authenticated), never null. - */ - private static List getUserAuthorities() { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - - if (auth == null || auth.getAuthorities() == null) { - return NO_AUTHORITIES; - } - - return auth.getAuthorities(); - } - - /** * Creates a array of GrantedAuthority objects from a comma-separated string * representation (e.g. "ROLE_A, ROLE_B, ROLE_C"). diff --git a/core/src/test/java/org/springframework/security/core/authority/AuthorityUtilsTests.java b/core/src/test/java/org/springframework/security/core/authority/AuthorityUtilsTests.java new file mode 100644 index 0000000000..f991b3d57e --- /dev/null +++ b/core/src/test/java/org/springframework/security/core/authority/AuthorityUtilsTests.java @@ -0,0 +1,31 @@ +package org.springframework.security.core.authority; + +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.AuthorityUtils; + +/** + * @author Luke Taylor + * @version $Id$ + */ +public class AuthorityUtilsTests { + + @Test + public void commaSeparatedStringIsParsedCorrectly() { + List authorityArray = + AuthorityUtils.commaSeparatedStringToAuthorityList(" ROLE_A, B, C, ROLE_D\n,\n E "); + + Set authorities = AuthorityUtils.authorityListToSet(authorityArray); + + assertTrue(authorities.contains("B")); + assertTrue(authorities.contains("C")); + assertTrue(authorities.contains("E")); + assertTrue(authorities.contains("ROLE_A")); + assertTrue(authorities.contains("ROLE_D")); + } +}