Commits

Sebastian Sdorra committed dd00d39

allow spaces in usernames

Comments (0)

Files changed (5)

scm-core/src/main/java/sonia/scm/util/HttpUtil.java

 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
   /** authentication realm for basic authentication */
   public static final String AUTHENTICATION_REALM = "SONIA :: SCM Manager";
 
+  /** Field description */
+  public static final String ENCODING = "UTF-8";
+
   /** authentication header */
   public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
 
   //~--- methods --------------------------------------------------------------
 
   /**
+   * Method description
+   *
+   *
+   * @param value
+   *
+   * @return
+   * @since 1.9
+   */
+  public static String decode(String value)
+  {
+    try
+    {
+      value = URLDecoder.decode(value, ENCODING);
+    }
+    catch (UnsupportedEncodingException ex)
+    {
+      throw new RuntimeException("could not decode", ex);
+    }
+
+    return value;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param value
+   *
+   * @return
+   * @since 1.9
+   */
+  public static String encode(String value)
+  {
+    try
+    {
+      value = URLEncoder.encode(value, ENCODING);
+    }
+    catch (UnsupportedEncodingException ex)
+    {
+      throw new RuntimeException("could not encode", ex);
+    }
+
+    return value;
+  }
+
+  /**
    * Send an unauthorized header back to the client
    *
    *

scm-core/src/main/java/sonia/scm/util/ValidationUtil.java

   private static final String REGEX_NAME = "^[A-z0-9\\.\\-_]+$";
 
   /** Field description */
-  private static final String REGEX_USERNAME = "^[A-z0-9\\.\\-_@]+$";
+  private static final String REGEX_USERNAME = "^[^ ][A-z0-9\\.\\-_@ ]+[^ ]$";
 
   //~--- get methods ----------------------------------------------------------
 

scm-core/src/test/java/sonia/scm/util/ValidationUtilTest.java

     assertTrue(ValidationUtil.isUsernameValid("Test123-git"));
     assertTrue(ValidationUtil.isUsernameValid("Test_user-123.git"));
     assertTrue(ValidationUtil.isUsernameValid("test@scm-manager.de"));
+    assertTrue(ValidationUtil.isUsernameValid("test 123"));
 
     // false
-    assertFalse(ValidationUtil.isUsernameValid("test 123"));
+    assertFalse(ValidationUtil.isUsernameValid(" test 123"));
+    assertFalse(ValidationUtil.isUsernameValid(" test 123 "));
+    assertFalse(ValidationUtil.isUsernameValid("test 123 "));
     assertFalse(ValidationUtil.isUsernameValid("test/123"));
     assertFalse(ValidationUtil.isUsernameValid("test%123"));
     assertFalse(ValidationUtil.isUsernameValid("test:123"));

scm-webapp/src/main/java/sonia/scm/api/rest/resources/AbstractManagerResource.java

 import sonia.scm.ModelObject;
 import sonia.scm.security.ScmSecurityException;
 import sonia.scm.util.AssertUtil;
+import sonia.scm.util.HttpUtil;
 import sonia.scm.util.Util;
 
 //~--- JDK imports ------------------------------------------------------------
     try
     {
       manager.create(item);
+
+      String id = getId(item);
+
+      id = HttpUtil.encode(id);
       response = Response.created(
         uriInfo.getAbsolutePath().resolve(
-          getPathPart().concat("/").concat(getId(item)))).build();
+          getPathPart().concat("/").concat(id))).build();
     }
     catch (ScmSecurityException ex)
     {

scm-webapp/src/main/webapp/resources/js/override/ext.form.vtypes.js

   // username validator
   
   username: function(val){
-    return val.match(/^[A-z0-9\.\-_@]+$/);
+    return val.match(/^[^ ][A-z0-9\.\-_@ ]*[^ ]$/);
   },
   
   usernameText: 'The username is invalid.'