← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8643: Account form, improved recapthca validation

 

------------------------------------------------------------
revno: 8643
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-10-22 16:55:29 +0200
message:
  Account form, improved recapthca validation
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AccountController.java
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/account.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/useraccount/account.vm
  dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AccountController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AccountController.java	2012-10-22 11:37:45 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AccountController.java	2012-10-22 14:55:29 +0000
@@ -57,9 +57,9 @@
     private static final Log log = LogFactory.getLog( AccountController.class );
     
     private static final String RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/verify";;
+    protected static final String PUB_KEY = "6LcM6tcSAAAAANwYsFp--0SYtcnze_WdYn8XwMMk";
     private static final String KEY = "6LcM6tcSAAAAAFnHo1f3lLstk3rZv3EVinNROfRq";
     private static final String TRUE = "true";
-    private static final String FALSE = "false";
     private static final String SPLIT = "\n";
     private static final int MAX_LENGTH = 80;
     
@@ -166,9 +166,11 @@
         // ---------------------------------------------------------------------
         
         if ( !TRUE.equalsIgnoreCase( results[0] ) )
-        {
+        {            
+            log.info( "Recaptcha failed with code: " + ( results.length > 0 ? results[1] : "" ) );
+
             response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
-            return results.length > 0 ? results[1] : FALSE;
+            return "The characters you entered did not match the word verification, please try again";
         }
 
         // ---------------------------------------------------------------------
@@ -210,27 +212,6 @@
             "{ \"response\": \"error\", \"message\": \"Username is already taken\" }";
     }
     
-    @RequestMapping( value = "/recaptcha", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_TEXT )
-    public @ResponseBody String validateRecaptcha( 
-        @RequestParam( value = "recaptcha_challenge_field" ) String recapChallenge,
-        @RequestParam( value = "recaptcha_response_field" ) String recapResponse,
-        HttpServletRequest request )
-    {
-        if ( StringUtils.trimToNull( recapChallenge ) == null || StringUtils.trimToNull( recapResponse ) == null )
-        {
-            return FALSE;
-        }
-        
-        String[] results = checkRecaptcha( KEY, request.getRemoteAddr(), recapChallenge, recapResponse );
-        
-        if ( results == null || results.length == 0 )
-        {
-            return FALSE;
-        }
-        
-        return TRUE.equalsIgnoreCase( results[0] ) ? results[0] : ( results.length > 0 ? results[1] : FALSE );
-    }
-
     // ---------------------------------------------------------------------
     // Supportive methods
     // ---------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/account.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/account.js	2012-10-22 11:30:52 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/account.js	2012-10-22 14:55:29 +0000
@@ -15,12 +15,7 @@
 		},
 		password: {
 			required: true,
-			rangelength: [ 1, 80 ],
-			notequalto : "#username",
-		},
-		retypePassword : {
-			required: true,
-			equalTo: "#password"
+			rangelength: [ 1, 80 ]
 		},
 		email: {
 			required: true,
@@ -31,16 +26,45 @@
 };
 
 $( document ).ready( function() {
-	jQuery( "#accountForm" ).validate( {
+	
+	Recaptcha.create( "6LcM6tcSAAAAANwYsFp--0SYtcnze_WdYn8XwMMk", "recaptchaDiv", {
+		callback: Recaptcha.focus_response_field
+	} );
+	
+	$( "#recaptchaValidationField" ).hide();
+	
+	$( "#accountForm" ).validate( {
 		rules: validationRules.rules,
+		submitHandler: accountSubmitHandler,
 		errorPlacement: function( error, element ) {
 			element.parent( "td" ).append( "<br>" ).append( error );
 		}
 	} );
 	
-	jQuery.extend( jQuery.validator.messages, {
+	$.extend( jQuery.validator.messages, {
 	    required: "This field is required",
 	    rangelength: "Please enter a value between 1 and 80 characters long",
 	    email: "Please enter a valid email address"
 	} );
-} );
\ No newline at end of file
+} );
+
+function accountSubmitHandler()
+{
+	$.ajax( {
+		url: "../../api/account",
+		data: $( "#accountForm" ).serialize(),
+		type: "POST",
+		success: function( data ) {
+			alert("Account created");
+		},
+		error: function( jqXHR, textStatus, errorThrown ) {
+			$( "#messageSpan" ).show().text( jqXHR.responseText );
+			Recaptcha.reload();
+		}
+	} );
+}
+
+function reloadRecaptcha()
+{
+	Recaptcha.reload();
+}
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/useraccount/account.vm'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/useraccount/account.vm	2012-10-22 11:37:45 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/useraccount/account.vm	2012-10-22 14:55:29 +0000
@@ -5,6 +5,7 @@
     <script type="text/javascript" src="../javascripts/jQuery/jquery.min.js"></script>
     <script type="text/javascript" src="../javascripts/jQuery/jquery.validate.js"></script>
     <script type="text/javascript" src="../javascripts/useraccount/account.js"></script>
+    <script type="text/javascript" src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js";></script>
     <link type="text/css" rel="stylesheet" href="../css/account.css">
 </head>
 <body>
@@ -49,15 +50,15 @@
     </tr>
     <tr>
     	<td><label>$i18n.getString( "prove_you_are_not_a_robot" )</label></td>
-    	<td style="padding-bottom: 5px">
-		<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LcM6tcSAAAAANwYsFp--0SYtcnze_WdYn8XwMMk";></script>
-		<noscript>
-		  <iframe src="https://www.google.com/recaptcha/api/noscript?k=6LcM6tcSAAAAANwYsFp--0SYtcnze_WdYn8XwMMk";
-		    height="300" width="500" frameborder="0"></iframe><br>
-		  <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
-		  <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
-		</noscript>
-		</td>
+    	<td style="padding-bottom: 5px"><div id="recaptchaDiv"></div></td>
+	</tr>
+    <tr>
+        <td></td>
+        <td><a href="javascript:reloadRecaptcha()">$i18n.getString( "cant_read_the_words_try_another_one" )</a></span></td>
+    </tr>
+	<tr>
+		<td></td>
+		<td><label id="messageSpan" class="error" style="display:none"></label></td>
 	</tr>
     <tr>
     	<td></td>

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties'
--- dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties	2012-10-22 11:37:45 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties	2012-10-22 14:55:29 +0000
@@ -126,6 +126,7 @@
 create_new_account=Create a new account
 prove_you_are_not_a_robot=Prove you are not a robot
 create=Create
+cant_read_the_words_try_another_one=Can't read the words? Try another one
 
 #-- PeriodTypes ----------------------------------------------------------------#