Browse Source

Added trimming of whitespace to tokens and use of Springs StringUtils.hasText() to check for content in the string passed to setAsText.

Luke Taylor 20 years ago
parent
commit
c89d4a8add

+ 4 - 4
core/src/main/java/org/acegisecurity/userdetails/memory/UserAttributeEditor.java

@@ -33,14 +33,12 @@ public class UserAttributeEditor extends PropertyEditorSupport {
     //~ Methods ================================================================
 
     public void setAsText(String s) throws IllegalArgumentException {
-        if ((s == null) || "".equals(s)) {
-            setValue(null);
-        } else {
+        if (StringUtils.hasText(s)) {
             String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
             UserAttribute userAttrib = new UserAttribute();
 
             for (int i = 0; i < tokens.length; i++) {
-                String currentToken = tokens[i];
+                String currentToken = tokens[i].trim();
 
                 if (i == 0) {
                     userAttrib.setPassword(currentToken);
@@ -61,6 +59,8 @@ public class UserAttributeEditor extends PropertyEditorSupport {
             } else {
                 setValue(null);
             }
+        } else {
+            setValue(null);
         }
     }
 }

+ 11 - 0
core/src/test/java/org/acegisecurity/providers/dao/memory/UserAttributeEditorTests.java

@@ -123,4 +123,15 @@ public class UserAttributeEditorTests extends TestCase {
         UserAttribute user = (UserAttribute) editor.getValue();
         assertTrue(user == null);
     }
+
+    public void testCorrectOperationWithTrailingSpaces() {
+        UserAttributeEditor editor = new UserAttributeEditor();
+        editor.setAsText("password ,ROLE_ONE,ROLE_TWO ");
+
+        UserAttribute user = (UserAttribute) editor.getValue();
+        assertEquals("password", user.getPassword());
+        assertEquals(2, user.getAuthorities().length);
+        assertEquals("ROLE_ONE", user.getAuthorities()[0].getAuthority());
+        assertEquals("ROLE_TWO", user.getAuthorities()[1].getAuthority());
+    }
 }