dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #38447
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19581: changed AccountController to use WebMessage for responses where applicable
------------------------------------------------------------
revno: 19581
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-07-08 15:12:45 +0700
message:
changed AccountController to use WebMessage for responses where applicable
modified:
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/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/javascripts/useraccount/restore.js
--
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/webapi/controller/AccountController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AccountController.java 2015-07-08 07:25:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AccountController.java 2015-07-08 08:12:45 +0000
@@ -33,6 +33,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hisp.dhis.configuration.ConfigurationService;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.security.RestoreOptions;
import org.hisp.dhis.security.RestoreType;
@@ -44,7 +45,9 @@
import org.hisp.dhis.user.UserAuthorityGroup;
import org.hisp.dhis.user.UserCredentials;
import org.hisp.dhis.user.UserService;
+import org.hisp.dhis.webapi.service.WebMessageService;
import org.hisp.dhis.webapi.utils.ContextUtils;
+import org.hisp.dhis.webapi.utils.WebMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -107,41 +110,41 @@
@Autowired
private SystemSettingManager systemSettingManager;
+ @Autowired
+ private WebMessageService webMessageService;
+
private ObjectMapper objectMapper = new ObjectMapper();
@RequestMapping( value = "/recovery", method = RequestMethod.POST )
public void recoverAccount(
@RequestParam String username,
HttpServletRequest request,
- HttpServletResponse response )
+ HttpServletResponse response ) throws WebMessageException
{
String rootPath = ContextUtils.getContextPath( request );
if ( !systemSettingManager.accountRecoveryEnabled() )
{
- ContextUtils.conflictResponse( response, "Account recovery is not enabled" );
- return;
+ throw new WebMessageException( WebMessageUtils.conflict( "Account recovery is not enabled" ) );
}
UserCredentials credentials = userService.getUserCredentialsByUsername( username );
if ( credentials == null )
{
- ContextUtils.conflictResponse( response, "User does not exist: " + username );
- return;
+ throw new WebMessageException( WebMessageUtils.conflict( "User does not exist: " + username ) );
}
boolean recover = securityService.sendRestoreMessage( credentials, rootPath, RestoreOptions.RECOVER_PASSWORD_OPTION );
if ( !recover )
{
- ContextUtils.conflictResponse( response, "Account could not be created" );
- return;
+ throw new WebMessageException( WebMessageUtils.conflict( "Account could not be recovered" ) );
}
log.info( "Recovery message sent for user: " + username );
- ContextUtils.okResponse( response, "Recovery message sent" );
+ webMessageService.send( WebMessageUtils.ok( "Recovery message sent" ), response, request );
}
@RequestMapping( value = "/restore", method = RequestMethod.POST )
@@ -151,45 +154,40 @@
@RequestParam String code,
@RequestParam String password,
HttpServletRequest request,
- HttpServletResponse response )
+ HttpServletResponse response ) throws WebMessageException
{
if ( !systemSettingManager.accountRecoveryEnabled() )
{
- ContextUtils.conflictResponse( response, "Account recovery is not enabled" );
- return;
+ throw new WebMessageException( WebMessageUtils.conflict( "Account recovery is not enabled" ) );
}
if ( password == null || !ValidationUtils.passwordIsValid( password ) )
{
- ContextUtils.badRequestResponse( response, "Password is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Password is not specified or invalid" ) );
}
if ( password.trim().equals( username.trim() ) )
{
- ContextUtils.badRequestResponse( response, "Password cannot be equal to username" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Password cannot be equal to username" ) );
}
UserCredentials credentials = userService.getUserCredentialsByUsername( username );
if ( credentials == null )
{
- ContextUtils.conflictResponse( response, "User does not exist: " + username );
- return;
+ throw new WebMessageException( WebMessageUtils.conflict( "User does not exist: " + username ) );
}
boolean restore = securityService.restore( credentials, token, code, password, RestoreType.RECOVER_PASSWORD );
if ( !restore )
{
- ContextUtils.badRequestResponse( response, "Account could not be restored" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Account could not be restored" ) );
}
log.info( "Account restored for user: " + username );
- ContextUtils.okResponse( response, "Account restored" );
+ webMessageService.send( WebMessageUtils.ok( "Account restored" ), response, request );
}
@RequestMapping( method = RequestMethod.POST )
@@ -207,7 +205,7 @@
@RequestParam( value = "recaptcha_challenge_field", required = false ) String recapChallenge,
@RequestParam( value = "recaptcha_response_field", required = false ) String recapResponse,
HttpServletRequest request,
- HttpServletResponse response )
+ HttpServletResponse response ) throws WebMessageException
{
UserCredentials credentials = null;
@@ -221,16 +219,14 @@
if ( credentials == null )
{
- ContextUtils.badRequestResponse( response, "Invitation link not valid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Invitation link not valid" ) );
}
boolean canRestore = securityService.canRestore( credentials, inviteToken, inviteCode, RestoreType.INVITE );
if ( !canRestore )
{
- ContextUtils.badRequestResponse( response, "Invitation code not valid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Invitation code not valid" ) );
}
RestoreOptions restoreOptions = securityService.getRestoreOptions( inviteToken );
@@ -243,8 +239,7 @@
if ( !allowed )
{
- ContextUtils.badRequestResponse( response, "User self registration is not allowed" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "User self registration is not allowed" ) );
}
}
@@ -268,71 +263,61 @@
if ( username == null || username.trim().length() > MAX_LENGTH )
{
- ContextUtils.badRequestResponse( response, "User name is not specified or invalid" );
+ throw new WebMessageException( WebMessageUtils.badRequest( "User name is not specified or invalid" ) );
}
UserCredentials usernameAlreadyTakenCredentials = userService.getUserCredentialsByUsername( username );
if ( canChooseUsername && usernameAlreadyTakenCredentials != null )
{
- ContextUtils.badRequestResponse( response, "User name is already taken" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "User name is already taken" ) );
}
if ( firstName == null || firstName.trim().length() > MAX_LENGTH )
{
- ContextUtils.badRequestResponse( response, "First name is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "First name is not specified or invalid" ) );
}
if ( surname == null || surname.trim().length() > MAX_LENGTH )
{
- ContextUtils.badRequestResponse( response, "Last name is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Last name is not specified or invalid" ) );
}
if ( password == null || !ValidationUtils.passwordIsValid( password ) )
{
- ContextUtils.badRequestResponse( response, "Password is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Password is not specified or invalid" ) );
}
if ( password.trim().equals( username != null ? username.trim() : null ) )
{
- ContextUtils.badRequestResponse( response, "Password cannot be equal to username" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Password cannot be equal to username" ) );
}
if ( email == null || !ValidationUtils.emailIsValid( email ) )
{
- ContextUtils.badRequestResponse( response, "Email is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Email is not specified or invalid" ) );
}
if ( phoneNumber == null || phoneNumber.trim().length() > 30 )
{
- ContextUtils.badRequestResponse( response, "Phone number is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Phone number is not specified or invalid" ) );
}
if ( employer == null || employer.trim().length() > MAX_LENGTH )
{
- ContextUtils.badRequestResponse( response, "Employer is not specified or invalid" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Employer is not specified or invalid" ) );
}
if ( !systemSettingManager.selfRegistrationNoRecaptcha() )
{
if ( recapChallenge == null )
{
- ContextUtils.badRequestResponse( response, "Recaptcha challenge must be specified" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Recaptcha challenge must be specified" ) );
}
if ( recapResponse == null )
{
- ContextUtils.badRequestResponse( response, "Recaptcha response must be specified" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Recaptcha response must be specified" ) );
}
// ---------------------------------------------------------------------
@@ -343,8 +328,7 @@
if ( results == null || results.length == 0 )
{
- ContextUtils.errorResponse( response, "Captcha could not be verified due to a server error" );
- return;
+ throw new WebMessageException( WebMessageUtils.error( "Captcha could not be verified due to a server error" ) );
}
// ---------------------------------------------------------------------
@@ -355,8 +339,7 @@
{
log.info( "Recaptcha failed with code: " + (results.length > 0 ? results[1] : "") );
- ContextUtils.badRequestResponse( response, "The characters you entered did not match the word verification, try again" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "The characters you entered did not match the word verification, try again" ) );
}
}
@@ -372,8 +355,7 @@
{
log.info( "Invite restore failed for: " + inviteUsername );
- ContextUtils.badRequestResponse( response, "Unable to create invited user account" );
- return;
+ throw new WebMessageException( WebMessageUtils.badRequest( "Unable to create invited user account" ) );
}
User user = credentials.getUser();
@@ -431,7 +413,7 @@
authenticate( username, password, authorities, request );
- ContextUtils.createdResponse( response, "Account created", null );
+ webMessageService.send( WebMessageUtils.ok( "Account created" ), response, request );
}
@RequestMapping( value = "/password", method = RequestMethod.POST )
=== 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 2014-01-17 03:48:57 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/account.js 2015-07-08 08:12:45 +0000
@@ -1,47 +1,47 @@
var validationRules = {
- rules: {
- firstName: {
- required: true,
- rangelength: [ 2, 80 ]
- },
- surname: {
- required: true,
- rangelength: [ 2, 80 ]
- },
- username: {
- required: true,
- rangelength: [ 4, 80 ],
- remote: "../../api/account/username"
- },
- password: {
- required: true,
- rangelength: [ 8, 80 ],
- password: true,
- notequalto: "#username",
- },
- retypePassword: {
- required: true,
- equalTo: "#password",
- },
- email: {
- required: true,
- email: true,
- rangelength: [ 4, 80 ]
- },
- inviteEmail : {
- required : true,
- email : true,
- rangelength : [ 4, 80 ]
- },
- phoneNumber: {
- required: true,
- rangelength: [ 6, 30 ]
- },
- employer: {
- required: true,
- rangelength: [ 2, 80 ]
- }
+ rules: {
+ firstName: {
+ required: true,
+ rangelength: [2, 80]
+ },
+ surname: {
+ required: true,
+ rangelength: [2, 80]
+ },
+ username: {
+ required: true,
+ rangelength: [4, 80],
+ remote: "../../api/account/username"
+ },
+ password: {
+ required: true,
+ rangelength: [8, 80],
+ password: true,
+ notequalto: "#username",
+ },
+ retypePassword: {
+ required: true,
+ equalTo: "#password",
+ },
+ email: {
+ required: true,
+ email: true,
+ rangelength: [4, 80]
+ },
+ inviteEmail: {
+ required: true,
+ email: true,
+ rangelength: [4, 80]
+ },
+ phoneNumber: {
+ required: true,
+ rangelength: [6, 30]
+ },
+ employer: {
+ required: true,
+ rangelength: [2, 80]
}
+ }
};
@@ -50,77 +50,78 @@
$(document).ready(function() {
- var locale = localStorage[login.localeKey];
-
- if( undefined !== locale && locale ) {
- login.changeLocale(locale);
- }
-
- if( recaptchaEnabled() ) {
- Recaptcha.create("6LcM6tcSAAAAANwYsFp--0SYtcnze_WdYn8XwMMk", "recaptchaDiv", {
- theme: "white"
- });
- }
-
- $("#accountForm").validate({
- rules: validationRules.rules,
- submitHandler: accountSubmitHandler,
- errorPlacement: function( error, element ) {
- element.parent("td").append("<br>").append(error);
- }
+ var locale = localStorage[login.localeKey];
+
+ if( undefined !== locale && locale ) {
+ login.changeLocale(locale);
+ }
+
+ if( recaptchaEnabled() ) {
+ Recaptcha.create("6LcM6tcSAAAAANwYsFp--0SYtcnze_WdYn8XwMMk", "recaptchaDiv", {
+ theme: "white"
});
+ }
+
+ $("#accountForm").validate({
+ rules: validationRules.rules,
+ submitHandler: accountSubmitHandler,
+ errorPlacement: function(error, element) {
+ element.parent("td").append("<br>").append(error);
+ }
+ });
});
function accountSubmitHandler() {
- if( recaptchaEnabled() ) {
- if( $.trim($("#recaptcha_challenge_field").val()).length == 0 ||
- $.trim($("#recaptcha_response_field").val()).length == 0 ) {
- $("#messageSpan").show().text("Please enter a value for the word verification above");
- return false;
- }
- }
-
- $("#submitButton").attr("disabled", "disabled");
-
- $.ajax({
- url: "../../api/account",
- data: $("#accountForm").serialize(),
- type: "post",
- success: function( data ) {
- window.location.href = "../../dhis-web-commons-about/redirect.action";
- },
- error: function( jqXHR, textStatus, errorThrown ) {
- $("#messageSpan").show().text(jqXHR.responseText);
- reloadRecaptcha();
- $("#submitButton").removeAttr("disabled");
- }
- });
+ if( recaptchaEnabled() ) {
+ if( $.trim($("#recaptcha_challenge_field").val()).length == 0 ||
+ $.trim($("#recaptcha_response_field").val()).length == 0 ) {
+ $("#messageSpan").show().text("Please enter a value for the word verification above");
+ return false;
+ }
+ }
+
+ $("#submitButton").attr("disabled", "disabled");
+
+ $.ajax({
+ url: "../../api/account",
+ data: $("#accountForm").serialize(),
+ type: "post",
+ success: function(data) {
+ window.location.href = "../../dhis-web-commons-about/redirect.action";
+ },
+ error: function(jqXHR, textStatus, errorThrown) {
+ var error = JSON.parse(jqXHR.responseText);
+ $("#messageSpan").show().text(error.message);
+ reloadRecaptcha();
+ $("#submitButton").removeAttr("disabled");
+ }
+ });
}
function recaptchaEnabled() {
- return typeof Recaptcha !== 'undefined';
+ return typeof Recaptcha !== 'undefined';
}
function reloadRecaptcha() {
- if( recaptchaEnabled() ) {
- Recaptcha.reload();
- }
+ if( recaptchaEnabled() ) {
+ Recaptcha.reload();
+ }
}
-login.changeLocale = function( locale ) {
- $.get('accountStrings.action?loc=' + locale, function( json ) {
- $('#create_new_account').html(json.create_new_account);
- $('#label_firstName').html(json.name);
- $('#firstName').attr("placeholder", json.first_name);
- $('#surname').attr("placeholder", json.last_name);
- $('#label_username').html(json.user_name);
- $('#label_password').html(json.password);
- $('#label_retypePassword').html(json.confirm_password);
- $('#label_email').html(json.email);
- $('#label_mobile_phone').html(json.mobile_phone);
- $('#label_employer').html(json.employer);
- $('#label_recaptchaDiv').html(json.prove_not_robot);
- $('#cant_read_words').html(json.cant_read_words);
- $('#submitButton').val(json.create);
- });
+login.changeLocale = function(locale) {
+ $.get('accountStrings.action?loc=' + locale, function(json) {
+ $('#create_new_account').html(json.create_new_account);
+ $('#label_firstName').html(json.name);
+ $('#firstName').attr("placeholder", json.first_name);
+ $('#surname').attr("placeholder", json.last_name);
+ $('#label_username').html(json.user_name);
+ $('#label_password').html(json.password);
+ $('#label_retypePassword').html(json.confirm_password);
+ $('#label_email').html(json.email);
+ $('#label_mobile_phone').html(json.mobile_phone);
+ $('#label_employer').html(json.employer);
+ $('#label_recaptchaDiv').html(json.prove_not_robot);
+ $('#cant_read_words').html(json.cant_read_words);
+ $('#submitButton').val(json.create);
+ });
}
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/restore.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/restore.js 2015-07-08 07:25:31 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/useraccount/restore.js 2015-07-08 08:12:45 +0000
@@ -16,7 +16,6 @@
};
$(document).ready(function() {
-
$("#restoreForm").validate({
rules: validationRules.rules,
submitHandler: restoreSubmitHandler,