Validate usernames to be valid emails (issue #2400)
authorChristos Stathis <chstath@ebs.gr>
Wed, 16 May 2012 14:37:40 +0000 (17:37 +0300)
committerChristos Stathis <chstath@ebs.gr>
Wed, 16 May 2012 14:37:40 +0000 (17:37 +0300)
src/gr/grnet/pithos/web/client/AddUserDialog.java
src/gr/grnet/pithos/web/client/PermissionsAddDialog.java

index 186a036..144ba04 100644 (file)
@@ -48,6 +48,7 @@ import com.google.gwt.event.dom.client.KeyCodes;
 import com.google.gwt.event.dom.client.KeyDownEvent;\r
 import com.google.gwt.http.client.Response;\r
 import com.google.gwt.http.client.URL;\r
+import com.google.gwt.regexp.shared.RegExp;\r
 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
 import com.google.gwt.user.client.ui.Anchor;\r
 import com.google.gwt.user.client.ui.Button;\r
@@ -179,6 +180,12 @@ public class AddUserDialog extends DialogBox {
                String name = userName.getText().trim();\r
                if (name.length() == 0)\r
                        return;\r
+               RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$", "i");\r
+               if (!emailValidator.test(name)) {\r
+                       app.displayWarning("Username must be a valid email address");\r
+                       return;\r
+               }\r
+                       \r
        group.addMember(name);\r
        String path = "?update=";\r
        PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUsername(), path) {\r
index 55aaec6..43914fe 100644 (file)
@@ -42,6 +42,7 @@ import com.google.gwt.dom.client.NativeEvent;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.event.dom.client.KeyCodes;
+import com.google.gwt.regexp.shared.RegExp;
 import com.google.gwt.user.client.Event.NativePreviewEvent;
 import com.google.gwt.user.client.ui.Anchor;
 import com.google.gwt.user.client.ui.Button;
@@ -139,7 +140,12 @@ public class PermissionsAddDialog extends DialogBox {
        protected void addPermission() {
         String selected = null;
                if (userAdd) {
-                       selected = userBox.getText();
+                       selected = userBox.getText().trim();
+                       RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$", "i");
+                       if (!emailValidator.test(selected)) {
+                               app.displayWarning("Username must be a valid email address");
+                               return;
+                       }
                } else if (groupBox.getSelectedIndex() > -1) {
                        String groupName = groupBox.getValue(groupBox.getSelectedIndex());
                        selected = app.getUsername() + ":" + groupName;
@@ -148,9 +154,10 @@ public class PermissionsAddDialog extends DialogBox {
             return;
         }
         if (selected == null || selected.length() == 0 || selected.equals(app.getUsername() + ":")) {
-               app.displayError("You have to select o username or group");
+               app.displayWarning("You have to select a username or group");
                return;
         }
+
                boolean readValue = read.getValue();
                boolean writeValue = write.getValue();