Author: channa
Date: Thu Jan 17 08:01:16 2008
New Revision: 12426

Log:

Adding password length validation and some refactoring to reduce code 
duplication.

Modified:
   trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardRegistrationBean.java
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/RegistrationBean.java
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/userprofile/AddUserBean.java

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java     
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java     
Thu Jan 17 08:01:16 2008
@@ -110,4 +110,6 @@
     public static final String PRIMARY_USER = "PrimaryUser";
     public static final String PRIMARY_PASSWORD = "PrimaryPassword";
     public static final String LOCALHOST = "localhost";
+
+    public static final int MIN_PASSWORD_LENGTH = 5;
 }

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardRegistrationBean.java
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardRegistrationBean.java
    (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/InfoCardRegistrationBean.java
    Thu Jan 17 08:01:16 2008
@@ -146,27 +146,10 @@
 
         if (userName.equals("")) {
             errors.put("userName", "User name cannot be empty.");
-            userName = "";
             valid = false;
         }
 
-        if (password.equals("")) {
-            errors.put("password", "Password cannot be empty.");
-            password = "";
-            valid = false;
-        }
-
-        if (confirmedPassword.equals("")) {
-            errors.put("confirmedPassword", "Password confirmation cannot be 
empty.");
-            confirmedPassword = "";
-            valid = false;
-        }
-
-        if (!confirmedPassword.equals(password)) {
-            errors.put("confirmedPassword", "Confirmation password must match 
password.");
-            confirmedPassword = "";
-            valid = false;
-        }
+        valid = RegistrationBean.isPasswordValid(password, confirmedPassword, 
errors);
 
         // Expensive operation, so do only once all other data has been 
validated.
         if (valid) {

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/RegistrationBean.java
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/RegistrationBean.java
    (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/identity/RegistrationBean.java
    Thu Jan 17 08:01:16 2008
@@ -28,7 +28,6 @@
 import org.wso2.usermanager.Realm;
 import org.wso2.usermanager.UserManagerException;
 import org.wso2.usermanager.UserStoreAdmin;
-import org.wso2.usermanager.UserStoreReader;
 import org.wso2.usermanager.verification.email.EmailVerifier;
 import org.wso2.wsas.persistence.PersistenceManager;
 
@@ -278,46 +277,24 @@
         boolean valid = true;
         if (userName.equals("")) {
             errors.put("userName", "User name cannot be empty.");
-            userName = "";
             valid = false;
         }
 
-        // Full name is not mandatory for creating the admin profile.
+        // Full name and e-mail ID are not mandatory for creating the admin 
profile.
         if (!adminCreation) {
             if (fullName.equals("")) {
                 errors.put("fullName", "Full name cannot be empty.");
-                fullName = "";
                 valid = false;
             }
-        }
-
-        if (password.equals("")) {
-            errors.put("password", "Password cannot be empty.");
-            password = "";
-            valid = false;
-        }
-
-        if (confirmedPassword.equals("")) {
-            errors.put("confirmedPassword", "Password confirmation cannot be 
empty.");
-            confirmedPassword = "";
-            valid = false;
-        }
-
-        if (!confirmedPassword.equals(password)) {
-            errors.put("confirmedPassword", "Confirmation password must match 
password.");
-            confirmedPassword = "";
-            valid = false;
-        }
 
-        // E-mail ID is not mandatory for creating the admin profile.
-        if (!adminCreation) {
             if (emailId.equals("")) {
                 errors.put("emailId", "E-mail ID cannot be empty.");
-                emailId = "";
                 valid = false;
             }
         }
 
+        valid = isPasswordValid(password, confirmedPassword, errors);
+
         // Expensive operation, so do only once all other data has been 
validated.
         if (valid) {
             if (ManageUsers.isExistingUser(request, userName)) {
@@ -329,4 +306,36 @@
 
         return valid;
     }
+
+    /**
+     * Performs the password validation and returns the validity state.
+     * @param password The content of the password field.
+     * @param confirmedPassword The content of the confirmed password field.
+     * @param errors table of errors.
+     * @return true if the password and confirmation are valid.
+     */
+    public static boolean isPasswordValid(String password, String 
confirmedPassword,
+                                          Hashtable errors) {
+        boolean valid = true;
+
+        if (password.equals("")) {
+            errors.put("password", "Password cannot be empty.");
+            valid = false;
+        } else if (password.length() < MashupConstants.MIN_PASSWORD_LENGTH) {
+            errors.put("password", "Password should be atleast " +
+                    MashupConstants.MIN_PASSWORD_LENGTH + " characters.");
+            valid = false;
+        }
+
+        if (confirmedPassword.equals("")) {
+            errors.put("confirmedPassword", "Password confirmation cannot be 
empty.");
+            valid = false;
+        }
+
+        if (!confirmedPassword.equals(password)) {
+            errors.put("confirmedPassword", "Confirmation password must match 
password.");
+            valid = false;
+        }
+        return valid;
+    }
 }

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/userprofile/AddUserBean.java
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/userprofile/AddUserBean.java
      (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/userprofile/AddUserBean.java
      Thu Jan 17 08:01:16 2008
@@ -242,29 +242,32 @@
         boolean valid = true;
         if (userName.equals("")) {
             errors.put("userName", "User name cannot be empty.");
-            userName = "";
             valid = false;
         }
 
         if (fullName.equals("")) {
             errors.put("fullName", "Full name cannot be empty.");
-            fullName = "";
             valid = false;
         }
 
-        // Password is not required when editing a profile.
-        if (!editMode && password.equals("")) {
-            errors.put("password", "Password cannot be empty.");
-            password = "";
-            valid = false;
-        }
-
-        // Expensive operation, so do only once all other data has been 
validated.
-        if (!editMode && valid) {
-            if (ManageUsers.isExistingUser(request, userName)) {
-                errors.put("userName", "User name exists, please select 
another.");
-                userName = "";
+        // These validations are only required when creating a new user.
+        if (!editMode) {
+            if (password.equals("")) {
+                errors.put("password", "Password cannot be empty.");
                 valid = false;
+            } else if (password.length() < 
MashupConstants.MIN_PASSWORD_LENGTH) {
+                errors.put("password", "Password should be atleast " +
+                        MashupConstants.MIN_PASSWORD_LENGTH + " characters.");
+                valid = false;
+            }
+
+            // Expensive operation, so do only once all other data has been 
validated.
+            if (valid) {
+                if (ManageUsers.isExistingUser(request, userName)) {
+                    errors.put("userName", "User name exists, please select 
another.");
+                    userName = "";
+                    valid = false;
+                }
             }
         }
 

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to